Search in sources :

Example 1 with JAXBUnmarshalException

use of org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException in project stdlib by petergeneric.

the class RemoteExceptionClientResponseFilter method filter.

@Override
public void filter(final ClientRequestContext requestContext, final ClientResponseContext responseContext) throws IOException {
    final int code = responseContext.getStatus();
    String operationId;
    if (Tracing.isVerbose()) {
        operationId = requestContext.getHeaderString(TracingConstants.HTTP_HEADER_CORRELATION_ID);
        if (operationId != null)
            Tracing.logOngoing(operationId, "HTTP:resp", () -> "" + code);
        else
            // can't find outgoing trace id
            operationId = Tracing.log("HTTP:resp:unexpected", () -> "" + code);
    } else {
        operationId = null;
    }
    if (code >= 200 && code <= 299)
        // Do not run if the return code is 2xx
        return;
    if (responseContext.getHeaders().containsKey(RestThrowableConstants.HEADER_RICH_EXCEPTION)) {
        try {
            final InputStream is = responseContext.getEntityStream();
            RestFailure failure;
            if (tryParseLegacyExceptionNamespace) {
                // If parsing the legacy namespace fails, throw the original parse error back to the client
                try {
                    // Mark the start of the stream so we can reset back to it if we need to process this as a legacy exception
                    is.mark(Integer.MAX_VALUE);
                    failure = parseResponse(is);
                } catch (JAXBUnmarshalException e) {
                    log.trace("Error parsing rich exception response, will fall back on parsing it as a legacy exception XML", e);
                    try {
                        failure = parseLegacyResponse(is);
                    } catch (Throwable legacyFailure) {
                        log.trace("Error parsing rich exception response as legacy rich exception XML!", legacyFailure);
                        // throw the original exception
                        throw e;
                    }
                }
            } else {
                failure = parseResponse(is);
            }
            if (Tracing.isVerbose() && failure != null && failure.exception != null) {
                final ExceptionInfo ei = failure.exception;
                Tracing.logOngoing(operationId, "HTTP:error", () -> ei.shortName + " " + ei.detail);
            }
            RestException exception = exceptionFactory.build(failure, responseContext);
            // Try to shorten the stack trace
            exception.fillInStackTrace();
            throw exception;
        } catch (ResponseProcessingException e) {
            throw e;
        } catch (Throwable e) {
            throw new ResponseProcessingException(null, "Error mapping exception from thrown from " + requestContext.getUri() + " to exception!", e);
        }
    }
}
Also used : JAXBUnmarshalException(org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException) RestFailure(com.peterphi.std.guice.restclient.jaxb.RestFailure) InputStream(java.io.InputStream) RestException(com.peterphi.std.guice.restclient.exception.RestException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) ExceptionInfo(com.peterphi.std.guice.restclient.jaxb.ExceptionInfo)

Example 2 with JAXBUnmarshalException

use of org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException in project candlepin by candlepin.

the class JAXBUnmarshalExceptionMapperTest method handleExceptionWithResponse.

@Test
public void handleExceptionWithResponse() {
    Response mockr = mock(Response.class);
    when(mockr.getStatus()).thenReturn(500);
    JAXBUnmarshalException nfe = new JAXBUnmarshalException("unacceptable", mockr);
    JAXBUnmarshalExceptionMapper nfem = injector.getInstance(JAXBUnmarshalExceptionMapper.class);
    Response r = nfem.toResponse(nfe);
    assertEquals(500, r.getStatus());
    verifyMessage(r, rtmsg("unacceptable"));
}
Also used : Response(javax.ws.rs.core.Response) JAXBUnmarshalException(org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException) Test(org.junit.Test)

Example 3 with JAXBUnmarshalException

use of org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException in project candlepin by candlepin.

the class JAXBUnmarshalExceptionMapperTest method handleExceptionWithoutResponse.

@Test
public void handleExceptionWithoutResponse() {
    JAXBUnmarshalException nfe = new JAXBUnmarshalException("unacceptable");
    JAXBUnmarshalExceptionMapper nfem = injector.getInstance(JAXBUnmarshalExceptionMapper.class);
    Response r = nfem.toResponse(nfe);
    assertEquals(400, r.getStatus());
    verifyMessage(r, rtmsg("unacceptable"));
}
Also used : Response(javax.ws.rs.core.Response) JAXBUnmarshalException(org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException) Test(org.junit.Test)

Aggregations

JAXBUnmarshalException (org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException)3 Response (javax.ws.rs.core.Response)2 Test (org.junit.Test)2 RestException (com.peterphi.std.guice.restclient.exception.RestException)1 ExceptionInfo (com.peterphi.std.guice.restclient.jaxb.ExceptionInfo)1 RestFailure (com.peterphi.std.guice.restclient.jaxb.RestFailure)1 InputStream (java.io.InputStream)1 ResponseProcessingException (javax.ws.rs.client.ResponseProcessingException)1