Skip to content

Commit 9e1054f

Browse files
committed
source: apply formatter + some minor changes on problem implementation
- rename `fromConfig` to `from` and always require the full path `problem.details` - remove `Internal` from title and description - adjust tests
1 parent dd300c2 commit 9e1054f

32 files changed

+810
-681
lines changed

jooby/src/main/java/io/jooby/Jooby.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import java.util.function.Supplier;
4242
import java.util.stream.Collectors;
4343

44-
import io.jooby.problem.ProblemDetailsHandler;
4544
import org.slf4j.Logger;
4645
import org.slf4j.LoggerFactory;
4746

@@ -55,6 +54,7 @@
5554
import io.jooby.internal.MutedServer;
5655
import io.jooby.internal.RegistryRef;
5756
import io.jooby.internal.RouterImpl;
57+
import io.jooby.problem.ProblemDetailsHandler;
5858
import jakarta.inject.Provider;
5959

6060
/**
@@ -1333,7 +1333,7 @@ public static Jooby createApp(
13331333
public boolean problemDetailsIsEnabled() {
13341334
var config = getConfig();
13351335
return config.hasPath(ProblemDetailsHandler.ENABLED_KEY)
1336-
&& config.getBoolean(ProblemDetailsHandler.ENABLED_KEY);
1336+
&& config.getBoolean(ProblemDetailsHandler.ENABLED_KEY);
13371337
}
13381338

13391339
private static void configurePackage(Package pkg) {

jooby/src/main/java/io/jooby/exception/InvalidCsrfToken.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ public InvalidCsrfToken(@Nullable String token) {
2828

2929
@Override
3030
public @NonNull HttpProblem toHttpProblem() {
31-
return HttpProblem.valueOf(statusCode,
32-
"Invalid CSRF token",
33-
"CSRF token '" + getMessage() + "' is invalid");
31+
return HttpProblem.valueOf(
32+
statusCode, "Invalid CSRF token", "CSRF token '" + getMessage() + "' is invalid");
3433
}
3534
}

jooby/src/main/java/io/jooby/exception/MethodNotAllowedException.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public List<String> getAllow() {
5252

5353
@Override
5454
public @NonNull HttpProblem toHttpProblem() {
55-
return HttpProblem.valueOf(statusCode,
55+
return HttpProblem.valueOf(
56+
statusCode,
5657
statusCode.reason(),
57-
"HTTP method '" + getMethod() + "' is not allowed. Allowed methods are: " + allow
58-
);
58+
"HTTP method '" + getMethod() + "' is not allowed. Allowed methods are: " + allow);
5959
}
6060
}

jooby/src/main/java/io/jooby/exception/NotAcceptableException.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ public NotAcceptableException(@Nullable String contentType) {
3737

3838
@Override
3939
public @NonNull HttpProblem toHttpProblem() {
40-
return HttpProblem.valueOf(statusCode,
40+
return HttpProblem.valueOf(
41+
statusCode,
4142
statusCode.reason(),
42-
"Server cannot produce a response matching the list of " +
43-
"acceptable values defined in the request's 'Accept' header"
44-
);
43+
"Server cannot produce a response matching the list of "
44+
+ "acceptable values defined in the request's 'Accept' header");
4545
}
4646
}

jooby/src/main/java/io/jooby/exception/NotFoundException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public NotFoundException(@NonNull String path) {
3737

3838
@Override
3939
public @NonNull HttpProblem toHttpProblem() {
40-
return HttpProblem.valueOf(statusCode,
40+
return HttpProblem.valueOf(
41+
statusCode,
4142
statusCode.reason(),
4243
"Route '" + getRequestPath() + "' not found. Please verify request path");
4344
}

jooby/src/main/java/io/jooby/exception/TypeMismatchException.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public TypeMismatchException(@NonNull String name, @NonNull Type type) {
5050
return name;
5151
}
5252

53-
5453
@Override
5554
public @NonNull HttpProblem toHttpProblem() {
5655
return HttpProblem.valueOf(statusCode, "Type Mismatch", getMessage());

jooby/src/main/java/io/jooby/exception/UnsupportedMediaType.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ public UnsupportedMediaType(@Nullable String type) {
3737

3838
@Override
3939
public @NonNull HttpProblem toHttpProblem() {
40-
return HttpProblem.valueOf(statusCode,
41-
statusCode.reason(),
42-
"Media type '" + getContentType() + "' is not supported");
40+
return HttpProblem.valueOf(
41+
statusCode, statusCode.reason(), "Media type '" + getContentType() + "' is not supported");
4342
}
4443
}

jooby/src/main/java/io/jooby/internal/RouterImpl.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import java.util.stream.IntStream;
3636
import java.util.stream.Stream;
3737

38-
import io.jooby.problem.ProblemDetailsHandler;
3938
import org.slf4j.Logger;
4039
import org.slf4j.LoggerFactory;
4140

@@ -48,6 +47,7 @@
4847
import io.jooby.exception.StatusCodeException;
4948
import io.jooby.internal.handler.ServerSentEventHandler;
5049
import io.jooby.internal.handler.WebSocketHandler;
50+
import io.jooby.problem.ProblemDetailsHandler;
5151
import jakarta.inject.Provider;
5252

5353
public class RouterImpl implements Router {
@@ -668,18 +668,16 @@ private void pureAscii(String pattern, Consumer<String> consumer) {
668668
}
669669

670670
/**
671-
* Define the global error handler.
672-
* If ProblemDetails is enabled the {@link ProblemDetailsHandler} instantiated
673-
* from configuration settings and returned. Otherwise, {@link DefaultErrorHandler} instance
674-
* returned.
671+
* Define the global error handler. If ProblemDetails is enabled the {@link ProblemDetailsHandler}
672+
* instantiated from configuration settings and returned. Otherwise, {@link DefaultErrorHandler}
673+
* instance returned.
675674
*
676675
* @param app - Jooby application instance
677676
* @return global error handler
678677
*/
679678
private ErrorHandler defineGlobalErrorHandler(Jooby app) {
680679
if (app.problemDetailsIsEnabled()) {
681-
var problemDetailsConfig = app.getConfig().getConfig(ProblemDetailsHandler.ROOT_CONFIG_PATH);
682-
return ProblemDetailsHandler.fromConfig(problemDetailsConfig);
680+
return ProblemDetailsHandler.from(app.getConfig());
683681
} else {
684682
return ErrorHandler.create();
685683
}

jooby/src/main/java/io/jooby/problem/HttpProblem.java

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,25 @@
55
*/
66
package io.jooby.problem;
77

8-
import edu.umd.cs.findbugs.annotations.Nullable;
9-
import io.jooby.StatusCode;
10-
118
import java.net.URI;
129
import java.time.Instant;
1310
import java.util.*;
1411

12+
import edu.umd.cs.findbugs.annotations.Nullable;
13+
import io.jooby.StatusCode;
14+
1515
/**
16-
* Representation of
17-
* <a href="https://www.rfc-editor.org/rfc/rfc7807">RFC7807</a>
18-
* <a href="https://www.rfc-editor.org/rfc/rfc9457">RFC9457</a>
19-
* Problem schema.
16+
* Representation of <a href="https://www.rfc-editor.org/rfc/rfc7807">RFC7807</a> <a
17+
* href="https://www.rfc-editor.org/rfc/rfc9457">RFC9457</a> Problem schema.
2018
*
2119
* @author kliushnichenko
2220
* @since 3.4.2
2321
*/
2422
public class HttpProblem extends RuntimeException {
2523

26-
private static final String INTERNAL_ERROR_TITLE = "Internal Server Error";
24+
private static final String INTERNAL_ERROR_TITLE = StatusCode.SERVER_ERROR.reason();
2725
private static final String INTERNAL_ERROR_DETAIL =
28-
"The server encountered an internal error or misconfiguration and was unable " +
29-
"to complete your request";
26+
"The server encountered an error or misconfiguration and was unable to complete your request";
3027

3128
private static final URI DEFAULT_TYPE = URI.create("about:blank");
3229

@@ -59,25 +56,15 @@ private static String createMessage(String title, String detail) {
5956
}
6057

6158
public static HttpProblem valueOf(StatusCode status, String title) {
62-
return builder()
63-
.title(title)
64-
.status(status)
65-
.build();
59+
return builder().title(title).status(status).build();
6660
}
6761

6862
public static HttpProblem valueOf(StatusCode status, String title, String detail) {
69-
return builder()
70-
.title(title)
71-
.status(status)
72-
.detail(detail)
73-
.build();
63+
return builder().title(title).status(status).detail(detail).build();
7464
}
7565

7666
public static HttpProblem valueOf(StatusCode status) {
77-
return builder()
78-
.title(status.reason())
79-
.status(status)
80-
.build();
67+
return builder().title(status.reason()).status(status).build();
8168
}
8269

8370
public static HttpProblem internalServerError() {
@@ -102,13 +89,13 @@ public String getTimestamp() {
10289
}
10390

10491
/**
105-
* A URI reference that identifies the problem type. Consumers MUST use the "type" URI
106-
* (after resolution, if necessary) as the problem type's primary identifier.
107-
* When this member is not present, its value is assumed to be "about:blank".
108-
* If the type URI is a locator (e.g., those with a "http" or "https" scheme), de-referencing it
109-
* SHOULD provide human-readable documentation for the problem type (e.g., using HTML).
110-
* However, consumers SHOULD NOT automatically dereference the type URI, unless they do so
111-
* when providing information to developers (e.g., when a debugging tool is in use)
92+
* A URI reference that identifies the problem type. Consumers MUST use the "type" URI (after
93+
* resolution, if necessary) as the problem type's primary identifier. When this member is not
94+
* present, its value is assumed to be "about:blank". If the type URI is a locator (e.g., those
95+
* with a "http" or "https" scheme), de-referencing it SHOULD provide human-readable documentation
96+
* for the problem type (e.g., using HTML). However, consumers SHOULD NOT automatically
97+
* dereference the type URI, unless they do so when providing information to developers (e.g.,
98+
* when a debugging tool is in use)
11299
*
113100
* @return a URI that identifies this problem's type
114101
*/
@@ -117,9 +104,8 @@ public URI getType() {
117104
}
118105

119106
/**
120-
* A short, human-readable summary of the problem type. It SHOULD NOT
121-
* change from occurrence to occurrence of the problem, except for
122-
* purposes of localization.
107+
* A short, human-readable summary of the problem type. It SHOULD NOT change from occurrence to
108+
* occurrence of the problem, except for purposes of localization.
123109
*
124110
* @return a short, human-readable summary of this problem
125111
*/
@@ -128,8 +114,7 @@ public String getTitle() {
128114
}
129115

130116
/**
131-
* The HTTP status code generated by the origin server for this
132-
* occurrence of the problem.
117+
* The HTTP status code generated by the origin server for this occurrence of the problem.
133118
*
134119
* @return the HTTP status code
135120
*/
@@ -147,8 +132,8 @@ public int getStatus() {
147132
}
148133

149134
/**
150-
* A URI that identifies the specific occurrence of the problem.
151-
* It may or may not yield further information if de-referenced.
135+
* A URI that identifies the specific occurrence of the problem. It may or may not yield further
136+
* information if de-referenced.
152137
*
153138
* @return an absolute URI that identifies this specific problem
154139
*/
@@ -183,16 +168,25 @@ public boolean hasErrors() {
183168

184169
@Override
185170
public String toString() {
186-
return "HttpProblem{" +
187-
"timestamp='" + timestamp + '\'' +
188-
", type=" + type +
189-
", title='" + title + '\'' +
190-
", status=" + status +
191-
", detail='" + detail + '\'' +
192-
", instance=" + instance +
193-
(hasErrors() ? ", errors=" + errors : "") +
194-
(hasParameters() ? ", parameters=" + parameters : "") +
195-
'}';
171+
return "HttpProblem{"
172+
+ "timestamp='"
173+
+ timestamp
174+
+ '\''
175+
+ ", type="
176+
+ type
177+
+ ", title='"
178+
+ title
179+
+ '\''
180+
+ ", status="
181+
+ status
182+
+ ", detail='"
183+
+ detail
184+
+ '\''
185+
+ ", instance="
186+
+ instance
187+
+ (hasErrors() ? ", errors=" + errors : "")
188+
+ (hasParameters() ? ", parameters=" + parameters : "")
189+
+ '}';
196190
}
197191

198192
public static class Builder {
@@ -205,8 +199,7 @@ public static class Builder {
205199
private final Map<String, Object> headers = new LinkedHashMap<>();
206200
private final List<Error> errors = new LinkedList<>();
207201

208-
Builder() {
209-
}
202+
Builder() {}
210203

211204
public Builder type(@Nullable final URI type) {
212205
this.type = type;
@@ -249,7 +242,7 @@ public Builder errors(final List<? extends Error> errors) {
249242
}
250243

251244
/**
252-
* @param key additional info parameter name
245+
* @param key additional info parameter name
253246
* @param value additional info parameter value
254247
* @return this for chaining
255248
*/
@@ -266,8 +259,11 @@ public HttpProblem build() {
266259
throw new RuntimeException("The problem 'status' should be specified");
267260
}
268261
if (this.status < 400) {
269-
throw new RuntimeException("Illegal status code " + this.status + ". " +
270-
"Problem details designed to serve 4xx and 5xx status codes");
262+
throw new RuntimeException(
263+
"Illegal status code "
264+
+ this.status
265+
+ ". "
266+
+ "Problem details designed to serve 4xx and 5xx status codes");
271267
}
272268
return new HttpProblem(this);
273269
}

jooby/src/main/java/io/jooby/problem/HttpProblemMappable.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
import edu.umd.cs.findbugs.annotations.NonNull;
99

1010
/**
11-
* Implementing {@link HttpProblemMappable} allows to control the transformation of exception
12-
* into {@link HttpProblem}.
13-
* {@link ProblemDetailsHandler} rely on `toHttpProblem()` method when it is available.
11+
* Implementing {@link HttpProblemMappable} allows to control the transformation of exception into
12+
* {@link HttpProblem}. {@link ProblemDetailsHandler} rely on `toHttpProblem()` method when it is
13+
* available.
1414
*
1515
* @author kliushnichenko
1616
* @since 3.4.2
1717
*/
1818
public interface HttpProblemMappable {
19-
@NonNull
20-
HttpProblem toHttpProblem();
19+
@NonNull HttpProblem toHttpProblem();
2120
}

0 commit comments

Comments
 (0)