Search in sources :

Example 1 with APIError

use of com.liferay.apio.architect.error.APIError in project com-liferay-apio-architect by liferay.

the class JSONLDErrorMessageMapperTest method testJSONLDErrorMessageMapper.

@Test
public void testJSONLDErrorMessageMapper() {
    APIError apiError = new APIError(new IllegalArgumentException(), "A title", "A description", "A type", 404);
    HttpHeaders httpHeaders = Mockito.mock(HttpHeaders.class);
    String error = ErrorWriter.writeError(_errorMessageMapper, apiError, httpHeaders);
    Conditions.Builder builder = new Conditions.Builder();
    Conditions conditions = builder.where("@type", is(aJsonString(equalTo("A type")))).where("description", is(aJsonString(equalTo("A description")))).where("statusCode", is(aJsonInt(equalTo(404)))).where("title", is(aJsonString(equalTo("A title")))).build();
    assertThat(error, is(aJsonObjectStringWith(conditions)));
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) JsonMatchers.aJsonString(com.liferay.apio.architect.test.util.json.JsonMatchers.aJsonString) APIError(com.liferay.apio.architect.error.APIError) Conditions(com.liferay.apio.architect.test.util.json.Conditions) Test(org.junit.Test)

Example 2 with APIError

use of com.liferay.apio.architect.error.APIError in project com-liferay-apio-architect by liferay.

the class ClientErrorExceptionConverter method convert.

@Override
public APIError convert(ClientErrorException exception) {
    Response response = exception.getResponse();
    int status = response.getStatus();
    return new APIError(exception, "General server error", "client-error", status);
}
Also used : Response(javax.ws.rs.core.Response) APIError(com.liferay.apio.architect.error.APIError)

Example 3 with APIError

use of com.liferay.apio.architect.error.APIError 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());
}
Also used : ErrorWriter(com.liferay.apio.architect.writer.ErrorWriter) ErrorMessageMapper(com.liferay.apio.architect.message.json.ErrorMessageMapper) ApioLogger(com.liferay.apio.architect.logger.ApioLogger) ErrorMessageMapperManager(com.liferay.apio.architect.wiring.osgi.manager.message.json.ErrorMessageMapperManager) Component(org.osgi.service.component.annotations.Component) APIError(com.liferay.apio.architect.error.APIError) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) OPTIONAL(org.osgi.service.component.annotations.ReferenceCardinality.OPTIONAL) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) GREEDY(org.osgi.service.component.annotations.ReferencePolicyOption.GREEDY) Request(javax.ws.rs.core.Request) Reference(org.osgi.service.component.annotations.Reference) ExceptionConverterManager(com.liferay.apio.architect.wiring.osgi.manager.ExceptionConverterManager) WebApplicationException(javax.ws.rs.WebApplicationException) ErrorMessageMapper(com.liferay.apio.architect.message.json.ErrorMessageMapper) APIError(com.liferay.apio.architect.error.APIError)

Example 4 with APIError

use of com.liferay.apio.architect.error.APIError in project com-liferay-apio-architect by liferay.

the class ProblemJSONErrorMessageMapperTest method testJSONLDErrorMessageMapper.

@Test
public void testJSONLDErrorMessageMapper() {
    APIError apiError = new APIError(new IllegalArgumentException(), "A title", "A description", "A type", 404);
    HttpHeaders httpHeaders = Mockito.mock(HttpHeaders.class);
    String error = ErrorWriter.writeError(_errorMessageMapper, apiError, httpHeaders);
    Conditions.Builder builder = new Conditions.Builder();
    Conditions conditions = builder.where("detail", is(aJsonString(equalTo("A description")))).where("status", is(aJsonInt(equalTo(404)))).where("title", is(aJsonString(equalTo("A title")))).where("type", is(aJsonString(equalTo("A type")))).build();
    assertThat(error, is(aJsonObjectStringWith(conditions)));
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) JsonMatchers.aJsonString(com.liferay.apio.architect.test.util.json.JsonMatchers.aJsonString) APIError(com.liferay.apio.architect.error.APIError) Conditions(com.liferay.apio.architect.test.util.json.Conditions) Test(org.junit.Test)

Example 5 with APIError

use of com.liferay.apio.architect.error.APIError 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)));
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) JSONObjectBuilder(com.liferay.apio.architect.message.json.JSONObjectBuilder) ErrorMessageMapper(com.liferay.apio.architect.message.json.ErrorMessageMapper) JsonMatchers.aJsonString(com.liferay.apio.architect.test.util.json.JsonMatchers.aJsonString) APIError(com.liferay.apio.architect.error.APIError) Conditions(com.liferay.apio.architect.test.util.json.Conditions) Test(org.junit.Test)

Aggregations

APIError (com.liferay.apio.architect.error.APIError)5 HttpHeaders (javax.ws.rs.core.HttpHeaders)4 Conditions (com.liferay.apio.architect.test.util.json.Conditions)3 JsonMatchers.aJsonString (com.liferay.apio.architect.test.util.json.JsonMatchers.aJsonString)3 Test (org.junit.Test)3 ErrorMessageMapper (com.liferay.apio.architect.message.json.ErrorMessageMapper)2 Response (javax.ws.rs.core.Response)2 ApioLogger (com.liferay.apio.architect.logger.ApioLogger)1 JSONObjectBuilder (com.liferay.apio.architect.message.json.JSONObjectBuilder)1 ExceptionConverterManager (com.liferay.apio.architect.wiring.osgi.manager.ExceptionConverterManager)1 ErrorMessageMapperManager (com.liferay.apio.architect.wiring.osgi.manager.message.json.ErrorMessageMapperManager)1 ErrorWriter (com.liferay.apio.architect.writer.ErrorWriter)1 Optional (java.util.Optional)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Request (javax.ws.rs.core.Request)1 Component (org.osgi.service.component.annotations.Component)1 Reference (org.osgi.service.component.annotations.Reference)1 OPTIONAL (org.osgi.service.component.annotations.ReferenceCardinality.OPTIONAL)1 GREEDY (org.osgi.service.component.annotations.ReferencePolicyOption.GREEDY)1