Skip to content

Commit 4c6dd2a

Browse files
updated
1 parent 89de4df commit 4c6dd2a

File tree

6 files changed

+60
-28
lines changed

6 files changed

+60
-28
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ all can be done with zero custom code but by specifying error details in `proper
2525

2626
## Installation
2727

28-
> **Current version: 1.6** Refer to [Release notes](https://github.com/officiallysingh/spring-boot-problem-handler/releases/tag/1.5) while upgrading
28+
> **Current version: 1.7** Refer to [Release notes](https://github.com/officiallysingh/spring-boot-problem-handler/releases/tag/1.7) while upgrading
2929
3030
Add the `spring-boot-problem-handler` jar to application dependencies. That is all it takes to get a default working
3131
exception handling mechanism in a Spring boot application.
@@ -35,12 +35,12 @@ Maven
3535
<dependency>
3636
<groupId>io.github.officiallysingh</groupId>
3737
<artifactId>spring-boot-problem-handler</artifactId>
38-
<version>1.6</version>
38+
<version>1.7</version>
3939
</dependency>
4040
```
4141
Gradle
4242
```groovy
43-
implementation 'io.github.officiallysingh:spring-boot-problem-handler:1.6'
43+
implementation 'io.github.officiallysingh:spring-boot-problem-handler:1.7'
4444
```
4545

4646
It does all hard part, A lot of advices are out of box available which are autoconfigured as `ControllerAdvice`s

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.officiallysingh</groupId>
88
<artifactId>spring-boot-problem-handler</artifactId>
9-
<version>1.6</version>
9+
<version>1.7</version>
1010
<name>spring-boot-problem-handler</name>
1111
<description>Commons exception handler library</description>
1212
<url>https://github.com/officiallysingh/spring-boot-problem-handler</url>
@@ -33,7 +33,7 @@
3333
<properties>
3434
<java.version>21</java.version>
3535
<spring-boot.version>3.2.0</spring-boot.version>
36-
<spring-cloud.version>2022.0.4</spring-cloud.version>
36+
<spring-cloud.version>2023.0.0</spring-cloud.version>
3737
<jakarta.servlet-api.version>6.0.0</jakarta.servlet-api.version>
3838
<jakarta.annotation-api.version>2.1.1</jakarta.annotation-api.version>
3939
<swagger-request-validator-spring-webmvc.version>2.39.0</swagger-request-validator-spring-webmvc.version>

src/main/java/com/ksoot/problem/spring/advice/AdviceTrait.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,27 @@ public interface AdviceTrait<T, R> {
3030

3131
Logger logger = LoggerFactory.getLogger(AdviceTrait.class);
3232

33-
// ------ Create problem from exceptions ------
34-
default Problem toProblem(
35-
final Throwable throwable, final String defaultErrorKey, final HttpStatus defaultStatus) {
36-
33+
default HttpStatus resolveStatus(final Throwable throwable) {
34+
HttpStatus defaultStatus = HttpStatus.valueOf(ProblemUtils.resolveStatus(throwable).value());
3735
String errorKey = ClassUtils.getName(throwable);
38-
39-
ProblemMessageSourceResolver statusResolver =
36+
ProblemMessageSourceResolver defaultStatusResolver =
4037
ProblemMessageSourceResolver.of(
4138
ProblemConstant.STATUS_CODE_PREFIX + errorKey, defaultStatus.value());
4239
HttpStatus status = defaultStatus;
4340
try {
44-
String statusCode = ProblemMessageProvider.getMessage(statusResolver);
41+
int statusCode = Integer.parseInt(ProblemMessageProvider.getMessage(defaultStatusResolver));
4542
status = HttpStatus.valueOf(statusCode);
4643
} catch (final Exception e) {
4744
// Ignore on purpose
4845
}
46+
return status;
47+
}
48+
49+
// ------ Create problem from exceptions ------
50+
default Problem toProblem(
51+
final Throwable throwable, final String defaultErrorKey, final HttpStatus status) {
52+
53+
String errorKey = ClassUtils.getName(throwable);
4954

5055
ProblemMessageSourceResolver defaultCodeResolver =
5156
ProblemMessageSourceResolver.of(
@@ -69,6 +74,9 @@ default Problem toProblem(
6974
ProblemMessageSourceResolver.of(
7075
ProblemConstant.DETAIL_CODE_PREFIX + errorKey,
7176
ProblemMessageProvider.getMessage(defaultDetailResolver));
77+
ProblemMessageSourceResolver statusResolver =
78+
ProblemMessageSourceResolver.of(
79+
ProblemConstant.STATUS_CODE_PREFIX + errorKey, status.value());
7280

7381
Problem problem =
7482
toProblem(throwable, codeResolver, titleResolver, detailResolver, statusResolver);
@@ -177,7 +185,7 @@ default ThrowableProblem toProblem(
177185
final MessageSourceResolvable codeResolver,
178186
final MessageSourceResolvable titleResolver,
179187
final MessageSourceResolvable detailResolver,
180-
final ProblemMessageSourceResolver statusResolver) {
188+
final MessageSourceResolvable statusResolver) {
181189
Map<String, Object> parameters = new LinkedHashMap<>();
182190
if (ProblemBeanRegistry.problemProperties().isDebugEnabled()) {
183191
parameters.put(CODE_RESOLVER, codeResolver);

src/main/java/com/ksoot/problem/spring/advice/general/ThrowableAdviceTrait.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.ksoot.problem.core.GeneralErrorKey;
44
import com.ksoot.problem.core.Problem;
5-
import com.ksoot.problem.core.ProblemUtils;
65
import com.ksoot.problem.spring.advice.AdviceTrait;
76
import org.springframework.http.HttpStatus;
87
import org.springframework.web.bind.annotation.ExceptionHandler;
@@ -15,7 +14,7 @@ public interface ThrowableAdviceTrait<T, R> extends AdviceTrait<T, R> {
1514

1615
@ExceptionHandler
1716
default R handleThrowable(final Throwable throwable, final T request) {
18-
HttpStatus status = HttpStatus.valueOf(ProblemUtils.resolveStatus(throwable).value());
17+
HttpStatus status = resolveStatus(throwable);
1918
Problem problem = toProblem(throwable, GeneralErrorKey.INTERNAL_SERVER_ERROR, status);
2019
return toResponse(throwable, request, status, problem);
2120
}

src/main/java/com/ksoot/problem/spring/config/ProblemProperties.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,31 @@
1919
@ConfigurationProperties(prefix = "problem")
2020
public class ProblemProperties {
2121

22+
/** Default: true, Whether or not to enable Problem handling. */
2223
private boolean enabled = true;
2324

24-
private String typeUrl;
25+
/** Default: http://localhost:8080/problems/help.html, Help page base url. */
26+
private String typeUrl = "http://localhost:8080/problems/help.html";
2527

28+
/**
29+
* Default: false, Whether or not to include debug-info such as message codes etc. in error
30+
* response messages.
31+
*/
2632
private boolean debugEnabled = false;
2733

34+
/** Default: false, Whether or not to include stacktrace in error response messages. */
2835
private boolean stacktraceEnabled = false;
2936

37+
/** Default: false, Whether or not to include exception cause in error response messages. */
3038
private boolean causeChainsEnabled = false;
3139

40+
/** Default: true, Whether or not to Enable Jackson Problem module. */
3241
private boolean jacksonModuleEnabled = true;
3342

43+
/** Default: true, Whether or not to Enable DAO exception handling advices. */
3444
private boolean daoAdviceEnabled = true;
3545

46+
/** Default: true, Whether or not to Enable Security exception handling advices. */
3647
private boolean securityAdviceEnabled = true;
3748

3849
private OpenApi openApi = new OpenApi();
@@ -44,12 +55,25 @@ public class ProblemProperties {
4455
@Valid
4556
public static class OpenApi {
4657

58+
/** Default: /oas/api.json, Path of API Specification json file. */
4759
private String path = "/oas/api.json";
4860

61+
/**
62+
* Default: None. List of path patterns in ant-pattern format to exclude from OpenAPI
63+
* Specification validation.
64+
*/
4965
private List<String> excludePatterns = new ArrayList<>();
5066

67+
/**
68+
* Default: true, Whether or not to enable Open API request validation.</br>While enabling make
69+
* sure Problem is also enabled.
70+
*/
5171
private boolean reqValidationEnabled = false;
5272

73+
/**
74+
* Default: false, Whether or not to enable Open API response validation.</br>While enabling
75+
* make sure Problem is also enabled.
76+
*/
5377
private boolean resValidationEnabled = false;
5478
}
5579
}

src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,72 @@
44
"name": "problem.enabled",
55
"type": "java.lang.Boolean",
66
"defaultValue": "true",
7-
"description": "Default: true, Whether or not to enable Problem handling"
7+
"description": "Default: true, Whether or not to enable Problem handling."
88
},
99
{
1010
"name": "problem.type-url",
1111
"type": "java.lang.String",
12-
"description": "Help page base url"
12+
"defaultValue": "http://localhost:8080/problems/help.html",
13+
"description": "Default: http://localhost:8080/problems/help.html, Help page base url."
1314
},
1415
{
1516
"name": "problem.debug-enabled",
1617
"type": "java.lang.Boolean",
1718
"defaultValue": "false",
18-
"description": "Default: false, Whether or not to include debug-info such as message codes etc. in error response messages"
19+
"description": "Default: false, Whether or not to include debug-info such as message codes etc. in error response messages."
1920
},
2021
{
2122
"name": "problem.stacktrace-enabled",
2223
"type": "java.lang.Boolean",
2324
"defaultValue": "false",
24-
"description": "Default: false, Whether or not to include stacktrace in error response messages"
25+
"description": "Default: false, Whether or not to include stacktrace in error response messages."
2526
},
2627
{
2728
"name": "problem.cause-chains-enabled",
2829
"type": "java.lang.Boolean",
2930
"defaultValue": "false",
30-
"description": "Default: false, Whether or not to include exception cause in error response messages"
31+
"description": "Default: false, Whether or not to include exception cause in error response messages."
3132
},
3233
{
3334
"name": "problem.jackson-module-enabled",
3435
"type": "java.lang.Boolean",
3536
"defaultValue": "true",
36-
"description": "Default: true, Whether or not to Enable Jackson Problem module"
37+
"description": "Default: true, Whether or not to Enable Jackson Problem module."
3738
},
3839
{
3940
"name": "problem.dao-advice-enabled",
4041
"type": "java.lang.Boolean",
4142
"defaultValue": "true",
42-
"description": "Default: true, Whether or not to Enable DAO exception handling advices"
43+
"description": "Default: true, Whether or not to Enable DAO exception handling advices."
4344
},
4445
{
4546
"name": "problem.security-advice-enabled",
4647
"type": "java.lang.Boolean",
4748
"defaultValue": "true",
48-
"description": "Default: true, Whether or not to Enable Security exception handling advices"
49+
"description": "Default: true, Whether or not to Enable Security exception handling advices."
4950
},
5051
{
5152
"name": "problem.open-api.path",
5253
"type": "java.lang.String",
5354
"defaultValue": "/oas/api.json",
54-
"description": "Default: /oas/api.json, Path of API Specification json file"
55+
"description": "Default: /oas/api.json, Path of API Specification json file."
5556
},
5657
{
5758
"name": "problem.open-api.exclude-patterns",
5859
"type": "java.lang.String",
59-
"description": "Default: None. List of path patterns in ant-pattern format to exclude from OpenAPI Specification validation"
60+
"description": "Default: None. List of path patterns in ant-pattern format to exclude from OpenAPI Specification validation."
6061
},
6162
{
6263
"name": "problem.open-api.req-validation-enabled",
6364
"type": "java.lang.Boolean",
6465
"defaultValue": "true",
65-
"description": "Default: true, Whether or not to enable Open API request validation.</br>While enabling make sure Problem is also enabled"
66+
"description": "Default: true, Whether or not to enable Open API request validation.</br>While enabling make sure Problem is also enabled."
6667
},
6768
{
6869
"name": "problem.open-api.res-validation-enabled",
6970
"type": "java.lang.Boolean",
7071
"defaultValue": "false",
71-
"description": "Default: false, Whether or not to enable Open API response validation.</br>While enabling make sure Problem is also enabled"
72+
"description": "Default: false, Whether or not to enable Open API response validation.</br>While enabling make sure Problem is also enabled."
7273
}
7374
]
7475
}

0 commit comments

Comments
 (0)