-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #299: add ErrorExtensionProvider via ServiceLoader to allow custo…
…m GraphQL error `extension` fields; use it for `code` and `exception` (class name)
- Loading branch information
Showing
12 changed files
with
120 additions
and
42 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
server/api/src/main/java/io/smallrye/graphql/api/ErrorExtensionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.smallrye.graphql.api; | ||
|
||
import jakarta.json.JsonValue; | ||
|
||
/** | ||
* To add you own GraphQL error <code>extension</code> fields, you can add your own implementations | ||
* of this class via the {@link java.util.ServiceLoader ServiceLoader} mechanism. | ||
*/ | ||
public interface ErrorExtensionProvider { | ||
String getKey(); | ||
|
||
JsonValue mapValueFrom(Throwable exception); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...ntation/src/main/java/io/smallrye/graphql/execution/error/ErrorCodeExtensionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.smallrye.graphql.execution.error; | ||
|
||
import static java.util.Locale.ROOT; | ||
|
||
import jakarta.json.Json; | ||
import jakarta.json.JsonValue; | ||
|
||
import io.smallrye.graphql.api.ErrorExtensionProvider; | ||
import io.smallrye.graphql.schema.model.ErrorInfo; | ||
import io.smallrye.graphql.spi.config.Config; | ||
|
||
public class ErrorCodeExtensionProvider implements ErrorExtensionProvider { | ||
@Override | ||
public String getKey() { | ||
return Config.ERROR_EXTENSION_CODE; | ||
} | ||
|
||
@Override | ||
public JsonValue mapValueFrom(Throwable exception) { | ||
return Json.createValue(errorCode(exception)); | ||
} | ||
|
||
private String errorCode(Throwable exception) { | ||
ErrorInfo errorInfo = ErrorInfoMap.getErrorInfo(exception.getClass().getName()); | ||
if (errorInfo == null) { | ||
return camelToKebab(exception.getClass().getSimpleName().replaceAll("Exception$", "")); | ||
} else { | ||
return errorInfo.getErrorCode(); | ||
} | ||
} | ||
|
||
private static String camelToKebab(String input) { | ||
return String.join("-", input.split("(?=\\p{javaUpperCase})")) | ||
.toLowerCase(ROOT); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
|
||
/** | ||
* Here we create a mapping of all error info that we know about | ||
* | ||
* | ||
* @author Phillip Kruger ([email protected]) | ||
*/ | ||
public class ErrorInfoMap { | ||
|
@@ -23,10 +23,6 @@ public static void register(Map<String, ErrorInfo> map) { | |
} | ||
} | ||
|
||
public static boolean hasErrorInfo(String className) { | ||
return errorInfoMap.containsKey(className); | ||
} | ||
|
||
public static ErrorInfo getErrorInfo(String className) { | ||
if (errorInfoMap.containsKey(className)) { | ||
return errorInfoMap.get(className); | ||
|
19 changes: 19 additions & 0 deletions
19
...rc/main/java/io/smallrye/graphql/execution/error/ExceptionNameErrorExtensionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.smallrye.graphql.execution.error; | ||
|
||
import jakarta.json.Json; | ||
import jakarta.json.JsonString; | ||
|
||
import io.smallrye.graphql.api.ErrorExtensionProvider; | ||
import io.smallrye.graphql.spi.config.Config; | ||
|
||
public class ExceptionNameErrorExtensionProvider implements ErrorExtensionProvider { | ||
@Override | ||
public String getKey() { | ||
return Config.ERROR_EXTENSION_EXCEPTION; | ||
} | ||
|
||
@Override | ||
public JsonString mapValueFrom(Throwable exception) { | ||
return Json.createValue(exception.getClass().getName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...ation/src/main/resources/META-INF/services/io.smallrye.graphql.api.ErrorExtensionProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
io.smallrye.graphql.execution.error.ErrorCodeExtensionProvider | ||
io.smallrye.graphql.execution.error.ExceptionNameErrorExtensionProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
|
||
/** | ||
* Implements the config for testing | ||
* | ||
* | ||
* @author Phillip Kruger ([email protected]) | ||
*/ | ||
public class TestConfig implements Config { | ||
|
@@ -32,9 +32,9 @@ public LogPayloadOption logPayload() { | |
|
||
@Override | ||
public Optional<List<String>> getErrorExtensionFields() { | ||
return Optional | ||
.of(Arrays.asList(new String[] { "exception", "classification", "code", "description", | ||
"validationErrorType", "queryPath" })); | ||
return Optional.of(Arrays.asList( | ||
"exception", "classification", "code", "description", | ||
"validationErrorType", "queryPath", "test-extension")); | ||
} | ||
|
||
@Override | ||
|
@@ -45,6 +45,7 @@ public boolean isIncludeDirectivesInSchema() { | |
@Override | ||
public <T> T getConfigValue(String key, Class<T> type, T defaultValue) { | ||
if (key.equals(TestEventingService.KEY)) { | ||
//noinspection unchecked | ||
return (T) Boolean.TRUE; | ||
} | ||
return defaultValue; | ||
|
18 changes: 18 additions & 0 deletions
18
...mplementation/src/test/java/io/smallrye/graphql/execution/TestErrorExtensionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.smallrye.graphql.execution; | ||
|
||
import jakarta.json.Json; | ||
import jakarta.json.JsonNumber; | ||
|
||
import io.smallrye.graphql.api.ErrorExtensionProvider; | ||
|
||
public class TestErrorExtensionProvider implements ErrorExtensionProvider { | ||
@Override | ||
public String getKey() { | ||
return "test-extension"; | ||
} | ||
|
||
@Override | ||
public JsonNumber mapValueFrom(Throwable exception) { | ||
return Json.createValue(exception.getClass().getSimpleName().length()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...ation/src/test/resources/META-INF/services/io.smallrye.graphql.api.ErrorExtensionProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.smallrye.graphql.execution.TestErrorExtensionProvider |