diff options
author | Bjørn Christian Seime <bjorncs@vespa.ai> | 2024-02-27 08:05:51 +0100 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@vespa.ai> | 2024-02-27 08:05:51 +0100 |
commit | b107fe16cf50d5ffde3499667461103275725e83 (patch) | |
tree | 22862cb398b35342e6f07a20f74f5c7b4483d28f /container-core/src/test/java | |
parent | 7e89f23fb55879d7265ae74bd218a29df642fe5d (diff) |
Handle `InvalidJsonException` by default. Fix mapper ordering
Diffstat (limited to 'container-core/src/test/java')
-rw-r--r-- | container-core/src/test/java/com/yahoo/restapi/RestApiImplTest.java | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/container-core/src/test/java/com/yahoo/restapi/RestApiImplTest.java b/container-core/src/test/java/com/yahoo/restapi/RestApiImplTest.java index d27e04bbd7a..d959344685a 100644 --- a/container-core/src/test/java/com/yahoo/restapi/RestApiImplTest.java +++ b/container-core/src/test/java/com/yahoo/restapi/RestApiImplTest.java @@ -1,6 +1,7 @@ // Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.restapi; +import ai.vespa.json.InvalidJsonException; import com.fasterxml.jackson.annotation.JsonProperty; import com.yahoo.container.jdisc.AclMapping; import com.yahoo.container.jdisc.HttpRequestBuilder; @@ -20,6 +21,8 @@ import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.MissingFormatWidthException; +import java.util.NoSuchElementException; import java.util.Set; import static com.yahoo.jdisc.http.HttpRequest.Method; @@ -99,6 +102,23 @@ class RestApiImplTest { } @Test + void chooses_most_specific_exception_mapper() { + RestApi restApi = RestApi.builder() + .addRoute(route("/json").get(ctx -> { throw new InvalidJsonException("oops invalid json"); })) + .addRoute(route("/illegal-argument").get(ctx -> { throw new IllegalArgumentException(); })) + .addRoute(route("/bad-format").get(ctx -> { throw new MissingFormatWidthException(""); })) + .addExceptionMapper(IllegalArgumentException.class, (ctx, exception) -> ErrorResponse.badRequest("oops illegal argument")) + .addExceptionMapper(NoSuchElementException.class, (ctx, exception) -> ErrorResponse.badRequest("oops no such element")) + .addExceptionMapper(RuntimeException.class, (ctx, exception) -> ErrorResponse.internalServerError("oops runtime")) + .addExceptionMapper(MissingFormatWidthException.class, (ctx, exception) -> ErrorResponse.internalServerError("oops bad format width")) + .build(); + // Uses default mapper for `InvalidJsonException` since it's more specific than `IllegalArgumentException` + verifyJsonResponse(restApi, Method.GET, "/json", null, 400, "{\"error-code\":\"BAD_REQUEST\",\"message\":\"oops invalid json\"}"); + verifyJsonResponse(restApi, Method.GET, "/illegal-argument", null, 400, "{\"message\":\"oops illegal argument\", \"error-code\":\"BAD_REQUEST\"}"); + verifyJsonResponse(restApi, Method.GET, "/bad-format", null, 500, "{\"message\":\"oops bad format width\", \"error-code\":\"INTERNAL_SERVER_ERROR\"}"); + } + + @Test void method_handler_can_consume_and_produce_json() { RestApi.HandlerWithRequestEntity<TestEntity, TestEntity> handler = (context, requestEntity) -> requestEntity; RestApi restApi = RestApi.builder() |