java.lang.Object
de.iip_ecosphere.platform.services.environment.spring.metricsProvider.RestAdvice

@ControllerAdvice public class RestAdvice extends Object
This class aims to provide a handler for expected exceptions that could occur when using the MetricsProviderRestService.
This class contains a mechanism implemented by Spring Rest Controller that allows the creation of custom exception handlers to expected exceptions that may occur during requests and, as a result provide a more custom response to the request.
There are two types of exceptions handled by this class:
  • IllegalArgumentExceptions
  • NumberFormatExceptions
Author:
Miguel Gomez
  • Constructor Details

    • RestAdvice

      public RestAdvice()
  • Method Details

    • illegalArgumentExceptionHandler

      @ResponseBody @ExceptionHandler(java.lang.IllegalArgumentException.class) @ResponseStatus(UNAUTHORIZED) public String illegalArgumentExceptionHandler(IllegalArgumentException iae)
      Captures and handles an IllegalArgumentException.
      Methods within the MetricsProvider may throw an IllegalArgumentException if the provided arguments are not valid or suitable for the execution of a certain task. If the MetricsProviderRestService receives a request that cannot be executed due to invalid arguments, this handler will return an client error status with the message from the produced exception in the response body.
      Parameters:
      iae - IllegalArgumentException that has been thrown
      Returns:
      the message nested in the exception as a response body of a HTTP response code 4XX
    • numberFormatExceptionHandler

      @ResponseBody @ExceptionHandler(java.lang.NumberFormatException.class) @ResponseStatus(BAD_REQUEST) public String numberFormatExceptionHandler(NumberFormatException nfe)
      Captures and handles an NumberFormatException.
      If the request sent to the MetricsProviderRestService contains a string that cannot be parsed into a valid number that is expected by the method, this exception will be thrown. The message indicating that the request couldn't be parsed will be sent as response body.
      Parameters:
      nfe - NumberFormatException that has been thrown
      Returns:
      the message nested in the exception as a response body of a HTTP response code 4XX