public JsonObject toJson() { JsonObject obj = new JsonObject(); obj.addProperty(CATEGORY, category.getHttpStatus()); obj.addProperty(MESSAGE, message); obj.addProperty(SOURCE, source); obj.addProperty(CODE, errorCode); obj.addProperty(DATA, data); obj.addProperty(RETRIABLE, retriable); return obj; }
public static JsonRpcResponse fromString(String rawResponse) { JsonParser parser = new JsonParser(); JsonObject response = parser.parse(rawResponse).getAsJsonObject(); JsonElement id = response.get("id"); JsonElement errorElement = response.get("error"); int responseStatus = HttpServletResponse.SC_OK; String error; if (! (errorElement instanceof JsonNull)) { if (errorElement instanceof JsonObject) { error = errorElement.toString(); // try parsing it into RpcCallException to get the HttpStatus from there RpcCallException rpcEx = RpcCallException.fromJson(error); if (rpcEx != null) { responseStatus = rpcEx.getCategory().getHttpStatus(); JsonElement resultElement = response.get("result"); return new JsonRpcResponse(id, resultElement == null ? JsonNull.INSTANCE : resultElement, errorElement, responseStatus); } } error = errorElement.getAsString(); if (StringUtils.isNotBlank(error)) { responseStatus = HttpServletResponse.SC_INTERNAL_SERVER_ERROR; } } JsonElement resultElement = response.get("result"); return new JsonRpcResponse(id, resultElement == null ? JsonNull.INSTANCE : resultElement, errorElement, responseStatus); }
@Override public Request newRequest(String uri) { Request retval = mock(Request.class); try { if (requestsTimeout || (isFirstRequestTimeout && requests.isEmpty())) { when(retval.send()).thenThrow(new TimeoutException()); } else { when(retval.send()).thenReturn(httpResponse); } if (requestsFail && featureFlag.equals("true")) { when(httpResponse.getStatus()).thenReturn(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } else if (responseException != null) { when(httpResponse.getStatus()).thenReturn(responseException.getCategory().getHttpStatus()); } else { when(httpResponse.getStatus()).thenReturn(HttpServletResponse.SC_OK); } } catch (Exception e) { e.printStackTrace(); } when(retval.method(anyString())).thenReturn(retval); when(retval.content(any(ContentProvider.class))).thenReturn(retval); when(retval.timeout(anyLong(), any(TimeUnit.class))).thenReturn(retval); requests.add(uri); return retval; }
lastStatusCode = retval.getStatus(); } catch (TimeoutException timeout) { lastStatusCode = RpcCallException.Category.RequestTimedOut.getHttpStatus(); lastException = new RpcCallException(RpcCallException.Category.RequestTimedOut, "Http-client timeout"); lastStatusCode = RpcCallException.Category.InternalServerError.getHttpStatus(); logger.debug(getRemoteMethod(), "Caught exception executing request", ex); if (lastStatusCode != RpcCallException.Category.RequestTimedOut.getHttpStatus()) { lastException = decoder.decodeException(retval); if (lastException != null && !lastException.isRetriable()) {
context.getRpcOriginMethod()); } catch (RpcCallException rpcEx) { sendErrorResponse(resp, rpcRequest, rpcEx.toString(), rpcEx.getCategory().getHttpStatus()); if (span != null) { Tags.ERROR.set(span, true);
@SuppressWarnings("unchecked") private JsonRpcResponse dispatchJsonRpcRequest(JsonRpcRequest rpcRequest, OrangeContext cxt) { JsonRpcResponse jsonResponse = new JsonRpcResponse(rpcRequest.getId(), JsonNull.INSTANCE, JsonNull.INSTANCE, HttpServletResponse.SC_OK); try { ServiceMethodHandler handler = handlers.getMethodHandler(rpcRequest.getMethod()); Message innerRequest = convertJsonToProtobuf(handler, rpcRequest); JsonElement idElement = rpcRequest.getId(); if (idElement == null) { jsonResponse.setId(new JsonPrimitive(-1)); } Message innerResponse = invokeHandlerChain(rpcRequest.getMethod(), handler, innerRequest, cxt); jsonResponse.setResult(ProtobufUtil.protobufToJson(innerResponse)); } catch (RpcCallException rpcEx) { logger.debug("Error processing request", rpcEx); jsonResponse.setError(rpcEx.toJson()); jsonResponse.setStatusCode(rpcEx.getCategory().getHttpStatus()); } catch (Exception ex) { logger.warn("Error processing request", ex); if (ex.getMessage() != null) { jsonResponse.setError(new JsonPrimitive(ex.getMessage())); } jsonResponse.setStatusCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } return jsonResponse; }
public void setResponseException(RpcCallException responseException) { this.responseException = responseException; if (featureFlag.equals("true")) { when(httpResponse.getStatus()).thenReturn(responseException.getCategory().getHttpStatus()); } String response = "{\"error\":" + responseException.toJson() + ",\"result\":{}}"; when(httpResponse.getContentAsString()).thenReturn(response); RpcEnvelope.Response pbResponse = RpcEnvelope.Response.newBuilder(). setError(responseException.toJson().toString()).build(); byte[] responseArray = pbResponse.toByteArray(); byte[] headerLength = Ints.toByteArray(responseArray.length); byte[] bodyLength = Ints.toByteArray(0); byte[] overallPayload = concatAll(headerLength, responseArray, bodyLength); when(httpResponse.getContent()).thenReturn(overallPayload); }