RpcExceptionMapper implements ExceptionMapper<RpcException> { @Override public Response toResponse(RpcException e) { if (e.getCause() instanceof ConstraintViolationException) { return handleConstraintViolationException((ConstraintViolationException) e.getCause()); } return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Internal server error: " + e.getMessage()).type(ContentType.TEXT_PLAIN_UTF_8).build(); }  @Override Response toResponse(RpcException e);  }
@Test public void testConstraintViolationException() { ConstraintViolationException violationException = mock(ConstraintViolationException.class); ConstraintViolation violation = mock(ConstraintViolation.class, Answers.RETURNS_DEEP_STUBS); given(violationException.getConstraintViolations()).willReturn(Sets.<ConstraintViolation<?>>newSet(violation)); RpcException rpcException = new RpcException("violation", violationException); Response response = exceptionMapper.toResponse(rpcException); assertThat(response, not(nullValue())); assertThat(response.getEntity(), instanceOf(ViolationReport.class)); }
@Test public void testNormalException() { RpcException rpcException = new RpcException(); Response response = exceptionMapper.toResponse(rpcException); assertThat(response, not(nullValue())); assertThat(response.getEntity(), instanceOf(String.class)); }