use of org.osgi.service.component.annotations.ReferenceCardinality.OPTIONAL in project com-liferay-apio-architect by liferay.
the class ErrorUtil method getErrorResponse.
/**
* Transforms an exception into a {@code Response}.
*
* @param exception the exception
* @param request the current request
* @param httpHeaders the current HTTP headers
* @return the response
*/
public Response getErrorResponse(Exception exception, Request request, HttpHeaders httpHeaders) {
Optional<APIError> apiErrorOptional = _exceptionConverterManager.convert(exception);
if (!apiErrorOptional.isPresent()) {
Class<? extends Exception> exceptionClass = exception.getClass();
if (_apioLogger != null) {
_apioLogger.warning("No exception converter found for " + exceptionClass);
}
if (exceptionClass.isAssignableFrom(WebApplicationException.class)) {
WebApplicationException webApplicationException = (WebApplicationException) exception;
return webApplicationException.getResponse();
}
return Response.serverError().build();
}
APIError apiError = apiErrorOptional.get();
if (_apioLogger != null) {
_apioLogger.error(apiError);
}
int statusCode = apiError.getStatusCode();
Optional<ErrorMessageMapper> errorMessageMapperOptional = _errorMessageMapperManager.getErrorMessageMapperOptional(request);
return errorMessageMapperOptional.map(errorMessageMapper -> {
String result = ErrorWriter.writeError(errorMessageMapper, apiError, httpHeaders);
return Response.status(statusCode).type(errorMessageMapper.getMediaType()).entity(result).build();
}).orElseGet(() -> Response.status(statusCode).build());
}
Aggregations