private static Response.ResponseBuilder encode503ResponseInternal(Exception exception) { return encodeExceptionResponse(exception, 503); }
/** * Returns a response builder with the specified status code, with a JSON serialized form of the causing exception, * and an appropriate HTTP header (Content-Type: application/json). */ public static Response.ResponseBuilder encodeExceptionResponse(Exception exception, int statusCode) { return Response.serverError() .entity(createSerializableError(exception)) .status(statusCode) .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); }
/** * Returns a 503 response, with a JSON-serialized form of the causing exception as the body, and an appropriate * HTTP header (Content-Type: application/json). */ public static Response encode503ResponseWithoutRetryAfter(Exception exception) { return encode503ResponseInternal(exception).build(); }
@Override public Response toResponse(NotInitializedException exception) { return ExceptionMappers.encode503ResponseWithoutRetryAfter(exception); } }
/** * Returns a 503 response, with body corresponding to the serialized exception. */ @Override public Response toResponse(NotCurrentLeaderException exception) { return ExceptionMappers.encode503ResponseWithRetryAfter(exception); } }
private static SerializableError createSerializableError(Exception exception) { return SerializableError.of( exception.getMessage(), exception.getClass(), getStackTraceElementsAsList(exception)); }
@Override public Response toResponse(BlockingTimeoutException exception) { return ExceptionMappers.encode503ResponseWithoutRetryAfter(exception); } }
/** * Returns a 503 response, with body corresponding to the serialized exception. */ @Override public Response toResponse(NotCurrentLeaderException exception) { return ExceptionMappers.encode503ResponseWithRetryAfter(exception); } }
private static SerializableError createSerializableError(Exception exception) { return SerializableError.of( exception.getMessage(), exception.getClass(), getStackTraceElementsAsList(exception)); }
@Override public Response toResponse(TooManyRequestsException exception) { return ExceptionMappers.encodeExceptionResponse(exception, TOO_MANY_REQUESTS_429) .build(); } }
/** * Returns a 503 response, with a JSON-serialized form of the causing exception as the body, and an appropriate * HTTP header (Content-Type: application/json). In addition, we also include a Retry-After header with a zero * value. */ public static Response encode503ResponseWithRetryAfter(Exception exception) { return encode503ResponseInternal(exception) .header(HttpHeaders.RETRY_AFTER, "0") .build(); }
@Override public Response toResponse(NotInitializedException exception) { return ExceptionMappers.encode503ResponseWithoutRetryAfter(exception); } }
/** * Returns a response builder with the specified status code, with a JSON serialized form of the causing exception, * and an appropriate HTTP header (Content-Type: application/json). */ public static Response.ResponseBuilder encodeExceptionResponse(Exception exception, int statusCode) { return Response.serverError() .entity(createSerializableError(exception)) .status(statusCode) .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); }
@Test public void canSerializeExceptions() throws IOException { Response exceptionResponse = ExceptionMappers.encodeExceptionResponse( SERVICE_NOT_AVAILABLE_EXCEPTION, STATUS_CODE).build(); Object exceptionEntity = exceptionResponse.getEntity(); String serializedException = MAPPER.writeValueAsString(exceptionEntity); String expected = getNormalizedRepresentationOfExpectedJson(); assertThat(serializedException).isEqualTo(expected); }
/** * Returns a 503 response, with a JSON-serialized form of the causing exception as the body, and an appropriate * HTTP header (Content-Type: application/json). */ public static Response encode503ResponseWithoutRetryAfter(Exception exception) { return encode503ResponseInternal(exception).build(); }
@Override public Response toResponse(BlockingTimeoutException exception) { return ExceptionMappers.encode503ResponseWithoutRetryAfter(exception); } }
private static Response.ResponseBuilder encode503ResponseInternal(Exception exception) { return encodeExceptionResponse(exception, 503); }
/** * Returns a 503 response, with a JSON-serialized form of the causing exception as the body, and an appropriate * HTTP header (Content-Type: application/json). In addition, we also include a Retry-After header with a zero * value. */ public static Response encode503ResponseWithRetryAfter(Exception exception) { return encode503ResponseInternal(exception) .header(HttpHeaders.RETRY_AFTER, "0") .build(); }
@Override public Response toResponse(TooManyRequestsException exception) { return ExceptionMappers.encodeExceptionResponse(exception, TOO_MANY_REQUESTS_429) .build(); } }