Class EnduranceTrioException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
com.endurancetrio.business.common.exception.EnduranceTrioException
- All Implemented Interfaces:
Serializable
The
EnduranceTrioException class represents all custom exceptions in the EnduranceTrio
project. It extends RuntimeException and carries HTTP status codes aligned with
REST semantics, along with structured error details.
Usage Patterns
Single Error with Generic Message
Use when the error is self-explanatory and the enum's default message suffices:
throw new EnduranceTrioException(new ErrorDTO(EnduranceTrioError.NOT_FOUND));
Response: details: "The requested resource was not found"
Single Error with Specific Context
Use when you need to provide request-specific details to the client:
String errorMsg = String.format("No route found with ID %d", routeId);
throw new EnduranceTrioException(new ErrorDTO(EnduranceTrioError.NOT_FOUND, errorMsg));
Response:
{
"details": "The requested resource was not found",
"data": [
{
"error": "NOT_FOUND",
"message": "No route found with ID 5"
}
]
}
Wrapping Lower-Layer Exceptions
Use when catching and re-throwing to preserve the root cause:
try {
repository.save(entity);
} catch (DataIntegrityViolationException ex) {
throw new EnduranceTrioException(
new ErrorDTO(EnduranceTrioError.CONCURRENT_UPDATE, "Version conflict detected"), ex
);
}
The original DataIntegrityViolationException is preserved in the cause chain and logged
for debugging.
Multiple Errors (Validation)
Use for aggregated validation failures (e.g., multiple field errors):
List<ErrorDTO> errors = List.of(
new ErrorDTO(EnduranceTrioError.BAD_REQUEST, "Name must not be blank"),
new ErrorDTO(EnduranceTrioError.BAD_REQUEST, "Distance must be positive")
);
throw new EnduranceTrioException(errors);
HTTP Code Mapping
Thecode field is intentionally coupled to HTTP status codes for REST API clarity:
- 400 (Bad Request): Invalid client input
- 404 (Not Found): Resource does not exist
- 409 (Conflict): Optimistic locking or concurrent modification
EnduranceTrioError.- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionEnduranceTrioException(@NonNull ErrorDTO error) EnduranceTrioException(@NonNull List<ErrorDTO> errors) EnduranceTrioException(@NonNull List<ErrorDTO> errors, Throwable cause) -
Method Summary
Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
-
Constructor Details
-
EnduranceTrioException
-
EnduranceTrioException
-
EnduranceTrioException
-
-
Method Details
-
getCode
public int getCode() -
getErrors
-