use of com.liferay.apio.architect.message.json.ErrorMessageMapper 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());
}
use of com.liferay.apio.architect.message.json.ErrorMessageMapper in project com-liferay-apio-architect by liferay.
the class ErrorWriterTest method testWriteErrorCreatesCorrectJsonObject.
@Test
public void testWriteErrorCreatesCorrectJsonObject() {
ErrorMessageMapper errorMessageMapper = new TestErrorMessageMapper();
APIError apiError = new APIError(new IllegalArgumentException(), "A title", "A description", "A type", 404);
HttpHeaders httpHeaders = Mockito.mock(HttpHeaders.class);
Mockito.when(httpHeaders.getHeaderString("start")).thenReturn("true");
Mockito.when(httpHeaders.getHeaderString("end")).thenReturn("true");
String error = ErrorWriter.writeError(errorMessageMapper, apiError, httpHeaders);
Conditions.Builder builder = new Conditions.Builder();
Conditions conditions = builder.where("description", is(aJsonString(equalTo("A description")))).where("title", is(aJsonString(equalTo("A title")))).where("type", is(aJsonString(equalTo("A type")))).where("status", is(aJsonInt(equalTo(404)))).where("start", is(aJsonBoolean(true))).where("end", is(aJsonBoolean(true))).withStrictModeDeactivated().build();
assertThat(error, is(aJsonObjectStringWith(conditions)));
}
Aggregations