Skip to content

Commit bb5a9fb

Browse files
ApplicationProblem and ApplicationException stacktrace message null fixed and dependencies versions upgraded
1 parent 2c0859b commit bb5a9fb

File tree

6 files changed

+107
-41
lines changed

6 files changed

+107
-41
lines changed

README.md

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

5050
## Installation
5151

52-
> **Current version: 1.7** Refer to [Release notes](https://github.com/officiallysingh/spring-boot-problem-handler/releases/tag/1.7) while upgrading
52+
> **Current version: 1.8** Refer to [Release notes](https://github.com/officiallysingh/spring-boot-problem-handler/releases/tag/1.7) while upgrading
5353
5454
Add the `spring-boot-problem-handler` jar to application dependencies. That is all it takes to get a default working
5555
exception handling mechanism in a Spring boot application.
@@ -62,12 +62,12 @@ Maven
6262
<dependency>
6363
<groupId>io.github.officiallysingh</groupId>
6464
<artifactId>spring-boot-problem-handler</artifactId>
65-
<version>1.7</version>
65+
<version>1.8</version>
6666
</dependency>
6767
```
6868
Gradle
6969
```groovy
70-
implementation 'io.github.officiallysingh:spring-boot-problem-handler:1.7'
70+
implementation 'io.github.officiallysingh:spring-boot-problem-handler:1.8'
7171
```
7272

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

pom.xml

Lines changed: 10 additions & 6 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.7</version>
9+
<version>1.8</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>
@@ -32,13 +32,13 @@
3232

3333
<properties>
3434
<java.version>21</java.version>
35-
<spring-boot.version>3.2.0</spring-boot.version>
36-
<spring-cloud.version>2023.0.0</spring-cloud.version>
35+
<spring-boot.version>3.2.5</spring-boot.version>
36+
<spring-cloud.version>2023.0.1</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>
39-
<swagger-request-validator-spring-webmvc.version>2.39.0</swagger-request-validator-spring-webmvc.version>
39+
<swagger-request-validator-spring-webmvc.version>2.40.0</swagger-request-validator-spring-webmvc.version>
4040
<commons-collections4.version>4.4</commons-collections4.version>
41-
<guava.version>32.1.3-jre</guava.version>
41+
<guava.version>33.1.0-jre</guava.version>
4242
<failsafe.version>2.4.4</failsafe.version>
4343

4444
<scm.connection>scm:git:git@github.com:officiallysingh/spring-boot-problem-handler.git</scm.connection>
@@ -49,7 +49,7 @@
4949
<maven-release-plugin.version>3.0.0-M1</maven-release-plugin.version>
5050
<maven-deploy-plugin.version>3.1.1</maven-deploy-plugin.version>
5151

52-
<fmt-maven-plugin.version>2.21.1</fmt-maven-plugin.version>
52+
<fmt-maven-plugin.version>2.23</fmt-maven-plugin.version>
5353
<maven-checkstyle-plugin.version>3.3.0</maven-checkstyle-plugin.version>
5454

5555
<maven.compiler.source>${java.version}</maven.compiler.source>
@@ -165,11 +165,15 @@
165165
<groupId>com.spotify.fmt</groupId>
166166
<artifactId>fmt-maven-plugin</artifactId>
167167
<version>${fmt-maven-plugin.version}</version>
168+
<!-- <configuration>-->
169+
<!-- <skip>true</skip>-->
170+
<!-- </configuration>-->
168171
<executions>
169172
<execution>
170173
<goals>
171174
<goal>format</goal>
172175
</goals>
176+
173177
</execution>
174178
</executions>
175179
</plugin>

src/main/java/com/ksoot/problem/core/ApplicationException.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import jakarta.annotation.Nullable;
44
import java.util.Map;
55
import lombok.Getter;
6-
import org.apache.commons.lang3.StringUtils;
76
import org.springframework.http.HttpStatus;
87
import org.springframework.util.Assert;
98

@@ -22,14 +21,15 @@ public final class ApplicationException extends Exception implements ProblemSupp
2221
private final Problem problem;
2322

2423
private ApplicationException(
24+
final String message,
2525
final HttpStatus status,
2626
final Problem problem,
2727
final String errorKey,
2828
@Nullable final String defaultDetail,
2929
@Nullable final Object[] detailArgs,
3030
@Nullable final ThrowableProblem cause,
3131
@Nullable final Map<String, Object> parameters) {
32-
super(StringUtils.isNotEmpty(defaultDetail) ? defaultDetail : defaultDetail, cause);
32+
super(message, cause);
3333
this.status = status;
3434
this.errorKey = errorKey;
3535
this.problem = problem;
@@ -48,11 +48,26 @@ public static ApplicationException of(
4848
@Nullable final Map<String, Object> parameters) {
4949
Assert.hasText(errorKey, "'errorKey' must not be null or empty");
5050
return new ApplicationException(
51-
status, null, errorKey, defaultDetail, detailArgs, cause, parameters);
51+
ProblemUtils.toMessage(errorKey, defaultDetail, null, cause),
52+
status,
53+
null,
54+
errorKey,
55+
defaultDetail,
56+
detailArgs,
57+
cause,
58+
parameters);
5259
}
5360

5461
public static ApplicationException of(final HttpStatus status, final Problem problem) {
5562
Assert.notNull(problem, "'problem' must not be null");
56-
return new ApplicationException(status, problem, null, null, null, null, null);
63+
return new ApplicationException(
64+
ProblemUtils.toMessage(null, null, problem, null),
65+
status,
66+
problem,
67+
null,
68+
null,
69+
null,
70+
null,
71+
null);
5772
}
5873
}

src/main/java/com/ksoot/problem/core/ApplicationProblem.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ public final class ApplicationProblem extends RuntimeException implements Proble
2121
private final Problem problem;
2222

2323
private ApplicationProblem(
24+
final String message,
2425
final HttpStatus status,
2526
final Problem problem,
2627
final String errorKey,
2728
@Nullable final String defaultDetail,
2829
@Nullable final Object[] detailArgs,
2930
@Nullable final ThrowableProblem cause,
3031
@Nullable final Map<String, Object> parameters) {
31-
super(defaultDetail, cause);
32+
super(message, cause);
3233
Assert.notNull(status, "'status' must not be null");
3334
this.status = status;
3435
this.errorKey = errorKey;
@@ -48,16 +49,39 @@ public static ApplicationProblem of(
4849
@Nullable final Map<String, Object> parameters) {
4950
Assert.hasText(errorKey, "'errorKey' must not be null or empty");
5051
return new ApplicationProblem(
51-
status, null, errorKey, defaultDetail, detailArgs, cause, parameters);
52+
ProblemUtils.toMessage(errorKey, defaultDetail, null, cause),
53+
status,
54+
null,
55+
errorKey,
56+
defaultDetail,
57+
detailArgs,
58+
cause,
59+
parameters);
5260
}
5361

5462
public static ApplicationProblem of(final HttpStatus status, final String errorKey) {
5563
Assert.hasText(errorKey, "'errorKey' must not be null or empty");
56-
return new ApplicationProblem(status, null, errorKey, null, null, null, null);
64+
return new ApplicationProblem(
65+
ProblemUtils.toMessage(errorKey, null, null, null),
66+
status,
67+
null,
68+
errorKey,
69+
null,
70+
null,
71+
null,
72+
null);
5773
}
5874

5975
public static ApplicationProblem of(final HttpStatus status, final Problem problem) {
6076
Assert.notNull(problem, "'problem' must not be null");
61-
return new ApplicationProblem(status, problem, null, problem.getDetail(), null, null, null);
77+
return new ApplicationProblem(
78+
ProblemUtils.toMessage(null, null, problem, null),
79+
status,
80+
problem,
81+
null,
82+
problem.getDetail(),
83+
null,
84+
null,
85+
null);
6286
}
6387
}

src/main/java/com/ksoot/problem/core/ProblemUtils.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package com.ksoot.problem.core;
22

33
import static java.util.Arrays.asList;
4+
import static java.util.stream.Collectors.joining;
45
import static org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation;
56

67
import com.ksoot.problem.spring.config.ProblemBeanRegistry;
78
import jakarta.annotation.Nullable;
89
import java.text.CharacterIterator;
910
import java.text.StringCharacterIterator;
11+
import java.util.Objects;
1012
import java.util.Optional;
13+
import java.util.stream.Stream;
1114
import lombok.experimental.UtilityClass;
1215
import org.apache.commons.lang3.exception.ExceptionUtils;
1316
import org.springframework.http.HttpStatus;
@@ -85,4 +88,20 @@ public static String getStackTrace(final Throwable exception) {
8588
}
8689
return escapedStacktrace.toString();
8790
}
91+
92+
public static String toMessage(
93+
@Nullable final String errorKey,
94+
@Nullable final String defaultDetail,
95+
@Nullable final Problem problem,
96+
@Nullable final Problem cause) {
97+
final Stream<String> parts =
98+
Stream.of(
99+
errorKey,
100+
defaultDetail,
101+
Objects.nonNull(problem) ? Problem.toString(problem) : null,
102+
Objects.nonNull(cause) ? Problem.toString(cause) : null)
103+
.filter(Objects::nonNull);
104+
105+
return parts.collect(joining(", "));
106+
}
88107
}

src/main/java/com/ksoot/problem/spring/advice/application/ApplicationMultiProblemAdviceTrait.java

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,37 @@ default R handleMultiProblem(final MultiProblem exception, final T request) {
3737
exception.getErrors().stream()
3838
.map(
3939
ex -> {
40-
if (ex instanceof Problem problem) {
41-
return problem;
42-
} else if (ex instanceof ProblemSupport problemSupport) {
43-
if (Objects.nonNull(problemSupport.getProblem())) {
44-
return problemSupport.getProblem();
45-
} else {
46-
String errorKey = problemSupport.getErrorKey();
47-
String detailCode = ProblemConstant.DETAIL_CODE_PREFIX + errorKey;
40+
switch (ex) {
41+
case Problem problem -> {
42+
return problem;
43+
}
44+
case ProblemSupport problemSupport -> {
45+
if (Objects.nonNull(problemSupport.getProblem())) {
46+
return problemSupport.getProblem();
47+
} else {
48+
String errorKey = problemSupport.getErrorKey();
49+
String detailCode = ProblemConstant.DETAIL_CODE_PREFIX + errorKey;
4850

51+
return toProblem(
52+
(Throwable) ex,
53+
problemSupport.getStatus(),
54+
errorKey,
55+
Optional.ofNullable(problemSupport.getDefaultDetail())
56+
.orElse(detailCode),
57+
problemSupport.getDetailArgs(),
58+
Optional.ofNullable(problemSupport.getParameters())
59+
.orElse(Collections.emptyMap()));
60+
}
61+
}
62+
case Throwable throwable -> {
4963
return toProblem(
50-
(Throwable) ex,
51-
problemSupport.getStatus(),
52-
errorKey,
53-
Optional.ofNullable(problemSupport.getDefaultDetail())
54-
.orElse(detailCode),
55-
problemSupport.getDetailArgs(),
56-
Optional.ofNullable(problemSupport.getParameters())
57-
.orElse(Collections.emptyMap()));
64+
throwable,
65+
GeneralErrorKey.INTERNAL_SERVER_ERROR,
66+
HttpStatus.INTERNAL_SERVER_ERROR);
5867
}
59-
} else if (ex instanceof Throwable throwable) {
60-
return toProblem(
61-
throwable,
62-
GeneralErrorKey.INTERNAL_SERVER_ERROR,
63-
HttpStatus.INTERNAL_SERVER_ERROR);
64-
} else {
65-
throw new IllegalStateException(
66-
"MultiProblem contain illegal instance: " + ex);
68+
case null, default ->
69+
throw new IllegalStateException(
70+
"MultiProblem contain illegal instance: " + ex);
6771
}
6872
})
6973
.toList());

0 commit comments

Comments
 (0)