55 */
66package io .jooby .problem ;
77
8- import edu .umd .cs .findbugs .annotations .Nullable ;
9- import io .jooby .StatusCode ;
10-
118import java .net .URI ;
129import java .time .Instant ;
1310import 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 */
2422public 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 }
0 commit comments