|
Note
|
This article needs further clarification. Help the project by contributing. |
-
All business exceptions should inherit a common abstract base class so they can be distinguished from technical exceptions.
-
Should be inheriting
RuntimeException -
Should store the cause
Exception -
Should contain a reason enum. The enum is project individual and contains values like
Conflict,NotFoundandIncorrectInput. This enum can later be used to decide how to inform the user about the problem e.g. via HTTP status codes when building REST apis.
-
-
the central entry point for exceptions in the application
-
is annotated with
@ExceptionHandlerat each method which handles the exceptions -
each method has a return value of
ResponseEntity<ErrorResponse> -
within the method, the values for HTTPStatus, ErrorCode, Message and UUID are set
-
the class is globally annotated with
@ControllerAdvice -
for each type of Exception there should be a handler method to managet the exception
Example code exception handler:
@ControllerAdvice
public class CustomControllerAdvice {
@ExceptionHandler(BusinessException.class)
public ResponseEntity<ErrorResponse> handleBusinessException(Exception ex) {
...-
ErrorResponse: The ErrorResponse class returns the error object to the client. -
mainly consists of constructors with a different number of parameters that provides the information about the class variables (message, HTTPStatus code etc.).