Class ControllerAdvisor

java.lang.Object
org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
edu.ntnu.idatt2106_2023_06.backend.exception.ControllerAdvisor
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.context.MessageSourceAware

@RestControllerAdvice public class ControllerAdvisor extends org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
ControllerAdvisor is a global exception handler for the Spring controllers. It, therefore, provides methods for handling exceptions thrown in the controllers. Thereafter, it returns an HTTP response entity with a body containing the details of the exception and the status code to be returned.
Author:
Leon Egeberg Hesthaug, Brage Halvorsen Kvamme, Trym Hamer Gudvangen
  • Field Summary

    Fields inherited from class org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler

    logger, PAGE_NOT_FOUND_LOG_CATEGORY, pageNotFoundLogger
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    org.springframework.http.ResponseEntity<Object>
    exceptionAction(Exception e, org.springframework.web.context.request.WebRequest webRequest)
    This method handles all uncaught exceptions in the application and returns a response with a 400 Bad Request status code.
    org.springframework.http.ResponseEntity<Object>
    existsAction(ExistsException e, org.springframework.web.context.request.WebRequest webRequest)
    This method handles ExistsException by returning an HTTP response entity with a body containing the time of the error and the message to be returned to the client with a status code of 400.
    org.springframework.http.ResponseEntity<Object>
    illegalStatValueExceptionAction(IllegalStatValueException e, org.springframework.web.context.request.WebRequest webRequest)
    This method handles ServletExceptions in the application and returns a response with a 500 Internal Server Error status code.
    org.springframework.http.ResponseEntity<Object>
    ioExceptionAction(IOException e, org.springframework.web.context.request.WebRequest webRequest)
    This method handles IOExceptions in the application and returns a response with a 500 Internal Server Error status code.
    org.springframework.http.ResponseEntity<Object>
    jwtExceptionAction(jakarta.servlet.ServletException e, org.springframework.web.context.request.WebRequest webRequest)
    This method handles JwtExceptions in the application and returns a response with a 400 Bad Request status code.
    org.springframework.http.ResponseEntity<Object>
    notFoundAction(NotFoundException e, org.springframework.web.context.request.WebRequest webRequest)
    This method handles NotFoundException by returning an HTTP response entity with a body containing the time of the error and the message to be returned to the client with a status code of 404.
    org.springframework.http.ResponseEntity<Object>
    runtimeExceptionAction(RuntimeException e, org.springframework.web.context.request.WebRequest webRequest)
    This method handles RuntimeExceptions in the application and returns a response with a 500 Internal Server Error status code.
    org.springframework.http.ResponseEntity<Object>
    unauthorizedAction(UnauthorizedException e, org.springframework.web.context.request.WebRequest webRequest)
    This method handles UnauthorizedExceptions by returning an HTTP response entity with a body containing the time of the error and the message to be returned to the client with a status code of 401.

    Methods inherited from class org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler

    createProblemDetail, createResponseEntity, getMessageSource, handleAsyncRequestTimeoutException, handleBindException, handleConversionNotSupported, handleErrorResponseException, handleException, handleExceptionInternal, handleHttpMediaTypeNotAcceptable, handleHttpMediaTypeNotSupported, handleHttpMessageNotReadable, handleHttpMessageNotWritable, handleHttpRequestMethodNotSupported, handleMethodArgumentNotValid, handleMissingPathVariable, handleMissingServletRequestParameter, handleMissingServletRequestPart, handleNoHandlerFoundException, handleServletRequestBindingException, handleTypeMismatch, setMessageSource

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ControllerAdvisor

      public ControllerAdvisor()
  • Method Details

    • unauthorizedAction

      @ExceptionHandler(UnauthorizedException.class) @ResponseStatus(UNAUTHORIZED) public org.springframework.http.ResponseEntity<Object> unauthorizedAction(UnauthorizedException e, org.springframework.web.context.request.WebRequest webRequest)
      This method handles UnauthorizedExceptions by returning an HTTP response entity with a body containing the time of the error and the message to be returned to the client with a status code of 401.
      Parameters:
      e - The UnauthorizedException that was thrown
      webRequest - The request that was made
      Returns:
      The HTTP response entity containing the details of the exception and status code
    • existsAction

      @ExceptionHandler(ExistsException.class) @ResponseStatus(BAD_REQUEST) public org.springframework.http.ResponseEntity<Object> existsAction(ExistsException e, org.springframework.web.context.request.WebRequest webRequest)
      This method handles ExistsException by returning an HTTP response entity with a body containing the time of the error and the message to be returned to the client with a status code of 400.
      Parameters:
      e - The ExistsException that was thrown
      webRequest - The request that was made
      Returns:
      The HTTP response entity containing the details of the exception and status code
    • notFoundAction

      @ExceptionHandler(NotFoundException.class) @ResponseStatus(NOT_FOUND) public org.springframework.http.ResponseEntity<Object> notFoundAction(NotFoundException e, org.springframework.web.context.request.WebRequest webRequest)
      This method handles NotFoundException by returning an HTTP response entity with a body containing the time of the error and the message to be returned to the client with a status code of 404.
      Parameters:
      e - The NotFoundException that was thrown
      webRequest - The request that was made
      Returns:
      The HTTP response entity containing the details of the exception and status code
    • exceptionAction

      @ExceptionHandler(java.lang.Exception.class) @ResponseStatus(BAD_REQUEST) public org.springframework.http.ResponseEntity<Object> exceptionAction(Exception e, org.springframework.web.context.request.WebRequest webRequest)
      This method handles all uncaught exceptions in the application and returns a response with a 400 Bad Request status code.
      Parameters:
      e - The exception that was thrown
      webRequest - The current web request
      Returns:
      ResponseEntity with a JSON body containing the error message and a 400 Bad Request status code
    • ioExceptionAction

      @ExceptionHandler(java.io.IOException.class) @ResponseStatus(INTERNAL_SERVER_ERROR) public org.springframework.http.ResponseEntity<Object> ioExceptionAction(IOException e, org.springframework.web.context.request.WebRequest webRequest)
      This method handles IOExceptions in the application and returns a response with a 500 Internal Server Error status code.
      Parameters:
      e - The IOException that was thrown
      webRequest - The current web request
      Returns:
      ResponseEntity with a JSON body containing the error message and a 500 Internal Server Error status code
    • jwtExceptionAction

      @ExceptionHandler(io.jsonwebtoken.JwtException.class) @ResponseStatus(BAD_REQUEST) public org.springframework.http.ResponseEntity<Object> jwtExceptionAction(jakarta.servlet.ServletException e, org.springframework.web.context.request.WebRequest webRequest)
      This method handles JwtExceptions in the application and returns a response with a 400 Bad Request status code.
      Parameters:
      e - The JwtException that was thrown
      webRequest - The current web request
      Returns:
      ResponseEntity with a JSON body containing the error message and a 400 Bad Request status code
    • runtimeExceptionAction

      @ExceptionHandler(java.lang.RuntimeException.class) @ResponseStatus(INTERNAL_SERVER_ERROR) public org.springframework.http.ResponseEntity<Object> runtimeExceptionAction(RuntimeException e, org.springframework.web.context.request.WebRequest webRequest)
      This method handles RuntimeExceptions in the application and returns a response with a 500 Internal Server Error status code.
      Parameters:
      e - The RuntimeException that was thrown
      webRequest - The current web request
      Returns:
      ResponseEntity with a JSON body containing the error message and a 500 Internal Server Error status code
    • illegalStatValueExceptionAction

      @ExceptionHandler(IllegalException.class) @ResponseStatus(BAD_REQUEST) public org.springframework.http.ResponseEntity<Object> illegalStatValueExceptionAction(IllegalStatValueException e, org.springframework.web.context.request.WebRequest webRequest)
      This method handles ServletExceptions in the application and returns a response with a 500 Internal Server Error status code.
      Parameters:
      e - The ServletException that was thrown
      webRequest - The current web request
      Returns:
      ResponseEntity with a JSON body containing the error message and a 500 Internal Server Error status code