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);
}
}
}
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;
}
Aggregations