Package io.github.og4dev.exception
Class ApiException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
io.github.og4dev.exception.ApiException
- All Implemented Interfaces:
Serializable
Base abstract exception class for custom API-related business logic exceptions.
This class provides a foundation for creating domain-specific exceptions with associated
HTTP status codes. Extend this class to create business logic exceptions that are automatically
handled by GlobalExceptionHandler and converted to
RFC 9457 ProblemDetail responses.
Usage Example:
public class ResourceNotFoundException extends ApiException {
public ResourceNotFoundException(String resource, Long id) {
super(String.format("%s not found with ID: %d", resource, id), HttpStatus.NOT_FOUND);
}
}
public class InsufficientFundsException extends ApiException {
public InsufficientFundsException(String accountId) {
super("Insufficient funds in account: " + accountId, HttpStatus.PAYMENT_REQUIRED);
}
}
Benefits:
- Automatic exception handling - No need to create
@ExceptionHandlermethods - RFC 9457 ProblemDetail formatting - Industry-standard error responses
- Type-safe with compile-time checking
- Clean, readable code - Express business rules clearly
- Consistent error responses - All custom exceptions follow the same format
- Since:
- 1.0.0
- Version:
- 1.0.0
- Author:
- Pasindu OG
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.http.HttpStatusGets the HTTP status code associated with this exception.Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
-
Method Details
-
getStatus
public org.springframework.http.HttpStatus getStatus()Gets the HTTP status code associated with this exception.- Returns:
- the HTTP status
-