Search in sources :

Example 1 with ErrorCode

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());
}
Also used : ErrorCode(org.projectnessie.error.ErrorCode) NessieInternalServerException(org.projectnessie.client.rest.NessieInternalServerException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with ErrorCode

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);
}
Also used : ErrorCode(org.projectnessie.error.ErrorCode) ResteasyViolationException(org.jboss.resteasy.api.validation.ResteasyViolationException) ValidationException(javax.validation.ValidationException)

Example 3 with ErrorCode

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);
}
Also used : BackendLimitExceededException(org.projectnessie.versioned.BackendLimitExceededException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) AccessControlException(java.security.AccessControlException) ErrorCode(org.projectnessie.error.ErrorCode) JsonParseException(com.fasterxml.jackson.core.JsonParseException) BaseNessieClientServerException(org.projectnessie.error.BaseNessieClientServerException)

Aggregations

ErrorCode (org.projectnessie.error.ErrorCode)3 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 AccessControlException (java.security.AccessControlException)1 ValidationException (javax.validation.ValidationException)1 ResteasyViolationException (org.jboss.resteasy.api.validation.ResteasyViolationException)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 NessieInternalServerException (org.projectnessie.client.rest.NessieInternalServerException)1 BaseNessieClientServerException (org.projectnessie.error.BaseNessieClientServerException)1 BackendLimitExceededException (org.projectnessie.versioned.BackendLimitExceededException)1