static String exceptionToString(Throwable ex) { if (ex == null) { return "null"; } StringWriter str = new StringWriter(); str.append("Exception: ").append(ex.getClass().getSimpleName()); str.append(" Message: ").append(ex.getMessage()); str.append(" Stacktrace: "); Throwable cause = ex.getCause(); if (cause != null) { str.append("\nCause: ").append(exceptionToString(cause)); } PrintWriter writer = new PrintWriter(str); try { ex.printStackTrace(writer); return str.getBuffer().toString(); } finally { try { str.close(); writer.close(); } catch (IOException e) {} } }
private boolean responseWasSuccessful(RpcCallExceptionDecoder decoder, ContentResponse response, int lastStatusCode) throws RpcCallException { if (shouldExposeErrorsToHttp(serviceProps)) { return lastStatusCode == 200 && response != null && response.getContent().length > 0; } else if (lastStatusCode != 0 && lastStatusCode != 200) { return false; } else if (response == null || response.getContent() == null) { return false; } RpcCallException exception = decoder.decodeException(response); return (exception == null); }
@Override public RpcCallException decodeException(ContentResponse response) throws RpcCallException { try { if (response != null) { JsonObject json = (JsonObject) new JsonParser().parse(response.getContentAsString()); JsonElement error = json.get("error"); if (error != null) { return RpcCallException.fromJson(error.toString()); } } } catch (Exception ex) { logger.warn("Caught exception decoding protobuf response exception", ex); throw new RpcCallException(RpcCallException.Category.InternalServerError, RpcCallExceptionDecoder.exceptionToString(ex)); } return null; }
lastException = decoder.decodeException(retval); if (lastException != null && !lastException.isRetriable()) { throw lastException;
@Test public void verifyExceptionToString() { Exception ex = new IllegalStateException("testing"); String result = RpcCallExceptionDecoder.exceptionToString(ex); assertThat(result).contains("Exception: IllegalStateException Message: testing " + "Stacktrace: java.lang.IllegalStateException: testing"); }
@Override public RpcCallException decodeException(ContentResponse response) throws RpcCallException { try { if (response != null) { byte[] data = response.getContent(); if (ArrayUtils.isEmpty(data)) { logger.warn("Unable to decode: empty response received"); return new RpcCallException(RpcCallException.Category.InternalServerError, "Empty response received"); } ProtobufRpcResponse pbResponse = new ProtobufRpcResponse(data); String error = pbResponse.getErrorMessage(); if (error != null) { return RpcCallException.fromJson(error); } } } catch (Exception ex) { logger.warn("Caught exception decoding protobuf response exception", ex); throw new RpcCallException(RpcCallException.Category.InternalServerError, RpcCallExceptionDecoder.exceptionToString(ex)); } return null; }