private static ResponseError fallbackResponseError(String header, Throwable throwable) { LOG.log(Level.SEVERE, header + ": " + throwable.getMessage(), throwable); ResponseError error = new ResponseError(); error.setMessage(header + "."); error.setCode(ResponseErrorCode.InternalError); ByteArrayOutputStream stackTrace = new ByteArrayOutputStream(); PrintWriter stackTraceWriter = new PrintWriter(stackTrace); throwable.printStackTrace(stackTraceWriter); stackTraceWriter.flush(); error.setData(stackTrace.toString()); return error; }
@Override public String getMessage() { return responseError.getMessage(); }
@Override public boolean equals(final Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; if (!super.equals(obj)) return false; ResponseMessage other = (ResponseMessage) obj; if (this.result == null) { if (other.result != null) return false; } else if (!this.result.equals(other.result)) return false; if (this.error == null) { if (other.error != null) return false; } else if (!this.error.equals(other.error)) return false; return true; }
it.setJsonrpc("2.0"); it.setId("12"); ResponseError _responseError = new ResponseError(); final Procedure1<ResponseError> _function_1 = (ResponseError it_1) -> { it_1.setCode(ResponseErrorCode.InvalidRequest); it_1.setMessage("Could not parse request."); }; ResponseError _doubleArrow = ObjectExtensions.<ResponseError>operator_doubleArrow(_responseError, _function_1);
protected IWorkspaceConfig getWorkspaceConfig() { if ((this._workspaceConfig == null)) { final ResponseError error = new ResponseError(ResponseErrorCode.serverNotInitialized, "Workspace has not been initialized yet.", null); throw new ResponseErrorException(error); } return this._workspaceConfig; }
out.name("success"); out.value(false); String errorMessage = error.getMessage(); out.name("message"); if (errorMessage == null) gson.toJson(errorMessage, errorMessage.getClass(), out); Object errorData = error.getData(); if (errorData != null) { out.name("body");
assertEquals("Internal error.", response.getError().getMessage()); assertEquals(ResponseErrorCode.InternalError.getValue(), response.getError().getCode()); String exception = (String) response.getError().getData(); String expected = "java.lang.RuntimeException: BAAZ\n\tat org.eclipse.lsp4j.jsonrpc.test.RemoteEndpointTest"; assertEquals(expected, exception.substring(0, expected.length()));
@Test public void testCancellation() { TestEndpoint endp = new TestEndpoint(); TestMessageConsumer consumer = new TestMessageConsumer(); RemoteEndpoint endpoint = new DebugRemoteEndpoint(consumer, endp); endpoint.consume(new RequestMessage() {{ setId("1"); setMethod("foo"); setParams("myparam"); }}); Entry<RequestMessage, CompletableFuture<Object>> entry = endp.requests.entrySet().iterator().next(); entry.getValue().cancel(true); ResponseMessage message = (ResponseMessage) consumer.messages.get(0); assertNotNull(message); ResponseError error = message.getError(); assertNotNull(error); assertEquals(error.getCode(), ResponseErrorCode.RequestCancelled.getValue()); assertEquals(error.getMessage(), "The request (id: 1, method: 'foo') has been cancelled"); }
public void setCode(ResponseErrorCode code) { this.setCode(code.getValue()); }
@Test public void testResponseErrorData() { MessageJsonHandler handler = new MessageJsonHandler(Collections.emptyMap()); ResponseMessage message = (ResponseMessage) handler.parseMessage("{\"jsonrpc\":\"2.0\"," + "\"id\":\"2\",\n" + "\"error\": { \"code\": -32001, \"message\": \"foo\",\n" + " \"data\": { \"uri\": \"file:/foo\", \"version\": 5, \"list\": [\"a\", \"b\", \"c\"] }\n" + " }\n" + "}"); ResponseError error = message.getError(); Assert.assertTrue("Expected a JsonObject in error.data", error.getData() instanceof JsonObject); JsonObject data = (JsonObject) error.getData(); Assert.assertEquals("file:/foo", data.get("uri").getAsString()); Assert.assertEquals(5, data.get("version").getAsInt()); JsonArray list = data.get("list").getAsJsonArray(); Assert.assertEquals("a", list.get(0).getAsString()); Assert.assertEquals("b", list.get(1).getAsString()); Assert.assertEquals("c", list.get(2).getAsString()); }
@Override public int hashCode() { final int prime = 31; int result = super.hashCode(); result = prime * result + ((this.result == null) ? 0 : this.result.hashCode()); result = prime * result + ((this.error == null) ? 0 : this.error.hashCode()); return result; }
ResponseMessage response = (ResponseMessage) consumer.messages.get(0); assertNotNull("Check response has error", response.getError()); assertEquals(ResponseErrorCode.InternalError.getValue(), response.getError().getCode()); } finally { logMessages.unregister();
it.setJsonrpc("2.0"); it.setId("12"); ResponseError _responseError = new ResponseError(); final Procedure1<ResponseError> _function_2 = (ResponseError it_1) -> { it_1.setCode(ResponseErrorCode.InvalidRequest); it_1.setMessage("Could not parse request."); }; ResponseError _doubleArrow = ObjectExtensions.<ResponseError>operator_doubleArrow(_responseError, _function_2);
@Override public void handleException(Throwable ex) { IStatus status = new Status(IStatus.ERROR, IConstants.PLUGIN_ID, IStatus.OK, "Error in calling delegate command handler", ex); JavaLanguageServerPlugin.log(status); if (ex instanceof ResponseErrorException) { throw (ResponseErrorException) ex; } throw new ResponseErrorException(new ResponseError(ResponseErrorCode.UnknownErrorCode, ex.getMessage(), ex)); } });
@Test public void testErrorResponse_AllOrders() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Location>() {}.getType(), new TypeToken<Void>() { }.getType())); MessageJsonHandler handler = new MessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); String[] properties = new String[] { "\"jsonrpc\":\"2.0\"", "\"id\":2", "\"message\": \"failed\"", "\"error\": {\"code\": 123456, \"message\": \"failed\", \"data\": {\"uri\": \"failed\"}}" }; testAllPermutations(properties, json -> { ResponseMessage message = (ResponseMessage) handler.parseMessage(json); Assert.assertEquals("failed", message.getError().getMessage()); Object data = message.getError().getData(); JsonObject expected = new JsonObject(); expected.addProperty("uri", "failed"); Assert.assertEquals(expected, data); Assert.assertNull(message.getResult()); }); }
@Test public void testCancellation() { TestEndpoint endp = new TestEndpoint(); TestMessageConsumer consumer = new TestMessageConsumer(); RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp); endpoint.consume(init(new RequestMessage(), it -> { it.setId("1"); it.setMethod("foo"); it.setParams("myparam"); })); Entry<RequestMessage, CompletableFuture<Object>> entry = endp.requests.entrySet().iterator().next(); entry.getValue().cancel(true); ResponseMessage message = (ResponseMessage) consumer.messages.get(0); assertNotNull(message); ResponseError error = message.getError(); assertNotNull(error); assertEquals(error.getCode(), ResponseErrorCode.RequestCancelled.getValue()); assertEquals(error.getMessage(), "The request (id: 1, method: 'foo') has been cancelled"); }
public void setCode(ResponseErrorCode code) { this.setCode(code.getValue()); }
@Override public int hashCode() { final int prime = 31; int result = super.hashCode(); result = prime * result + ((this.result == null) ? 0 : this.result.hashCode()); result = prime * result + ((this.error == null) ? 0 : this.error.hashCode()); return result; }
@Test public void testExceptionHandlerMisbehaving1() { LogMessageAccumulator logMessages = new LogMessageAccumulator(); try { // Don't show the exception in the test execution log logMessages.registerTo(RemoteEndpoint.class); TestEndpoint endp = new TestEndpoint() { @Override public CompletableFuture<Object> request(String method, Object parameter) { throw new RuntimeException("BAAZ"); } }; TestMessageConsumer consumer = new TestMessageConsumer(); // Misbehaving exception handler that returns null RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp, (e) -> null); endpoint.consume(init(new RequestMessage(), it -> { it.setId("1"); it.setMethod("foo"); it.setParams("myparam"); })); assertEquals("Check some response received", 1, consumer.messages.size()); ResponseMessage response = (ResponseMessage) consumer.messages.get(0); assertEquals(ResponseErrorCode.InternalError.getValue(), response.getError().getCode()); } finally { logMessages.unregister(); } }
private static ResponseError fallbackResponseError(String header, Throwable throwable) { LOG.log(Level.SEVERE, header + ": " + throwable.getMessage(), throwable); ResponseError error = new ResponseError(); error.setMessage(header + "."); error.setCode(ResponseErrorCode.InternalError); ByteArrayOutputStream stackTrace = new ByteArrayOutputStream(); PrintWriter stackTraceWriter = new PrintWriter(stackTrace); throwable.printStackTrace(stackTraceWriter); stackTraceWriter.flush(); error.setData(stackTrace.toString()); return error; }