Class RestServiceExceptionFacade

  • All Implemented Interfaces:
    javax.ws.rs.ext.ExceptionMapper<Throwable>

    @Provider
    public class RestServiceExceptionFacade
    extends Object
    implements javax.ws.rs.ext.ExceptionMapper<Throwable>
    This is an implementation of ExceptionMapper that acts as generic exception facade for REST services. It maps exceptions to an according HTTP status code and JSON result as defined by devonfw REST error specification.
    Since:
    2.0.0
    • Constructor Detail

      • RestServiceExceptionFacade

        public RestServiceExceptionFacade()
        The constructor.
    • Method Detail

      • registerToplevelSecurityException

        protected void registerToplevelSecurityException​(Class<? extends Throwable> securityException)
        Registers a Class as a top-level security exception. Instances of this class and all its subclasses will be handled as security errors. Therefore an according HTTP error code is used and no further details about the exception is send to the client to prevent sensitive data exposure.
        Parameters:
        securityException - is the Class reflecting the security error.
      • registerToplevelSecurityExceptions

        protected void registerToplevelSecurityExceptions()
        This method registers the top-level security exceptions. You may override it to add additional or other classes.
      • registerToplevelSecurityExceptions

        protected void registerToplevelSecurityExceptions​(String className)
        Parameters:
        className - the className to be registered
      • toResponse

        public javax.ws.rs.core.Response toResponse​(Throwable exception)
        Specified by:
        toResponse in interface javax.ws.rs.ext.ExceptionMapper<Throwable>
      • toResponse

        protected javax.ws.rs.core.Response toResponse​(Throwable exception,
                                                       Throwable catched)
        Parameters:
        exception - the exception to handle
        catched - the original exception that was cached. Either same as error or a (child-) cause of it.
        Returns:
        the response build from the exception.
        See Also:
        toResponse(Throwable)
      • createResponse

        protected javax.ws.rs.core.Response createResponse​(Throwable exception,
                                                           net.sf.mmm.util.exception.api.ValidationErrorUserException error,
                                                           Map<String,​List<String>> errorsMap)
        Creates the Response for the given validation exception.
        Parameters:
        exception - is the original validation exception.
        error - is the wrapped exception or the same as exception.
        errorsMap - is a map with all validation errors
        Returns:
        the requested Response.
      • handleGenericError

        protected javax.ws.rs.core.Response handleGenericError​(Throwable exception,
                                                               Throwable catched)
        Exception handling for generic exception (fallback).
        Parameters:
        exception - the exception to handle
        catched - the original exception that was cached. Either same as error or a (child-) cause of it.
        Returns:
        the response build from the exception
      • handleSecurityError

        protected javax.ws.rs.core.Response handleSecurityError​(Throwable exception,
                                                                Throwable catched)
        Exception handling for security exception.
        Parameters:
        exception - the exception to handle
        catched - the original exception that was cached. Either same as error or a (child-) cause of it.
        Returns:
        the response build from exception
      • handleValidationException

        protected javax.ws.rs.core.Response handleValidationException​(Throwable exception,
                                                                      Throwable catched)
        Exception handling for validation exception.
        Parameters:
        exception - the exception to handle
        catched - the original exception that was cached. Either same as error or a (child-) cause of it.
        Returns:
        the response build from the exception.
      • getExposedErrorDetails

        protected String getExposedErrorDetails​(Throwable error)
        Parameters:
        error - is the Throwable to extract message details from.
        Returns:
        the exposed message(s).
      • createResponse

        protected javax.ws.rs.core.Response createResponse​(net.sf.mmm.util.exception.api.NlsRuntimeException error)
        Create the Response for the given NlsRuntimeException.
        Parameters:
        error - the generic NlsRuntimeException.
        Returns:
        the corresponding Response.
      • createResponse

        protected javax.ws.rs.core.Response createResponse​(javax.ws.rs.core.Response.Status status,
                                                           net.sf.mmm.util.exception.api.NlsRuntimeException error,
                                                           Map<String,​List<String>> errorsMap)
        Create a response message as a JSON-String from the given parts.
        Parameters:
        status - is the HTTP Response.Status.
        error - is the catched or wrapped NlsRuntimeException.
        errorsMap - is a map with all validation errors
        Returns:
        the corresponding Response.
      • createResponse

        protected javax.ws.rs.core.Response createResponse​(javax.ws.rs.core.Response.Status status,
                                                           net.sf.mmm.util.exception.api.NlsRuntimeException error,
                                                           String message,
                                                           Map<String,​List<String>> errorsMap)
        Create a response message as a JSON-String from the given parts.
        Parameters:
        status - is the HTTP Response.Status.
        error - is the catched or wrapped NlsRuntimeException.
        message - is the JSON message attribute.
        errorsMap - is a map with all validation errors
        Returns:
        the corresponding Response.
      • createResponse

        protected javax.ws.rs.core.Response createResponse​(javax.ws.rs.core.Response.Status status,
                                                           net.sf.mmm.util.exception.api.NlsRuntimeException error,
                                                           String message,
                                                           String code,
                                                           Map<String,​List<String>> errorsMap)
        Create a response message as a JSON-String from the given parts.
        Parameters:
        status - is the HTTP Response.Status.
        error - is the catched or wrapped NlsRuntimeException.
        message - is the JSON message attribute.
        code - is the error code.
        errorsMap - is a map with all validation errors
        Returns:
        the corresponding Response.
      • createResponse

        protected javax.ws.rs.core.Response createResponse​(javax.ws.rs.core.Response.Status status,
                                                           String message,
                                                           String code,
                                                           UUID uuid,
                                                           Map<String,​List<String>> errorsMap)
        Create a response message as a JSON-String from the given parts.
        Parameters:
        status - is the HTTP Response.Status.
        message - is the JSON message attribute.
        code - is the error code.
        uuid - the UUID of the response message.
        errorsMap - is a map with all validation errors
        Returns:
        the corresponding Response.
      • createJsonErrorResponseMessage

        protected String createJsonErrorResponseMessage​(String message,
                                                        String code,
                                                        UUID uuid,
                                                        Map<String,​List<String>> errorsMap)
        Create a response message as a JSON-String from the given parts.
        Parameters:
        message - the message of the response message
        code - the code of the response message
        uuid - the uuid of the response message
        errorsMap - is a map with all validation errors
        Returns:
        the response message as a JSON-String
      • createResponse

        protected javax.ws.rs.core.Response createResponse​(javax.ws.rs.WebApplicationException exception)
        Add a response message to an existing response.
        Parameters:
        exception - the WebApplicationException.
        Returns:
        the response with the response message added
      • getMapper

        public com.fasterxml.jackson.databind.ObjectMapper getMapper()
        Returns:
        the ObjectMapper for JSON mapping.
      • setMapper

        @Inject
        public void setMapper​(com.fasterxml.jackson.databind.ObjectMapper mapper)
        Parameters:
        mapper - the mapper to set
      • setExposeInternalErrorDetails

        public void setExposeInternalErrorDetails​(boolean exposeInternalErrorDetails)
        Parameters:
        exposeInternalErrorDetails - - true if internal exception details shall be exposed to clients (useful for debugging and testing), false if such details are hidden to prevent Sensitive Data Exposure (default, has to be used in production environment).