public static void dealWithFailure(Throwable t) { if (t instanceof ServiceException) { ServiceException exc = (ServiceException) t; if (exc.failureCode() == BAD_NAME_ERROR) { System.out.println("Failed to process the document: The name in the document is bad. " + "The name provided is: " + exc.getDebugInfo().getString("name")); } else if (exc.failureCode() == NO_NAME_ERROR) { System.out.println("Failed to process the document: No name was found"); } } else { System.out.println("Unexpected error: " + t); } } }
@Test public void testWithFailingResult() { AtomicReference<Throwable> result = new AtomicReference<>(); Service service = Service.createProxy(consumerNode.get(), "my.service"); service.methodWthFailingResult("Fail", ar -> { assertThat(ar.cause() instanceof ServiceException).isTrue(); assertThat(((ServiceException)ar.cause()).failureCode()).isEqualTo(30); assertThat(((ServiceException)ar.cause()).getDebugInfo()).isEqualTo(new JsonObject().put("test", "val")); result.set(ar.cause()); }); Awaitility.await().atMost(10, TimeUnit.SECONDS).until(() -> result.get() != null); }
@Test public void testErrorHandling() { proxy.failingCall("Fail", handler -> { assertTrue(handler.cause() instanceof ServiceException); assertEquals("Call has failed", handler.cause().getMessage()); assertEquals(25, ((ServiceException)handler.cause()).failureCode()); assertEquals(new JsonObject().put("test", "val"), ((ServiceException)handler.cause()).getDebugInfo()); testComplete(); }); await(); }
@Override public void encodeToWire(Buffer buffer, ServiceException body) { buffer.appendInt(body.failureCode()); if (body.getMessage() == null) { buffer.appendByte((byte)0); } else { buffer.appendByte((byte)1); byte[] encoded = body.getMessage().getBytes(CharsetUtil.UTF_8); buffer.appendInt(encoded.length); buffer.appendBytes(encoded); } body.getDebugInfo().writeToBuffer(buffer); }