private void readRpcError(String data) { try { JsonRpcResponse json = JsonRpcResponse.fromString(data); this.errorMessage = json.getError().toString(); } catch (Exception ex) { logger.warn("Caught exception parsing response"); } }
@Test public void testJsonArrayError() { String input = "{\"error\":\"[{},{}]\",\"result\":{\"foo\":\"bar\"}}"; JsonRpcResponse response = JsonRpcResponse.fromString(input); assertThat(response.getStatusCode()).isEqualTo(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); }
@Test public void testJsonResponseWithSuccessCode() { String input = "{\"error\":\"\",\"result\":{\"foo\":\"bar\"}}"; JsonRpcResponse response = JsonRpcResponse.fromString(input); assertThat(response.getStatusCode()).isEqualTo(HttpServletResponse.SC_OK); }
@Test public void verifyParseResponse() { String input = "{\"error\":\"errror\",\"result\":{\"foo\":\"bar\"}}"; JsonRpcResponse response = JsonRpcResponse.fromString(input); assertThat(response.getError().getAsString()).isEqualTo("errror"); assertThat(response.getResult().toString()).isEqualTo("{\"foo\":\"bar\"}"); assertThat(response.getStatusCode()).isEqualTo(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); }
@Test public void testJsonResponseWithRpcException() { String jsonRpcException = "{\"category\":400,\"message\":\"You fool!\"," + "\"source\":\"com.sixt.service.foobar\",\"code\":\"SERVICE_PROTOBUF_ENUM\"," + "\"data\":\"my data\",\"retriable\":true}"; String input = "{\"error\":" + jsonRpcException + ",\"result\":{\"foo\":\"bar\"}}"; JsonRpcResponse response = JsonRpcResponse.fromString(input); assertThat(response.getError()).isNotNull(); assertThat(response.getStatusCode()).isEqualTo(HttpServletResponse.SC_BAD_REQUEST); }
public String callSynchronous(JsonArray params, OrangeContext orangeContext) throws RpcCallException { HttpClientWrapper clientWrapper = loadBalancer.getHttpClientWrapper(); HttpRequestWrapper balancedPost = clientWrapper.createHttpPost(this); //set custom headers if (orangeContext != null) { orangeContext.getProperties().forEach(balancedPost::setHeader); } balancedPost.setHeader("Content-type", TYPE_JSON); //TODO: fix: Temporary workaround below until go services are more http compliant balancedPost.setHeader("Connection", "close"); JsonRpcRequest jsonRequest = new JsonRpcRequest(null, methodName, params); String json = jsonRequest.toString(); balancedPost.setContentProvider(new StringContentProvider(json)); logger.debug("Sending request of size {}", json.length()); ContentResponse rpcResponse = clientWrapper.execute(balancedPost, new JsonRpcCallExceptionDecoder(), orangeContext); String rawResponse = rpcResponse.getContentAsString(); logger.debug("Json response from the service: {}", rawResponse); return JsonRpcResponse.fromString(rawResponse).getResult().getAsString(); }