Skip to content

Latest commit

 

History

History
51 lines (35 loc) · 1.96 KB

exceptions.adoc

File metadata and controls

51 lines (35 loc) · 1.96 KB

Exceptions

Note

This article needs further clarification. Help the project by contributing.

Business exceptions

Use a common base class

  • 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, NotFound and IncorrectInput. 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.

Exception handler

  • the central entry point for exceptions in the application

  • is annotated with @ExceptionHandler at 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 class

  • 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.).