Class RestServiceExceptionFacade
- java.lang.Object
-
- com.devonfw.module.rest.service.impl.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 ofExceptionMapper
that acts as generic exception facade for REST services. Itmaps
exceptions
to an according HTTP status code and JSON result as defined by devonfw REST error specification.- Since:
- 2.0.0
-
-
Constructor Summary
Constructors Constructor Description RestServiceExceptionFacade()
The constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.protected javax.ws.rs.core.Response
createResponse(Throwable exception, net.sf.mmm.util.exception.api.ValidationErrorUserException error, Map<String,List<String>> errorsMap)
Creates theResponse
for the given validation exception.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.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.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.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.protected javax.ws.rs.core.Response
createResponse(javax.ws.rs.WebApplicationException exception)
Add a response message to an existing response.protected javax.ws.rs.core.Response
createResponse(net.sf.mmm.util.exception.api.NlsRuntimeException error)
Create theResponse
for the givenNlsRuntimeException
.protected String
getExposedErrorDetails(Throwable error)
com.fasterxml.jackson.databind.ObjectMapper
getMapper()
protected javax.ws.rs.core.Response
handleGenericError(Throwable exception, Throwable catched)
Exception handling for generic exception (fallback).protected javax.ws.rs.core.Response
handleSecurityError(Throwable exception, Throwable catched)
Exception handling for security exception.protected javax.ws.rs.core.Response
handleValidationException(Throwable exception, Throwable catched)
Exception handling for validation exception.boolean
isExposeInternalErrorDetails()
protected void
registerToplevelSecurityException(Class<? extends Throwable> securityException)
protected void
registerToplevelSecurityExceptions()
This method registers thetop-level security exceptions
.protected void
registerToplevelSecurityExceptions(String className)
void
setExposeInternalErrorDetails(boolean exposeInternalErrorDetails)
void
setMapper(com.fasterxml.jackson.databind.ObjectMapper mapper)
javax.ws.rs.core.Response
toResponse(Throwable exception)
protected javax.ws.rs.core.Response
toResponse(Throwable exception, Throwable catched)
-
-
-
Method Detail
-
registerToplevelSecurityException
protected void registerToplevelSecurityException(Class<? extends Throwable> securityException)
Registers aClass
as a top-level securityexception
. 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 theClass
reflecting the security error.
-
registerToplevelSecurityExceptions
protected void registerToplevelSecurityExceptions()
This method registers thetop-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 interfacejavax.ws.rs.ext.ExceptionMapper<Throwable>
-
toResponse
protected javax.ws.rs.core.Response toResponse(Throwable exception, Throwable catched)
- Parameters:
exception
- the exception to handlecatched
- the original exception that was cached. Either same aserror
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 theResponse
for the given validation exception.- Parameters:
exception
- is the original validation exception.error
- is the wrapped exception or the same asexception
.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 handlecatched
- the original exception that was cached. Either same aserror
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 handlecatched
- the original exception that was cached. Either same aserror
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 handlecatched
- the original exception that was cached. Either same aserror
or a (child-)cause
of it.- Returns:
- the response build from the exception.
-
getExposedErrorDetails
protected String getExposedErrorDetails(Throwable error)
- Parameters:
error
- is theThrowable
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 theResponse
for the givenNlsRuntimeException
.- Parameters:
error
- the genericNlsRuntimeException
.- 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 HTTPResponse.Status
.error
- is the catched or wrappedNlsRuntimeException
.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 HTTPResponse.Status
.error
- is the catched or wrappedNlsRuntimeException
.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 HTTPResponse.Status
.error
- is the catched or wrappedNlsRuntimeException
.message
- is the JSON message attribute.code
- is theerror 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 HTTPResponse.Status
.message
- is the JSON message attribute.code
- is theerror code
.uuid
- theUUID
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 messagecode
- the code of the response messageuuid
- the uuid of the response messageerrorsMap
- 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
- theWebApplicationException
.- 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).
-
isExposeInternalErrorDetails
public boolean isExposeInternalErrorDetails()
- Returns:
- exposeInternalErrorDetails the value set by
setExposeInternalErrorDetails(boolean)
.
-
-