From b107fe16cf50d5ffde3499667461103275725e83 Mon Sep 17 00:00:00 2001 From: Bjørn Christian Seime Date: Tue, 27 Feb 2024 08:05:51 +0100 Subject: Handle `InvalidJsonException` by default. Fix mapper ordering --- container-core/src/main/java/com/yahoo/restapi/RestApiImpl.java | 6 +++++- container-core/src/main/java/com/yahoo/restapi/RestApiMappers.java | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'container-core/src/main/java/com/yahoo') diff --git a/container-core/src/main/java/com/yahoo/restapi/RestApiImpl.java b/container-core/src/main/java/com/yahoo/restapi/RestApiImpl.java index 090e06c221f..e5aeb33d45d 100644 --- a/container-core/src/main/java/com/yahoo/restapi/RestApiImpl.java +++ b/container-core/src/main/java/com/yahoo/restapi/RestApiImpl.java @@ -211,7 +211,11 @@ class RestApiImpl implements RestApi { exceptionMappers.addAll(RestApiMappers.DEFAULT_EXCEPTION_MAPPERS); } // Topologically sort children before superclasses, so most the specific match is found by iterating through mappers in order. - exceptionMappers.sort((a, b) -> (a.type.isAssignableFrom(b.type) ? 1 : 0) + (b.type.isAssignableFrom(a.type) ? -1 : 0)); + exceptionMappers.sort((l, r) -> { + if (l.type.equals(r.type)) return 0; + if (l.type.isAssignableFrom(r.type)) return 1; + return -1; + }); return exceptionMappers; } diff --git a/container-core/src/main/java/com/yahoo/restapi/RestApiMappers.java b/container-core/src/main/java/com/yahoo/restapi/RestApiMappers.java index 62b46d26ba9..6d785ba58bc 100644 --- a/container-core/src/main/java/com/yahoo/restapi/RestApiMappers.java +++ b/container-core/src/main/java/com/yahoo/restapi/RestApiMappers.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 ai.vespa.json.Json; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @@ -52,6 +53,7 @@ public class RestApiMappers { (context, entity) -> new JacksonJsonResponse<>(200, entity, context.jacksonJsonMapper(), true))); static List> DEFAULT_EXCEPTION_MAPPERS = List.of( + new ExceptionMapperHolder<>(InvalidJsonException.class, (ctx, e) -> ErrorResponse.badRequest(e.getMessage())), new ExceptionMapperHolder<>(RestApiException.class, (context, exception) -> exception.response())); private RestApiMappers() {} -- cgit v1.2.3