You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/asciidoc/problem-details.adoc
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
== Problem Details
1
+
===Problem Details
2
2
3
3
Most APIs have a way to report problems and errors, helping the user understand when something went wrong and what the issue is.
4
4
The method used depends on the API’s style, technology, and design.
@@ -12,7 +12,7 @@ If it suits the API’s needs, using this standard benefits both designers and u
12
12
13
13
`Jooby` provides built-in support for `Problem Details`.
14
14
15
-
=== Set up ProblemDetails
15
+
====Set up ProblemDetails
16
16
17
17
To enable the `ProblemDetails`, simply add the following line to your configuration:
18
18
@@ -44,11 +44,11 @@ problem.details {
44
44
<3> You can optionally mute some exceptions logging completely.
45
45
46
46
47
-
=== Creating problems
47
+
==== Creating problems
48
48
49
49
`HttpProblem` class represents the `RFC 7807` model. It is the main entity you need to work with to produce the problem.
50
50
51
-
==== Static helpers
51
+
===== Static helpers
52
52
53
53
There are several handy static methods to produce a simple `HttpProblem`:
54
54
@@ -111,7 +111,7 @@ Resulting response:
111
111
}
112
112
----
113
113
114
-
==== Builder
114
+
===== Builder
115
115
116
116
Use builder to create a rich problem instance with all properties:
117
117
@@ -126,7 +126,7 @@ throw HttpProblem.builder()
126
126
.build();
127
127
----
128
128
129
-
=== Adding extra parameters
129
+
==== Adding extra parameters
130
130
131
131
`RFC 7807` has a simple extension model: APIs are free to add any other properties to the problem details object, so all properties other than the five ones listed above are extensions.
132
132
@@ -163,7 +163,7 @@ Resulting response:
163
163
}
164
164
----
165
165
166
-
=== Adding headers
166
+
==== Adding headers
167
167
168
168
Some `HTTP` codes (like `413` or `426`) require additional response headers, or it may be required by third-party system/integration. `HttpProblem` support additional headers in response:
169
169
@@ -177,7 +177,7 @@ throw HttpProblem.builder()
177
177
.build();
178
178
----
179
179
180
-
=== Respond with errors details
180
+
==== Respond with errors details
181
181
182
182
`RFC 9457` finally described how errors should be delivered in HTTP APIs.
183
183
It is basically another extension `errors` on a root level. Adding errors is straight-forward using `error()` or `errors()` for bulk addition in builder:
@@ -214,7 +214,7 @@ In response:
214
214
If you need to enrich errors with more information feel free to extend `HttpProblem.Error` and make your custom errors model.
215
215
====
216
216
217
-
=== Custom `Exception` to `HttpProblem`
217
+
==== Custom `Exception` to `HttpProblem`
218
218
219
219
Apparently, you may already have many custom `Exception` classes in the codebase, and you want to make them `Problem Details` compliant without complete re-write. You can achieve this by implementing `HttpProblemMappable` interface. It allows you to control how exceptions should be transformed into `HttpProblem` if default behaviour doesn't suite your needs:
220
220
@@ -233,7 +233,7 @@ public class MyException implements HttpProblemMappable {
233
233
}
234
234
----
235
235
236
-
=== Custom Problems
236
+
==== Custom Problems
237
237
238
238
Extending `HttpProblem` and utilizing builder functionality makes it really easy:
239
239
@@ -255,7 +255,7 @@ public class OutOfStockProblem extends HttpProblem {
255
255
}
256
256
----
257
257
258
-
=== Custom Exception Handlers
258
+
==== Custom Exception Handlers
259
259
260
260
All the features described above should give you ability to rely solely on built-in global error handler. But, in case you still need custom exception handler for some reason, you still can do it:
0 commit comments