Skip to content

Commit 7e03faf

Browse files
committed
Update ProblemException message generation
1 parent a60b00d commit 7e03faf

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

src/main/java/io/github/malczuuu/problem4j/core/ProblemException.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,61 @@
11
package io.github.malczuuu.problem4j.core;
22

3+
import java.io.Serial;
4+
35
public class ProblemException extends RuntimeException {
46

7+
@Serial private static final long serialVersionUID = 1L;
8+
59
private final Problem problem;
610

711
public ProblemException(Problem problem) {
8-
super(emptyIfNull(problem.getDetail()));
12+
super(produceExceptionMessage(problem));
913
this.problem = problem;
1014
}
1115

12-
private static String emptyIfNull(String str) {
13-
return str != null ? str : "";
16+
public ProblemException(String message, Problem problem) {
17+
super(message);
18+
this.problem = problem;
1419
}
1520

1621
public ProblemException(Problem problem, Throwable cause) {
17-
super(emptyIfNull(problem.getDetail()), cause);
22+
super(produceExceptionMessage(problem), cause);
1823
this.problem = problem;
1924
}
2025

26+
public ProblemException(String message, Problem problem, Throwable cause) {
27+
super(message, cause);
28+
this.problem = problem;
29+
}
30+
31+
private static String produceExceptionMessage(Problem problem) {
32+
StringBuilder builder = new StringBuilder();
33+
34+
if (problem.getTitle() != null) {
35+
builder.append(problem.getTitle());
36+
}
37+
38+
if (problem.getDetail() != null) {
39+
if (!builder.isEmpty()) {
40+
builder.append(": ");
41+
}
42+
builder.append(problem.getDetail());
43+
}
44+
45+
if (problem.getStatus() != 0) {
46+
if (!builder.isEmpty()) {
47+
builder.append(" ");
48+
}
49+
builder.append("(code: ").append(problem.getStatus()).append(")");
50+
}
51+
52+
if (builder.isEmpty()) {
53+
return null;
54+
}
55+
56+
return builder.toString();
57+
}
58+
2159
public Problem getProblem() {
2260
return problem;
2361
}

0 commit comments

Comments
 (0)