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 ofExceptionMapperthat acts as generic exception facade for REST services. Itmapsexceptionsto an according HTTP status code and JSON result as defined by devonfw REST error specification.- Since:
- 2.0.0
-
-
Field Summary
Fields Modifier and Type Field Description private booleanexposeInternalErrorDetailsprivate static org.slf4j.LoggerLOGLogger instance.private com.fasterxml.jackson.databind.ObjectMappermapperprivate Class<? extends Throwable>rollbackExceptionprivate List<Class<? extends Throwable>>securityExceptionsprivate Class<? extends Throwable>transactionSystemException
-
Constructor Summary
Constructors Constructor Description RestServiceExceptionFacade()The constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected StringcreateJsonErrorResponseMessage(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.ResponsecreateResponse(Throwable exception, ValidationErrorUserException error, Map<String,List<String>> errorsMap)Creates theResponsefor the given validation exception.protected javax.ws.rs.core.ResponsecreateResponse(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.ResponsecreateResponse(javax.ws.rs.core.Response.Status status, 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.ResponsecreateResponse(javax.ws.rs.core.Response.Status status, 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.ResponsecreateResponse(javax.ws.rs.core.Response.Status status, NlsRuntimeException error, Map<String,List<String>> errorsMap)Create a response message as a JSON-String from the given parts.protected javax.ws.rs.core.ResponsecreateResponse(javax.ws.rs.WebApplicationException exception)Add a response message to an existing response.protected javax.ws.rs.core.ResponsecreateResponse(NlsRuntimeException error)Create theResponsefor the givenNlsRuntimeException.protected StringgetExposedErrorDetails(Throwable error)com.fasterxml.jackson.databind.ObjectMappergetMapper()private ThrowablegetRollbackCause(Throwable exception)protected javax.ws.rs.core.ResponsehandleGenericError(Throwable exception, Throwable catched)Exception handling for generic exception (fallback).protected javax.ws.rs.core.ResponsehandleSecurityError(Throwable exception, Throwable catched)Exception handling for security exception.protected javax.ws.rs.core.ResponsehandleValidationException(Throwable exception, Throwable catched)Exception handling for validation exception.booleanisExposeInternalErrorDetails()private Class<? extends Throwable>loadException(String className)protected voidregisterToplevelSecurityException(Class<? extends Throwable> securityException)protected voidregisterToplevelSecurityExceptions()This method registers thetop-level security exceptions.protected voidregisterToplevelSecurityExceptions(String className)voidsetExposeInternalErrorDetails(boolean exposeInternalErrorDetails)voidsetMapper(com.fasterxml.jackson.databind.ObjectMapper mapper)javax.ws.rs.core.ResponsetoResponse(Throwable exception)protected javax.ws.rs.core.ResponsetoResponse(Throwable exception, Throwable catched)private NlsRuntimeExceptionunwrapNlsUserError(Throwable exception)Unwraps potential NLS user error from a wrapper exception such asJsonMappingExceptionorPersistenceException.
-
-
-
Method Detail
-
registerToplevelSecurityException
protected void registerToplevelSecurityException(Class<? extends Throwable> securityException)
Registers aClassas 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 theClassreflecting 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:
toResponsein interfacejavax.ws.rs.ext.ExceptionMapper<Throwable>
-
unwrapNlsUserError
private NlsRuntimeException unwrapNlsUserError(Throwable exception)
Unwraps potential NLS user error from a wrapper exception such asJsonMappingExceptionorPersistenceException.- Parameters:
exception- the exception to unwrap.- Returns:
- the unwrapped
NlsRuntimeExceptionexception ornullif nouse error.
-
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 aserroror a (child-)causeof it.- Returns:
- the response build from the exception.
- See Also:
toResponse(Throwable)
-
createResponse
protected javax.ws.rs.core.Response createResponse(Throwable exception, ValidationErrorUserException error, Map<String,List<String>> errorsMap)
Creates theResponsefor 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 aserroror a (child-)causeof 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 aserroror a (child-)causeof 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 aserroror a (child-)causeof it.- Returns:
- the response build from the exception.
-
getExposedErrorDetails
protected String getExposedErrorDetails(Throwable error)
- Parameters:
error- is theThrowableto extract message details from.- Returns:
- the exposed message(s).
-
createResponse
protected javax.ws.rs.core.Response createResponse(NlsRuntimeException error)
Create theResponsefor 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, 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, 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, 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- theUUIDof 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
ObjectMapperfor 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- -trueif internal exception details shall be exposed to clients (useful for debugging and testing),falseif 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).
-
-