Search in sources :

Example 1 with Error

use of io.micronaut.http.annotation.Error in project kestra by kestra-io.

the class ErrorController method error.

@Error(global = true)
public HttpResponse<JsonError> error(HttpRequest<?> request, ConversionErrorException e) {
    if (e.getConversionError().getCause() instanceof InvalidTypeIdException) {
        try {
            InvalidTypeIdException invalidTypeIdException = ((InvalidTypeIdException) e.getConversionError().getCause());
            String path = path(invalidTypeIdException);
            Field typeField = InvalidTypeIdException.class.getDeclaredField("_typeId");
            typeField.setAccessible(true);
            Object typeClass = typeField.get(invalidTypeIdException);
            JsonError error = new JsonError("Invalid type: " + typeClass).link(Link.SELF, Link.of(request.getUri())).embedded("errors", Arrays.asList(new JsonError("Invalid type: " + typeClass).path(path), new JsonError(e.getMessage()).path(path)));
            return jsonError(error, HttpStatus.UNPROCESSABLE_ENTITY, "Invalid entity");
        } catch (Exception ignored) {
        }
    } else if (e.getConversionError().getCause() instanceof JsonMappingException) {
        try {
            JsonMappingException jsonMappingException = ((JsonMappingException) e.getConversionError().getCause());
            String path = path(jsonMappingException);
            JsonError error = new JsonError("Invalid json mapping").link(Link.SELF, Link.of(request.getUri())).embedded("errors", Collections.singletonList(new JsonError(e.getMessage()).path(path)));
            return jsonError(error, HttpStatus.UNPROCESSABLE_ENTITY, "Invalid json mapping");
        } catch (Exception ignored) {
        }
    }
    return jsonError(request, e, HttpStatus.UNPROCESSABLE_ENTITY, "Internal server error");
}
Also used : Field(java.lang.reflect.Field) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) InvalidTypeIdException(com.fasterxml.jackson.databind.exc.InvalidTypeIdException) JsonError(io.micronaut.http.hateoas.JsonError) InvalidFormatException(com.fasterxml.jackson.databind.exc.InvalidFormatException) ConversionErrorException(io.micronaut.core.convert.exceptions.ConversionErrorException) InvalidTypeIdException(com.fasterxml.jackson.databind.exc.InvalidTypeIdException) FileNotFoundException(java.io.FileNotFoundException) DeserializationException(io.kestra.core.exceptions.DeserializationException) ConstraintViolationException(javax.validation.ConstraintViolationException) UnsatisfiedQueryValueRouteException(io.micronaut.web.router.exceptions.UnsatisfiedQueryValueRouteException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) NoSuchElementException(java.util.NoSuchElementException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonError(io.micronaut.http.hateoas.JsonError) Error(io.micronaut.http.annotation.Error)

Example 2 with Error

use of io.micronaut.http.annotation.Error in project akhq by tchiotludo.

the class ErrorController method error.

@Error(global = true)
public HttpResponse<?> error(HttpRequest<?> request, Throwable e) {
    log.error(e.getMessage(), e);
    StringWriter stringWriter = new StringWriter();
    e.printStackTrace(new PrintWriter(stringWriter));
    JsonError error = new JsonError("Internal Server Error: " + e.getMessage()).link(Link.SELF, Link.of(request.getUri())).embedded("stacktrace", new JsonError(stringWriter.toString()));
    return HttpResponse.<JsonError>serverError().body(error);
}
Also used : StringWriter(java.io.StringWriter) JsonError(io.micronaut.http.hateoas.JsonError) PrintWriter(java.io.PrintWriter) JsonError(io.micronaut.http.hateoas.JsonError) Error(io.micronaut.http.annotation.Error)

Aggregations

Error (io.micronaut.http.annotation.Error)2 JsonError (io.micronaut.http.hateoas.JsonError)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 InvalidFormatException (com.fasterxml.jackson.databind.exc.InvalidFormatException)1 InvalidTypeIdException (com.fasterxml.jackson.databind.exc.InvalidTypeIdException)1 DeserializationException (io.kestra.core.exceptions.DeserializationException)1 ConversionErrorException (io.micronaut.core.convert.exceptions.ConversionErrorException)1 UnsatisfiedQueryValueRouteException (io.micronaut.web.router.exceptions.UnsatisfiedQueryValueRouteException)1 FileNotFoundException (java.io.FileNotFoundException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Field (java.lang.reflect.Field)1 NoSuchElementException (java.util.NoSuchElementException)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1