Search in sources :

Example 1 with ErrorMessage

use of com.bakdata.quick.common.api.model.ErrorMessage in project quick by bakdata.

the class ControllerUpdateSchemaTest method returnsErrorForWrongBody.

@Test
void returnsErrorForWrongBody() {
    final HttpRequest<?> httpRequest = HttpRequest.create(HttpMethod.POST, "/control/schema").body(new SchemaData("test"));
    assertThatExceptionOfType(HttpClientResponseException.class).isThrownBy(() -> this.httpClient.retrieve(httpRequest).blockingFirst()).isInstanceOfSatisfying(HttpClientResponseException.class, ex -> assertThat(extractErrorMessage(ex)).isPresent().get().hasFieldOrPropertyWithValue("type", "errors/clientError").hasFieldOrPropertyWithValue("title", "Bad Request").extracting(ErrorMessage::getDetail, InstanceOfAssertFactories.STRING).startsWith("Could not parse GraphQL schema:"));
}
Also used : SchemaData(com.bakdata.quick.common.api.model.gateway.SchemaData) ErrorMessage(com.bakdata.quick.common.api.model.ErrorMessage) Test(org.junit.jupiter.api.Test) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest)

Example 2 with ErrorMessage

use of com.bakdata.quick.common.api.model.ErrorMessage in project quick by bakdata.

the class GeneralExceptionHandler method handle.

@Override
public HttpResponse<ErrorMessage> handle(final HttpRequest request, final Exception exception) {
    log.error("Unexpected and not handled error: ", exception);
    final String detail = "An unexpected error occurred:" + exception.getMessage();
    final ErrorMessage error = HttpStatusError.toError(HttpStatus.INTERNAL_SERVER_ERROR, request.getPath(), detail);
    return HttpResponse.<ErrorMessage>serverError().body(error);
}
Also used : ErrorMessage(com.bakdata.quick.common.api.model.ErrorMessage)

Example 3 with ErrorMessage

use of com.bakdata.quick.common.api.model.ErrorMessage in project quick by bakdata.

the class SchemaRegistryExceptionHandler method handle.

@Override
public HttpResponse<ErrorMessage> handle(final HttpRequest request, final RestClientException exception) {
    // strip error code as it is not relevant for user
    final String message;
    if (exception.getMessage() != null) {
        final int i = exception.getMessage().indexOf("; error code: ");
        message = exception.getMessage().substring(0, i);
    } else {
        message = UNKNOWN_ERROR;
    }
    final ErrorMessage errorMessage = ErrorMessage.builder().type("error/schema").code(exception.getStatus()).title("Schema Error").detail(message).uriPath(request.getPath()).build();
    return HttpResponse.<ErrorMessage>status(HttpStatus.valueOf(exception.getStatus())).body(errorMessage);
}
Also used : ErrorMessage(com.bakdata.quick.common.api.model.ErrorMessage)

Example 4 with ErrorMessage

use of com.bakdata.quick.common.api.model.ErrorMessage in project quick by bakdata.

the class AvroExceptionHandler method handle.

@Override
public HttpResponse<ErrorMessage> handle(final HttpRequest request, final AvroRuntimeException exception) {
    final ErrorMessage errorMessage;
    final String detail;
    if (exception instanceof AvroTypeException || exception instanceof AvroMissingFieldException || exception instanceof UnresolvedUnionException) {
        detail = String.format("Could not parse input: %s", exception.getMessage());
    } else if (exception instanceof AvroConversionException) {
        detail = String.format("An Error occurred during conversion of the Avro: %s", exception.getMessage());
    } else {
        errorMessage = HttpStatusError.toError(HttpStatus.INTERNAL_SERVER_ERROR, request.getPath(), exception.getMessage());
        return HttpResponse.<ErrorMessage>status(HttpStatus.valueOf(errorMessage.getCode())).body(errorMessage);
    }
    errorMessage = HttpStatusError.toError(HttpStatus.BAD_REQUEST, request.getPath(), detail);
    return HttpResponse.<ErrorMessage>status(HttpStatus.valueOf(errorMessage.getCode())).body(errorMessage);
}
Also used : AvroMissingFieldException(org.apache.avro.AvroMissingFieldException) AvroConversionException(tech.allegro.schema.json2avro.converter.AvroConversionException) ErrorMessage(com.bakdata.quick.common.api.model.ErrorMessage) UnresolvedUnionException(org.apache.avro.UnresolvedUnionException) AvroTypeException(org.apache.avro.AvroTypeException)

Example 5 with ErrorMessage

use of com.bakdata.quick.common.api.model.ErrorMessage in project quick by bakdata.

the class JsonErrorExceptionHandler method doFilter.

@Override
public Publisher<MutableHttpResponse<?>> doFilter(final HttpRequest<?> request, final ServerFilterChain chain) {
    return Flowable.fromPublisher(chain.proceed(request)).map(response -> {
        // check all 4xx and 5xx (client and server errors)
        if (response.getStatus().getCode() >= 400) {
            final Optional<JsonError> body = response.getBody(JsonError.class);
            if (body.isPresent()) {
                log.trace("Convert JSON error to custom error message");
                final JsonError jsonError = body.get();
                final ErrorMessage errorMessage = HttpStatusError.toError(response.getStatus(), request.getPath(), jsonError.getMessage());
                return HttpResponse.status(response.getStatus()).body(errorMessage);
            }
        }
        return response;
    });
}
Also used : JsonError(io.micronaut.http.hateoas.JsonError) ErrorMessage(com.bakdata.quick.common.api.model.ErrorMessage)

Aggregations

ErrorMessage (com.bakdata.quick.common.api.model.ErrorMessage)6 SchemaData (com.bakdata.quick.common.api.model.gateway.SchemaData)1 JsonError (io.micronaut.http.hateoas.JsonError)1 MicronautTest (io.micronaut.test.extensions.junit5.annotation.MicronautTest)1 AvroMissingFieldException (org.apache.avro.AvroMissingFieldException)1 AvroTypeException (org.apache.avro.AvroTypeException)1 UnresolvedUnionException (org.apache.avro.UnresolvedUnionException)1 Test (org.junit.jupiter.api.Test)1 AvroConversionException (tech.allegro.schema.json2avro.converter.AvroConversionException)1