Class ApiException

All Implemented Interfaces:
Serializable

public abstract class ApiException extends RuntimeException
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 @ExceptionHandler methods
  • 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 Details

    • getStatus

      public org.springframework.http.HttpStatus getStatus()
      Gets the HTTP status code associated with this exception.
      Returns:
      the HTTP status