Search in sources :

Example 1 with JsonError

use of io.micronaut.http.hateoas.JsonError in project check-ins by objectcomputing.

the class QuestionCategoryControllerTest method testPUTQuestionCategoryNotFound.

@Test
public void testPUTQuestionCategoryNotFound() {
    QuestionCategory requestBody = new QuestionCategory();
    requestBody.setId(UUID.randomUUID());
    requestBody.setName("Fake Category");
    HttpClientResponseException thrown = assertThrows(HttpClientResponseException.class, () -> {
        client.toBlocking().exchange(HttpRequest.PUT("/", requestBody).basicAuth(MEMBER_ROLE, MEMBER_ROLE));
    });
    JsonError responseBody = thrown.getResponse().getBody(JsonError.class).get();
    assertEquals(("No category with id " + requestBody.getId()), responseBody.getMessage());
    assertEquals(HttpStatus.NOT_FOUND, thrown.getStatus());
}
Also used : HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) JsonError(io.micronaut.http.hateoas.JsonError) Test(org.junit.jupiter.api.Test)

Example 2 with JsonError

use of io.micronaut.http.hateoas.JsonError in project check-ins by objectcomputing.

the class QuestionCategoryControllerTest method testPUTNoIDSupplied.

@Test
public void testPUTNoIDSupplied() {
    QuestionCategoryCreateDTO requestBody = new QuestionCategoryCreateDTO();
    requestBody.setName("Fake Category");
    HttpClientResponseException thrown = assertThrows(HttpClientResponseException.class, () -> {
        client.toBlocking().exchange(HttpRequest.PUT("/", requestBody).basicAuth(ADMIN_ROLE, ADMIN_ROLE));
    });
    JsonError responseBody = thrown.getResponse().getBody(JsonError.class).get();
    assertEquals("This question category does not exist", responseBody.getMessage());
    assertEquals(HttpStatus.BAD_REQUEST, thrown.getStatus());
}
Also used : HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) JsonError(io.micronaut.http.hateoas.JsonError) Test(org.junit.jupiter.api.Test)

Example 3 with JsonError

use of io.micronaut.http.hateoas.JsonError in project kestra by kestra-io.

the class ErrorControllerTest method type.

@Test
void type() {
    Map<String, Object> flow = ImmutableMap.of("id", IdUtils.create(), "namespace", "io.kestra.test", "tasks", Collections.singletonList(ImmutableMap.of("id", IdUtils.create(), "type", "io.kestra.invalid")));
    HttpClientResponseException exception = assertThrows(HttpClientResponseException.class, () -> {
        client.toBlocking().retrieve(POST("/api/v1/flows", flow), Argument.of(Flow.class), Argument.of(JsonError.class));
    });
    assertThat(exception.getStatus(), is(HttpStatus.UNPROCESSABLE_ENTITY));
    String response = exception.getResponse().getBody(String.class).get();
    assertThat(response, containsString("Invalid type: io.kestra.invalid"));
    assertThat(response, containsString("\"path\":\"io.kestra.core.models.flows.Flow[\\\"tasks\\\"] > java.util.ArrayList[0]\""));
    assertThat(response, containsString("Failed to convert argument"));
// missing getter & setter on JsonError
// assertThat(exception.getResponse().getBody(JsonError.class).get().getEmbedded().get("errors").get().get(0).getPath(), containsInAnyOrder("tasks"));
}
Also used : HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) JsonError(io.micronaut.http.hateoas.JsonError) Matchers.containsString(org.hamcrest.Matchers.containsString) Flow(io.kestra.core.models.flows.Flow) Test(org.junit.jupiter.api.Test) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest)

Example 4 with JsonError

use of io.micronaut.http.hateoas.JsonError in project kestra by kestra-io.

the class FlowControllerTest method invalidUpdateFlow.

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
void invalidUpdateFlow() {
    String flowId = IdUtils.create();
    Flow flow = generateFlow(flowId, "io.kestra.unittest", "a");
    Flow result = client.toBlocking().retrieve(POST("/api/v1/flows", flow), Flow.class);
    assertThat(result.getId(), is(flow.getId()));
    Flow finalFlow = generateFlow(IdUtils.create(), "io.kestra.unittest2", "b");
    ;
    HttpClientResponseException e = assertThrows(HttpClientResponseException.class, () -> {
        client.toBlocking().exchange(PUT("/api/v1/flows/" + flow.getNamespace() + "/" + flowId, finalFlow), Argument.of(Flow.class), Argument.of(JsonError.class));
    });
    String jsonError = e.getResponse().getBody(String.class).get();
    assertThat(e.getStatus(), is(UNPROCESSABLE_ENTITY));
    assertThat(jsonError, containsString("flow.id"));
    assertThat(jsonError, containsString("flow.namespace"));
}
Also used : HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) JsonError(io.micronaut.http.hateoas.JsonError) Matchers.containsString(org.hamcrest.Matchers.containsString) Flow(io.kestra.core.models.flows.Flow) Test(org.junit.jupiter.api.Test) AbstractMemoryRunnerTest(io.kestra.core.runners.AbstractMemoryRunnerTest)

Example 5 with JsonError

use of io.micronaut.http.hateoas.JsonError 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)

Aggregations

JsonError (io.micronaut.http.hateoas.JsonError)13 HttpClientResponseException (io.micronaut.http.client.exceptions.HttpClientResponseException)6 Test (org.junit.jupiter.api.Test)6 Flow (io.kestra.core.models.flows.Flow)2 Error (io.micronaut.http.annotation.Error)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ErrorMessage (com.bakdata.quick.common.api.model.ErrorMessage)1 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 AbstractMemoryRunnerTest (io.kestra.core.runners.AbstractMemoryRunnerTest)1 NonNull (io.micronaut.core.annotation.NonNull)1 ConversionErrorException (io.micronaut.core.convert.exceptions.ConversionErrorException)1 MutableHttpResponse (io.micronaut.http.MutableHttpResponse)1 MicronautTest (io.micronaut.test.extensions.junit5.annotation.MicronautTest)1 UnsatisfiedQueryValueRouteException (io.micronaut.web.router.exceptions.UnsatisfiedQueryValueRouteException)1 FileNotFoundException (java.io.FileNotFoundException)1 PrintWriter (java.io.PrintWriter)1