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: src/docs/asciidoc/index.adoc
+202-1Lines changed: 202 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1295,6 +1295,28 @@ With this configuration, 400 Bad Request will be printed on DEBUG level.
1295
1295
401 Unauthorized will be printed on INFO.
1296
1296
Finally, all status code in the 5xx range will be printed on ERROR.
1297
1297
1298
+
==== Filter logging
1299
+
1300
+
If you like to filter certain exceptions from being logged (while you still want the exception handling to happen), then you can declare a `@Bean` of type `io.github.wimdeblauwe.errorhandlingspringbootstarter.LoggingServiceFilter`:
public class MyLoggingServiceFilter implements LoggingServiceFilter {
1309
+
@Override
1310
+
public boolean shouldLogException(ApiErrorResponse errorResponse,
1311
+
Throwable exception) {
1312
+
if( exception instanceof SomeException ) {
1313
+
return false;
1314
+
}
1315
+
return true;
1316
+
}
1317
+
}
1318
+
----
1319
+
1298
1320
=== Spring Security
1299
1321
1300
1322
==== AuthenticationEntryPoint
@@ -1459,6 +1481,173 @@ Those are implementations of `jakarta.servlet.Filter`, usually subclasses of `or
1459
1481
1460
1482
By setting the property `error.handling.handle-filter-chain-exceptions` to `true`, the library will handle those exceptions and return error responses just like is done for exceptions coming from controller methods.
1461
1483
1484
+
=== Problem Detail format (RFC 9457)
1485
+
1486
+
The library supports the https://www.rfc-editor.org/rfc/rfc9457[RFC 9457] Problem Detail format as an alternative to the default JSON response format.
1487
+
When enabled, error responses will use the standardized Problem Detail structure and the `application/problem+json` content type.
1488
+
1489
+
==== Enabling Problem Detail format
1490
+
1491
+
To enable Problem Detail format, set the following property:
1492
+
1493
+
[source,properties]
1494
+
----
1495
+
error.handling.use-problem-detail-format=true
1496
+
----
1497
+
1498
+
==== Response format comparison
1499
+
1500
+
When Problem Detail format is disabled (default), the response looks like this:
1501
+
1502
+
[source,json]
1503
+
----
1504
+
{
1505
+
"code": "USER_NOT_FOUND",
1506
+
"message": "Could not find user with id 123"
1507
+
}
1508
+
----
1509
+
1510
+
When Problem Detail format is enabled, the same error produces:
1511
+
1512
+
[source,json]
1513
+
----
1514
+
{
1515
+
"type": "user-not-found",
1516
+
"title": "Not Found",
1517
+
"status": 404,
1518
+
"detail": "Could not find user with id 123"
1519
+
}
1520
+
----
1521
+
1522
+
The Problem Detail format is built like this:
1523
+
1524
+
[cols="1,1"]
1525
+
|===
1526
+
|Problem Detail field | Description
1527
+
1528
+
|`type`
1529
+
|Value of the `code` field (converted to kebab-case by default)
1530
+
1531
+
|`detail`
1532
+
|Value of the `message` field
1533
+
1534
+
|`title`
1535
+
|The HTTP status text (This is coming from Spring itself)
1536
+
1537
+
|`status`
1538
+
|The numeric HTTP status code
1539
+
|===
1540
+
1541
+
==== Configuring the type prefix
1542
+
1543
+
You can add a prefix to the `type` field to create fully qualified URIs.
1544
+
This is useful for providing documentation links for your error types:
"detail": "Object of class [com.example.user.User] with identifier [1]: optimistic locking failed",
1646
+
"identifier": "1",
1647
+
"persistentClassName": "com.example.user.User"
1648
+
}
1649
+
----
1650
+
1462
1651
== Custom exception handler
1463
1652
1464
1653
If the <<Configuration,extensive customization options>> are not enough, you can write your own `ApiExceptionHandler` implementation.
@@ -1585,7 +1774,19 @@ When this is set to `true`, you can use any superclass from your `Exception` typ
1585
1774
1586
1775
|error.handling.handle-filter-chain-exceptions
1587
1776
|Set this to `true` to have the library intercept any exception thrown from custom filters and also have the same error responses as exceptions thrown from controller methods.
1588
-
|`false`.
1777
+
|`false`
1778
+
1779
+
|error.handling.use-problem-detail-format
1780
+
|Set this to `true` to use the https://www.rfc-editor.org/rfc/rfc9457[RFC 9457] Problem Detail format for error responses instead of the default format.
1781
+
|`false`
1782
+
1783
+
|error.handling.problem-detail-type-prefix
1784
+
|The prefix to use for the `type` field in Problem Detail responses. This is typically a URL that serves as a namespace for the error types.
|When using Problem Detail format, this controls whether the error code is converted to kebab-case for the `type` field. For example, `USER_NOT_FOUND` would become `user-not-found`.
1789
+
|`true`
1589
1790
|===
1590
1791
1591
1792
== Adding Error Responses to OpenAPI Documentation
Copy file name to clipboardExpand all lines: src/main/java/io/github/wimdeblauwe/errorhandlingspringbootstarter/AbstractErrorHandlingConfiguration.java
+9-2Lines changed: 9 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -33,8 +33,15 @@ public ErrorHandlingFacade errorHandlingFacade(List<ApiExceptionHandler> handler
0 commit comments