Search in sources :

Example 1 with CustomErrorException

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

the class Elide method mapError.

public CustomErrorException mapError(RuntimeException error) {
    if (errorMapper != null) {
        log.trace("Attempting to map unknown exception of type {}", error.getClass());
        CustomErrorException customizedError = errorMapper.map(error);
        if (customizedError != null) {
            log.debug("Successfully mapped exception from type {} to {}", error.getClass(), customizedError.getClass());
            return customizedError;
        } else {
            log.debug("No error mapping present for {}", error.getClass());
        }
    }
    return null;
}
Also used : CustomErrorException(com.yahoo.elide.core.exceptions.CustomErrorException)

Example 2 with CustomErrorException

use of com.yahoo.elide.core.exceptions.CustomErrorException 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)

Example 3 with CustomErrorException

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

the class QueryRunner method handleRuntimeException.

public static ElideResponse handleRuntimeException(Elide elide, RuntimeException error, boolean isVerbose) {
    CustomErrorException mappedException = elide.mapError(error);
    ObjectMapper mapper = elide.getMapper().getObjectMapper();
    if (mappedException != null) {
        return buildErrorResponse(mapper, mappedException, isVerbose);
    }
    if (error instanceof WebApplicationException) {
        WebApplicationException e = (WebApplicationException) error;
        log.debug("WebApplicationException", e);
        String body = e.getResponse().getEntity() != null ? e.getResponse().getEntity().toString() : e.getMessage();
        return ElideResponse.builder().responseCode(e.getResponse().getStatus()).body(body).build();
    }
    if (error instanceof HttpStatusException) {
        HttpStatusException e = (HttpStatusException) error;
        if (e instanceof ForbiddenAccessException) {
            if (log.isDebugEnabled()) {
                log.debug("{}", ((ForbiddenAccessException) e).getLoggedMessage());
            }
        } else {
            log.debug("Caught HTTP status exception {}", e.getStatus(), e);
        }
        return buildErrorResponse(mapper, new HttpStatusException(200, e.getMessage()) {

            @Override
            public int getStatus() {
                return 200;
            }

            @Override
            public Pair<Integer, JsonNode> getErrorResponse() {
                return e.getErrorResponse();
            }

            @Override
            public Pair<Integer, JsonNode> getVerboseErrorResponse() {
                return e.getVerboseErrorResponse();
            }

            @Override
            public String getVerboseMessage() {
                return e.getVerboseMessage();
            }

            @Override
            public String toString() {
                return e.toString();
            }
        }, 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(mapper, new CustomErrorException(HttpStatus.SC_OK, 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) ConstraintViolationException(javax.validation.ConstraintViolationException) CustomErrorException(com.yahoo.elide.core.exceptions.CustomErrorException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Pair(org.apache.commons.lang3.tuple.Pair)

Example 4 with CustomErrorException

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

the class QueryRunner method buildErrorResponse.

public static ElideResponse buildErrorResponse(ObjectMapper mapper, HttpStatusException error, boolean isVerbose) {
    JsonNode errorNode;
    if (!(error instanceof CustomErrorException)) {
        // get the error message and optionally encode it
        String errorMessage = isVerbose ? error.getVerboseMessage() : error.getMessage();
        errorMessage = Encode.forHtml(errorMessage);
        ErrorObjects errors = ErrorObjects.builder().addError().with("message", errorMessage).build();
        errorNode = mapper.convertValue(errors, JsonNode.class);
    } else {
        errorNode = isVerbose ? error.getVerboseErrorResponse().getRight() : error.getErrorResponse().getRight();
    }
    String errorBody;
    try {
        errorBody = mapper.writeValueAsString(errorNode);
    } catch (JsonProcessingException e) {
        errorBody = errorNode.toString();
    }
    return ElideResponse.builder().responseCode(error.getStatus()).body(errorBody).build();
}
Also used : ErrorObjects(com.yahoo.elide.core.exceptions.ErrorObjects) JsonNode(com.fasterxml.jackson.databind.JsonNode) CustomErrorException(com.yahoo.elide.core.exceptions.CustomErrorException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

CustomErrorException (com.yahoo.elide.core.exceptions.CustomErrorException)4 ErrorObjects (com.yahoo.elide.core.exceptions.ErrorObjects)3 ForbiddenAccessException (com.yahoo.elide.core.exceptions.ForbiddenAccessException)2 HttpStatusException (com.yahoo.elide.core.exceptions.HttpStatusException)2 HashMap (java.util.HashMap)2 ConstraintViolationException (javax.validation.ConstraintViolationException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 InvalidURLException (com.yahoo.elide.core.exceptions.InvalidURLException)1 JsonPatchExtensionException (com.yahoo.elide.core.exceptions.JsonPatchExtensionException)1 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)1 Pair (org.apache.commons.lang3.tuple.Pair)1