Search in sources :

Example 1 with JsonPatchExtensionException

use of com.yahoo.elide.core.exceptions.JsonPatchExtensionException in project elide by yahoo.

the class JsonApiPatch method throwErrorResponse.

/**
 * Turn an exception into a proper error response from patch extension.
 */
private void throwErrorResponse() {
    ArrayNode errorContainer = getErrorContainer();
    boolean failed = false;
    for (PatchAction action : actions) {
        failed = processAction(errorContainer, failed, action);
    }
    JsonPatchExtensionException failure = new JsonPatchExtensionException(HttpStatus.SC_BAD_REQUEST, errorContainer);
    // attach error causes to exception
    for (PatchAction action : actions) {
        if (action.cause != null) {
            failure.addSuppressed(action.cause);
        }
    }
    throw failure;
}
Also used : ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JsonPatchExtensionException(com.yahoo.elide.core.exceptions.JsonPatchExtensionException)

Example 2 with JsonPatchExtensionException

use of com.yahoo.elide.core.exceptions.JsonPatchExtensionException in project elide by yahoo.

the class Elide method handleRuntimeException.

private ElideResponse handleRuntimeException(RuntimeException error, boolean isVerbose) {
    CustomErrorException mappedException = mapError(error);
    if (mappedException != null) {
        return buildErrorResponse(mappedException, isVerbose);
    }
    if (error instanceof WebApplicationException) {
        throw error;
    }
    if (error instanceof ForbiddenAccessException) {
        ForbiddenAccessException e = (ForbiddenAccessException) error;
        if (log.isDebugEnabled()) {
            log.debug("{}", e.getLoggedMessage());
        }
        return buildErrorResponse(e, isVerbose);
    }
    if (error instanceof JsonPatchExtensionException) {
        JsonPatchExtensionException e = (JsonPatchExtensionException) error;
        log.debug("JSON patch extension exception caught", e);
        return buildErrorResponse(e, isVerbose);
    }
    if (error instanceof HttpStatusException) {
        HttpStatusException e = (HttpStatusException) error;
        log.debug("Caught HTTP status exception", e);
        return buildErrorResponse(e, isVerbose);
    }
    if (error instanceof ParseCancellationException) {
        ParseCancellationException e = (ParseCancellationException) error;
        log.debug("Parse cancellation exception uncaught by Elide (i.e. invalid URL)", e);
        return buildErrorResponse(new InvalidURLException(e), isVerbose);
    }
    if (error instanceof ConstraintViolationException) {
        ConstraintViolationException e = (ConstraintViolationException) error;
        log.debug("Constraint violation exception caught", e);
        String message = "Constraint violation";
        final ErrorObjects.ErrorObjectsBuilder errorObjectsBuilder = ErrorObjects.builder();
        for (ConstraintViolation<?> constraintViolation : e.getConstraintViolations()) {
            errorObjectsBuilder.addError().withDetail(constraintViolation.getMessage());
            final String propertyPathString = constraintViolation.getPropertyPath().toString();
            if (!propertyPathString.isEmpty()) {
                Map<String, Object> source = new HashMap<>(1);
                source.put("property", propertyPathString);
                errorObjectsBuilder.with("source", source);
            }
        }
        return buildErrorResponse(new CustomErrorException(HttpStatus.SC_BAD_REQUEST, message, errorObjectsBuilder.build()), isVerbose);
    }
    log.error("Error or exception uncaught by Elide", error);
    throw new RuntimeException(error);
}
Also used : ErrorObjects(com.yahoo.elide.core.exceptions.ErrorObjects) WebApplicationException(javax.ws.rs.WebApplicationException) HashMap(java.util.HashMap) HttpStatusException(com.yahoo.elide.core.exceptions.HttpStatusException) ForbiddenAccessException(com.yahoo.elide.core.exceptions.ForbiddenAccessException) InvalidURLException(com.yahoo.elide.core.exceptions.InvalidURLException) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) ConstraintViolationException(javax.validation.ConstraintViolationException) CustomErrorException(com.yahoo.elide.core.exceptions.CustomErrorException) JsonPatchExtensionException(com.yahoo.elide.core.exceptions.JsonPatchExtensionException)

Aggregations

JsonPatchExtensionException (com.yahoo.elide.core.exceptions.JsonPatchExtensionException)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 CustomErrorException (com.yahoo.elide.core.exceptions.CustomErrorException)1 ErrorObjects (com.yahoo.elide.core.exceptions.ErrorObjects)1 ForbiddenAccessException (com.yahoo.elide.core.exceptions.ForbiddenAccessException)1 HttpStatusException (com.yahoo.elide.core.exceptions.HttpStatusException)1 InvalidURLException (com.yahoo.elide.core.exceptions.InvalidURLException)1 HashMap (java.util.HashMap)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)1