Skip to content

Commit

Permalink
Change error.yaml format to use kebab-case (#1368)
Browse files Browse the repository at this point in the history
  • Loading branch information
amorton authored Aug 27, 2024
1 parent 668069a commit 2cb7b05
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
Expand Down Expand Up @@ -70,8 +70,8 @@ public static ErrorConfig getInstance() {
@JsonCreator
public ErrorConfig(
@JsonProperty("snippets") List<Snippet> snippets,
@JsonProperty("request_errors") List<ErrorDetail> requestErrors,
@JsonProperty("server_errors") List<ErrorDetail> serverErrors) {
@JsonProperty("request-errors") List<ErrorDetail> requestErrors,
@JsonProperty("server-errors") List<ErrorDetail> serverErrors) {

// defensive immutable copies because this config can be shared
this.snippets = snippets == null ? List.of() : List.copyOf(snippets);
Expand Down Expand Up @@ -268,11 +268,11 @@ private static ErrorConfig maybeCacheErrorConfig(ErrorConfig errorConfig) {
public static ErrorConfig readFromYamlString(String yaml) throws JsonProcessingException {

// This is only going to happen once at system start, ok to create a new mapper
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
ObjectMapper mapper = new YAMLMapper();
// module for Optional support see
// https://github.com/FasterXML/jackson-modules-java8/tree/2.18/datatypes
mapper.registerModule(new Jdk8Module());
mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
mapper.setPropertyNamingStrategy(PropertyNamingStrategies.KEBAB_CASE);
return mapper.readValue(yaml, ErrorConfig.class);
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/resources/errors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# Where
# * Family: Identifies if the error relates to the client request or the server processing, analogous
# to the 4XX and 5XX HTTP status codes. Supported values are REQUEST or SERVER. In this file they are
# represented by the request_errors and server_errors keys.
# represented by the request-errors and server-errors keys.
# * Scope: Optionally identifies the part of the request or server processing that caused the fault, for example "FILTER"
# when there is a problem in the filter clause. Scope generally map to a concrete APIException class such as
# FilterException.
Expand All @@ -30,11 +30,11 @@
# - name: UPPER_SNAKE_CASE_1
# - body: A string with the text of the snippet, recommend using the `|-` to trim trailing newlines.
#
# "request_errors" and "server_errors" are lists of error objects, for the REQUEST and SERVER family respectively.
# "request-errors" and "server-errors" are lists of error objects, for the REQUEST and SERVER family respectively.
# Each error object has:
# - scope: UPPER_SNAKE_CASE_1
# - code: UPPER_SNAKE_CASE_1
# - http_response_override: (optional) The HTTP status code to return when this error is thrown. If not present, the
# - http-response-override: (optional) The HTTP status code to return when this error is thrown. If not present, the
# default status code is 200 for most things. This is not returned in the error object JSON
# It is used to override the HTTP status code in the response.
# NOTE: NO checking is done to confirm this is a valid HTTP status code.
Expand All @@ -50,7 +50,7 @@ snippets:
body: |-
Please contact support if the issue persists.
request_errors:
request-errors:
- scope: FILTER
code: MULTIPLE_ID_FILTER
title: the title for the error
Expand All @@ -64,7 +64,7 @@ request_errors:
${SNIPPET.CONTACT_SUPPORT}
server_errors:
server-errors:
- scope: DATABASE
code: SERVER_READ_FAILED
title: the title for the error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public void initializeFromResource() throws JsonProcessingException {
public void readErrorsYaml() throws JsonProcessingException {
String yaml =
"""
request_errors:
request-errors:
- scope: TEST_SCOPE_1
code: TEST_ERROR_ID_1
title: the title for the error
http_response_override: 501
http-response-override: 501
body: |-
big long body with ${vars} in it
- scope:
Expand All @@ -55,7 +55,7 @@ public void readErrorsYaml() throws JsonProcessingException {
Line 1 of body
Line 2 of body
server_errors:
server-errors:
- scope: TEST_SCOPE_3
code: TEST_ERROR_ID_2
title: the title for the error
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/invalid_exception_errors.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# For use when testing the exception classes that do not have ctor or break etc.

request_errors:
request-errors:
- scope: MISSING_CTOR
code: EXCEPTION_MISSING_CTOR
title: test for exception class missing the ctor needed for templating
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/test_errors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ snippets:
body: |-
Please contact support if the issue persists.
request_errors:
request-errors:
- scope: TEST_REQUEST_SCOPE
code: SCOPED_REQUEST_ERROR
title: A scoped request error
Expand Down Expand Up @@ -34,7 +34,7 @@ request_errors:
- scope:
code: HTTP_OVERRIDE
http_response_override: 500
http-response-override: 500
title: An error that overrides the HTTP response
body: |-
body with ${name} and ${value} in it

0 comments on commit 2cb7b05

Please sign in to comment.