Search in sources :

Example 1 with BackendLimitExceededException

use of org.projectnessie.versioned.BackendLimitExceededException in project nessie by projectnessie.

the class ErrorTestService method unhandledExceptionInTvsStore.

/**
 * Throws an exception depending on the parameter.
 *
 * @return nothing
 * @see TestNessieError#unhandledRuntimeExceptionInStore()
 * @see TestNessieError#backendThrottledExceptionInStore()
 */
@Path("unhandledExceptionInTvsStore/{exception}")
@GET
@Consumes(MediaType.APPLICATION_JSON)
public String unhandledExceptionInTvsStore(@PathParam("exception") String exception) throws ReferenceNotFoundException {
    Exception ex;
    switch(exception) {
        case "runtime":
            ex = new RuntimeException("Store.getValues-throwing");
            break;
        case "throttle":
            ex = new BackendLimitExceededException("Store.getValues-throttled");
            break;
        default:
            throw new IllegalArgumentException("test code error");
    }
    DatabaseAdapter databaseAdapter = Mockito.mock(DatabaseAdapter.class);
    Mockito.when(databaseAdapter.namedRefs(Mockito.any())).thenThrow(ex);
    PersistVersionStore<BaseContent, CommitMessage, BaseContent.Type> tvs = new PersistVersionStore<>(databaseAdapter, SimpleStoreWorker.INSTANCE);
    try (Stream<ReferenceInfo<CommitMessage>> refs = tvs.getNamedRefs(GetNamedRefsParams.DEFAULT)) {
        refs.forEach(ref -> {
        });
    }
    return "we should not get here";
}
Also used : PersistVersionStore(org.projectnessie.versioned.persist.store.PersistVersionStore) MediaType(javax.ws.rs.core.MediaType) CommitMessage(org.projectnessie.versioned.testworker.CommitMessage) BackendLimitExceededException(org.projectnessie.versioned.BackendLimitExceededException) BaseContent(org.projectnessie.versioned.testworker.BaseContent) NessieReferenceNotFoundException(org.projectnessie.error.NessieReferenceNotFoundException) BackendLimitExceededException(org.projectnessie.versioned.BackendLimitExceededException) ConstraintDefinitionException(javax.validation.ConstraintDefinitionException) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) ConstraintDeclarationException(javax.validation.ConstraintDeclarationException) GroupDefinitionException(javax.validation.GroupDefinitionException) NessieNotFoundException(org.projectnessie.error.NessieNotFoundException) DatabaseAdapter(org.projectnessie.versioned.persist.adapter.DatabaseAdapter) ReferenceInfo(org.projectnessie.versioned.ReferenceInfo) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) GET(javax.ws.rs.GET)

Example 2 with BackendLimitExceededException

use of org.projectnessie.versioned.BackendLimitExceededException 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

BackendLimitExceededException (org.projectnessie.versioned.BackendLimitExceededException)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 AccessControlException (java.security.AccessControlException)1 ConstraintDeclarationException (javax.validation.ConstraintDeclarationException)1 ConstraintDefinitionException (javax.validation.ConstraintDefinitionException)1 GroupDefinitionException (javax.validation.GroupDefinitionException)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 MediaType (javax.ws.rs.core.MediaType)1 BaseNessieClientServerException (org.projectnessie.error.BaseNessieClientServerException)1 ErrorCode (org.projectnessie.error.ErrorCode)1 NessieNotFoundException (org.projectnessie.error.NessieNotFoundException)1 NessieReferenceNotFoundException (org.projectnessie.error.NessieReferenceNotFoundException)1 ReferenceInfo (org.projectnessie.versioned.ReferenceInfo)1 ReferenceNotFoundException (org.projectnessie.versioned.ReferenceNotFoundException)1 DatabaseAdapter (org.projectnessie.versioned.persist.adapter.DatabaseAdapter)1 PersistVersionStore (org.projectnessie.versioned.persist.store.PersistVersionStore)1 BaseContent (org.projectnessie.versioned.testworker.BaseContent)1