@Test public void doesNotHandleNonJsonMediaTypes() { assertThat(decode(MediaType.TEXT_PLAIN, STATUS_500, SERIALIZED_EXCEPTION)).isEmpty(); assertThat(decode(MediaType.TEXT_HTML, STATUS_500, SERIALIZED_EXCEPTION)).isEmpty(); assertThat(decode(MediaType.MULTIPART_FORM_DATA, STATUS_500, SERIALIZED_EXCEPTION)).isEmpty(); }
@Test public void testSpecificException() { RemoteException exception = encodeAndDecode(new IllegalArgumentException("msg")).get(); assertThat(exception).isInstanceOf(RemoteException.class); assertThat(exception.getMessage()).startsWith("RemoteException: java.lang.IllegalArgumentException (msg)"); }
private static Optional<RemoteException> decode(String contentType, int status, @CheckForNull String body) { return handler.handle(response(status, contentType, body)); }
@Test public void handlesWebApplicationExceptions() { testEncodingAndDecodingWebException(ClientErrorException.class, Response.Status.NOT_ACCEPTABLE); testEncodingAndDecodingWebException(ServerErrorException.class, javax.ws.rs.core.Response.Status.BAD_GATEWAY); testEncodingAndDecodingWebException(WebApplicationException.class, javax.ws.rs.core.Response.Status.NOT_MODIFIED); }
@Test public void doesNotHandleUnparseableBody() { assertThat(decode(MediaType.APPLICATION_JSON, STATUS_500, "not json")).isEmpty(); }
private static void testEncodingAndDecodingWebException(Class<? extends WebApplicationException> exceptionClass, Response.Status status) { WebApplicationException exceptionToProcess; try { exceptionToProcess = exceptionClass.getConstructor(String.class, Response.Status.class).newInstance(message, status); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { throw new RuntimeException(e); } RemoteException exception = encodeAndDecode(exceptionToProcess).get(); assertThat(exception.getCause()).isNull(); assertThat(exception.getStatus()).isEqualTo(status.getStatusCode()); assertThat(exception.getError().errorCode()).isEqualTo(exceptionClass.getName()); assertThat(exception.getMessage()) .startsWith("RemoteException: " + exceptionClass.getName() + " (" + message + ")"); }
@Test public void doesNotHandleNullBody() { assertThat(decode(MediaType.APPLICATION_JSON, STATUS_500, null)).isEmpty(); }
@Test public void handlesNotAuthorizedException() throws Exception { NotAuthorizedException originalException = new NotAuthorizedException(message, javax.ws.rs.core.Response.Status.UNAUTHORIZED); RemoteException exception = encodeAndDecode(originalException).get(); assertThat(exception.getCause()).isNull(); assertThat(exception.getStatus()).isEqualTo(javax.ws.rs.core.Response.Status.UNAUTHORIZED.getStatusCode()); assertThat(exception.getError().errorCode()).isEqualTo(NotAuthorizedException.class.getName()); assertThat(exception.getError().errorName()).isEqualTo(message); assertThat(exception.getMessage()) .isEqualTo("RemoteException: javax.ws.rs.NotAuthorizedException (" + message + ") with instance ID " + exception.getError().errorInstanceId()); }
private static Optional<RemoteException> encodeAndDecode(Exception exception) { Preconditions.checkArgument(!(exception instanceof ServiceException), "Use SerializableError#forException"); Object error = SerializableError.builder() .errorCode(exception.getClass().getName()) .errorName(exception.getMessage()) .build(); String json; try { json = SERVER_MAPPER.writeValueAsString(error); } catch (JsonProcessingException e) { throw new RuntimeException(e); } int status = (exception instanceof WebApplicationException) ? ((WebApplicationException) exception).getResponse().getStatus() : 400; return decode(MediaType.APPLICATION_JSON, status, json); }
@Test public void extractsRemoteExceptionForAllErrorCodes() throws Exception { for (int code : ImmutableList.of(300, 400, 404, 500)) { RemoteException exception = decode(MediaType.APPLICATION_JSON, code, SERIALIZED_EXCEPTION).get(); assertThat(exception.getCause()).isNull(); assertThat(exception.getStatus()).isEqualTo(code); assertThat(exception.getError().errorCode()).isEqualTo(ErrorType.FAILED_PRECONDITION.code().name()); assertThat(exception.getError().errorName()).isEqualTo(ErrorType.FAILED_PRECONDITION.name()); assertThat(exception.getMessage()).isEqualTo( "RemoteException: " + ErrorType.FAILED_PRECONDITION.code().name() + " (" + ErrorType.FAILED_PRECONDITION.name() + ") with instance ID " + SERVICE_EXCEPTION.getErrorInstanceId()); } }