use of org.projectnessie.error.ErrorCode in project nessie by projectnessie.
the class TestTranslatingVersionNessieApi method exceptionsInternalError.
@Test
void exceptionsInternalError() throws Exception {
int statusCode = 500;
String type = "internal error";
String nessieErrorMessage = "Message " + type;
String reason = "Reason " + type;
String serverStackTrace = "Stack trace " + type;
String exceptionMessage = String.format("%s (HTTP/%d): %s\n%s", reason, statusCode, nessieErrorMessage, serverStackTrace);
String oldVersionType = "org.projectnessie.client.rest.NessieInternalServerException";
Class<? extends Throwable> expectedType = NessieInternalServerException.class;
ErrorCode errorCode = ErrorCode.UNKNOWN;
Throwable translated = translateException(oldVersionType, errorCode.httpStatus(), nessieErrorMessage, reason, serverStackTrace);
assertThat(translated).isInstanceOf(expectedType).asInstanceOf(InstanceOfAssertFactories.type(NessieInternalServerException.class)).extracting(Throwable::getMessage, NessieInternalServerException::getError).containsExactly(exceptionMessage, ImmutableNessieError.builder().reason(reason).message(nessieErrorMessage).status(statusCode).errorCode(errorCode).serverStackTrace(serverStackTrace).build());
}
use of org.projectnessie.error.ErrorCode in project nessie by projectnessie.
the class ResteasyExceptionMapper method toResponse.
@Override
public Response toResponse(ResteasyViolationException exception) {
Exception e = exception.getException();
if (e == null) {
boolean returnValueViolation = !exception.getReturnValueViolations().isEmpty();
ErrorCode errorCode = returnValueViolation ? ErrorCode.UNKNOWN : ErrorCode.BAD_REQUEST;
return buildExceptionResponse(errorCode, exception.getMessage(), exception, // no need to send the stack trace for a validation-error
false, b -> b.header(Validation.VALIDATION_HEADER, "true"));
}
return buildExceptionResponse(ErrorCode.UNKNOWN, unwrapException(exception), exception);
}
use of org.projectnessie.error.ErrorCode in project nessie by projectnessie.
the class NessieExceptionMapper method toResponse.
@Override
public Response toResponse(Exception exception) {
ErrorCode errorCode;
String message;
if (exception instanceof BaseNessieClientServerException) {
BaseNessieClientServerException e = (BaseNessieClientServerException) exception;
errorCode = e.getErrorCode();
message = exception.getMessage();
} else if (exception.getCause() instanceof BaseNessieClientServerException) {
BaseNessieClientServerException e = (BaseNessieClientServerException) exception.getCause();
errorCode = e.getErrorCode();
message = exception.getCause().getMessage();
} else if (exception instanceof JsonParseException || exception instanceof JsonMappingException || exception instanceof IllegalArgumentException) {
errorCode = ErrorCode.BAD_REQUEST;
message = exception.getMessage();
} else if (exception instanceof BackendLimitExceededException) {
LOGGER.warn("Backend throttled/refused the request: {}", exception.toString());
errorCode = ErrorCode.TOO_MANY_REQUESTS;
message = "Backend store refused to process the request: " + exception;
} else if (exception instanceof AccessControlException) {
errorCode = ErrorCode.FORBIDDEN;
message = exception.getMessage();
} else {
LOGGER.warn("Unhandled exception returned as HTTP/500 to client", exception);
errorCode = ErrorCode.UNKNOWN;
message = Throwables.getCausalChain(exception).stream().map(Throwable::toString).collect(Collectors.joining(", caused by"));
}
return buildExceptionResponse(errorCode, message, exception);
}
Aggregations