From 37606fedbab5779090a479725069dbd1240f7986 Mon Sep 17 00:00:00 2001 From: Urban Malc Date: Fri, 5 Feb 2021 10:03:15 +0100 Subject: [PATCH] Added kumuluzee-rest exceptions to show-error-message list by default --- README.md | 22 ++++++++++++++++++- .../mp/config/KumuluzConfigMapper.java | 20 ++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 74b494b..597cac3 100644 --- a/README.md +++ b/README.md @@ -194,7 +194,7 @@ public class CustomerResource { Exceptions can be thrown during query/mutation execution. The response will have the structure of the GraphQL error as defined in the GraphQL specification. -By default, all messages from unchecked exceptions will be hidden for security reasons. You can override this behavior +By default, all messages from unchecked exceptions (except some defaults, see below) will be hidden for security reasons. You can override this behavior with the configuration key `kumuluzee.graphql.exceptions.show-error-message`. The message will be replaced with `Server Error` and can be set using the configuration key `kumuluzee.graphql.exceptions.default-error-message`. By default, all messages from checked exceptions will be shown. You can hide messages from exceptions with the @@ -211,6 +211,26 @@ kumuluzee: default-error-message: Server error, for more information contact ustomer service. ``` +#### `show-error-message` defaults + +To provide a more seamless integration with __kumuluzee-rest__, some exceptions are added to `show-error-message` +list by default, namely: + +- com.kumuluz.ee.rest.exceptions.InvalidEntityFieldException +- com.kumuluz.ee.rest.exceptions.InvalidFieldValueException +- com.kumuluz.ee.rest.exceptions.NoGenericTypeException +- com.kumuluz.ee.rest.exceptions.NoSuchEntityFieldException +- com.kumuluz.ee.rest.exceptions.QueryFormatException + +To disable these defaults and handle everything manually use the following configuration: + +```yaml +kumuluzee: + graphql: + exceptions: + include-show-error-defaults: false +``` + ## Querying GraphQL endpoint GraphQL endpoint (`/graphql`) should be queried using a POST request. Request body should be a JSON object containing diff --git a/microprofile/src/main/java/com/kumuluz/ee/graphql/mp/config/KumuluzConfigMapper.java b/microprofile/src/main/java/com/kumuluz/ee/graphql/mp/config/KumuluzConfigMapper.java index 1dff959..613db71 100644 --- a/microprofile/src/main/java/com/kumuluz/ee/graphql/mp/config/KumuluzConfigMapper.java +++ b/microprofile/src/main/java/com/kumuluz/ee/graphql/mp/config/KumuluzConfigMapper.java @@ -40,6 +40,15 @@ public class KumuluzConfigMapper implements ConfigurationSource { private static final Map CONFIG_MAP = new HashMap<>(); private static final Map CONFIG_MAP_LIST = new HashMap<>(); + private static final String SHOW_ERROR_DEFAULTS_CONFIG_KEY = + "kumuluzee.graphql.exceptions.include-show-error-defaults"; + private static final String[] SHOW_ERROR_DEFAULTS = new String[] { + "com.kumuluz.ee.rest.exceptions.InvalidEntityFieldException", + "com.kumuluz.ee.rest.exceptions.InvalidFieldValueException", + "com.kumuluz.ee.rest.exceptions.NoGenericTypeException", + "com.kumuluz.ee.rest.exceptions.NoSuchEntityFieldException", + "com.kumuluz.ee.rest.exceptions.QueryFormatException", + }; static { CONFIG_MAP.put(ConfigKey.DEFAULT_ERROR_MESSAGE, "kumuluzee.graphql.exceptions.default-error-message"); @@ -81,7 +90,16 @@ public Optional get(String s) { mappedKey = CONFIG_MAP_LIST.get(s); if (mappedKey != null) { - return configurationUtil.getList(mappedKey).map(ls -> String.join(",", ls)); + Optional returnValue = configurationUtil.getList(mappedKey).map(ls -> String.join(",", ls)); + + if ("kumuluzee.graphql.exceptions.show-error-message".equals(mappedKey) && + configurationUtil.getBoolean(SHOW_ERROR_DEFAULTS_CONFIG_KEY).orElse(true)) { + + String defaults = String.join(",", SHOW_ERROR_DEFAULTS); + returnValue = Optional.of(defaults + returnValue.map(v -> "," + v).orElse("")); + } + + return returnValue; } return Optional.empty();