Search in sources :

Example 1 with ExceptionInfo

use of com.peterphi.std.guice.restclient.jaxb.ExceptionInfo 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 ExceptionInfo

use of com.peterphi.std.guice.restclient.jaxb.ExceptionInfo in project stdlib by petergeneric.

the class RestFailureMarshaller method renderThrowable.

private ExceptionInfo renderThrowable(Throwable e) {
    final ExceptionInfo info = new ExceptionInfo();
    final Class<?> clazz = e.getClass();
    info.shortName = clazz.getSimpleName();
    info.className = clazz.getName();
    info.detail = e.getMessage();
    // Optionally fill in the stack trace
    if (stackTraces) {
        final StringWriter sw = new StringWriter(512);
        final PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        pw.close();
        info.stackTrace = sw.toString();
    }
    // Recursively fill in the cause
    if (e.getCause() != null)
        info.causedBy = renderThrowable(e.getCause());
    return info;
}
Also used : StringWriter(java.io.StringWriter) ExceptionInfo(com.peterphi.std.guice.restclient.jaxb.ExceptionInfo) PrintWriter(java.io.PrintWriter)

Aggregations

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