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
It does all hard part, A lot of advices are out of box available which are autoconfigured as `ControllerAdvice`s
73
+
It does all hard part, A lot of advices are out of box available that are autoconfigured as `ControllerAdvice`s
74
74
depending on the jars in classpath of consumer application.
75
75
**Even for exceptions for which no advices are defined**, respective error response can be specified by
76
76
messages in `properties` file, elaborated in [*Usage*](https://github.com/officiallysingh/spring-boot-problem-handler#usage) section.
77
-
New custom advices could be required only in cases where it is required to take some data from exception instance
77
+
New custom advice could be required only in cases where it is required to take some data from exception instance
78
78
to dynamically derive [*Error key*](https://github.com/officiallysingh/spring-boot-problem-handler#error-key)
79
-
or to use this data to resolve any placeholders in error message. In such cases consumer application can define
79
+
or to use this data to resolve any placeholders in an error message. In such cases consumer application can define
80
80
their own custom `ControllerAdvice`'s,
81
81
Any existing advice can be referred to weave the custom advice into the framework.
82
82
83
83
> A default set of `ControllerAdvice`s are always configured irrespective of the fact that whether
84
-
the application is Spring Web or Spring Webflux, but few advices are conditional
84
+
the application is Spring Web or Spring Webflux. However, few advices are conditional
85
85
such as for Handling Security, OpenAPI and Dao related exceptions, which are elaborated in their respective sections.
86
86
87
87
## Features
88
88
89
89
* A lot of inbuilt `ControllerAdvice`'s out of box available to handle most common exceptions.
90
-
* Extendable to add more advices or override existing advices in consumer applications, weaving them into aligned framework for exception handling.
90
+
* Extendable to add more advices or override existing advices in consumer applications, weaving them into an aligned framework for exception handling.
91
91
* Customizable Error response structure.
92
-
* Provides mechanism to specify error response for any kind of exception without defining any `ControllerAdvice`.
92
+
* Provides a mechanism to specify error response for any kind of exception without defining any `ControllerAdvice`.
93
93
* Works with both Spring Web and Spring Webflux applications.
94
94
* Customizable to override the default attributes in error response by overriding the same in `properties` file.
95
95
* The autoconfigured advices can be disabled or overridden or extended as per needs.
@@ -189,7 +189,7 @@ These advices are autoconfigured as a bean `SecurityExceptionHandler` if followi
189
189
and [**`ProblemAccessDeniedHandler`**](src/main/java/com/ksoot/problem/spring/advice/security/ProblemAccessDeniedHandler.java)
190
190
are autoconfigured as `authenticationEntryPoint` and `accessDeniedHandler` beans respectively.
191
191
192
-
But to make it workfollowing needs to be done in application's Spring Security configuration.
192
+
But to make it work, the following needs to be done in the application's Spring Security configuration.
193
193
Refer to example [**`WebSecurityConfiguration`**](https://github.com/officiallysingh/problem-handler-web-demo/blob/main/src/main/java/com/ksoot/problem/demo/config/WebSecurityConfiguration.java)
194
194
```java
195
195
@Autowired
@@ -232,7 +232,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
232
232
and [**`ProblemServerAccessDeniedHandler`**](src/main/java/com/ksoot/problem/spring/advice/security/ProblemServerAccessDeniedHandler.java)
233
233
are autoconfigured as `authenticationEntryPoint` and `accessDeniedHandler` beans respectively.
234
234
235
-
But to make it work following needs to be done in application Spring Security configuration.
235
+
But to make it work, the following needs to be done in application Spring Security configuration.
236
236
Refer to example [**`WebFluxSecurityConfiguration`**](https://github.com/officiallysingh/problem-handler-webflux-demo/blob/main/src/main/java/com/ksoot/problem/demo/config/WebFluxSecurityConfiguration.java)
237
237
```java
238
238
@Autowired
@@ -293,7 +293,7 @@ in addition also requires the following configuration:
*`problem.enabled`:- To enable or disable autoconfigurations, default is `true`.
353
+
*`problem.enabled`:- To enable or disable autoconfiguration, default is `true`.
354
354
In case consumer applications are interested to avail advices but want full control over configurations,
355
355
then it can be set to `false` and required advices can be configured as Spring beans similar to how they are autoconfigured.
356
356
*`problem.type-url`:- The base `URL` for **Help page** describing errors. For different exceptions respective code for exception is appended to it followed by a `#`
The main concept behind specifying the error attributes in `properties` file is **Error key**, which is mandatory to be unique for each error scenario.
378
378
**It is either derived or specified by application** while throwing exception and used to externalize the error attributes in `properties` file.
379
379
380
-
For example if error key for some exception is `some.error.key`, then error response attributes can be specified in `properties` file as follows.
380
+
For example, if error key for some exception is `some.error.key`, then error response attributes can be specified in `properties` file as follows.
Used in `type`. Commonly used to set unique codes for different business error scenarios.
434
434
435
435
## Message resolvers
436
-
To know how to define the error attributes in properties file, enable debugging as follows.
436
+
To know how to define the error attributes in the properties file, enable debugging as follows.
437
437
```properties
438
438
problem.debug-enabled=true
439
439
```
@@ -480,7 +480,7 @@ For example in case of `ConstraintViolationException` `codes` would be multiple
480
480
}
481
481
}
482
482
```
483
-
Respective codes for corresponding attribute can be copied and message can be specified for same in `properties` file.
483
+
Respective codes for corresponding attribute can be copied, and a message can be specified for the same in `properties` file.
484
484
485
485
> [!NOTE]
486
486
> `org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException` i.e. fully qualified name of exception is the [*Error key*](https://github.com/officiallysingh/spring-boot-problem-handler#error-key) in above case.
To minimize the number of properties following defaults are taken if `HttpStatus` is specified as `status.`(error key) property.
500
-
***Code** is taken as specified `HttpStatus`'s int code e.g. if `HttpStatus` is given as `EXPECTATION_FAILED` then the Code default would be `417`
501
-
***Title** is taken as specified `HttpStatus`'s reason phrase e.g. if `HttpStatus` is given as `EXPECTATION_FAILED` then the Title default would be `Expectation Failed`
500
+
***Code** is taken as specified `HttpStatus`'s int code e.g., if `HttpStatus` is given as `EXPECTATION_FAILED` then the Code default would be `417`
501
+
***Title** is taken as specified `HttpStatus`'s reason phrase e.g., if `HttpStatus` is given as `EXPECTATION_FAILED` then the Title default would be `Expectation Failed`
502
502
***Detail** default is taken from thrown exception's `exception.getMessage()`.
503
503
504
504
> [!NOTE]
@@ -521,7 +521,7 @@ It provides multiple fluent methods to build and throw exceptions.
Error response attributes `code`, `title` and `detail` are expected from the message source (`properties` file) available as follows.
524
-
Notice the [*Error key*](https://github.com/officiallysingh/spring-boot-problem-handler#error-key)**sample.problem** in following properties
524
+
Notice the [*Error key*](https://github.com/officiallysingh/spring-boot-problem-handler#error-key)**sample.problem** in the following properties
525
525
526
526
```properties
527
527
code.sample.problem=AYX123
@@ -576,7 +576,7 @@ throw problem;
576
576
577
577
* Applications may also define `enum`s implementing [**`ErrorType`**](src/main/java/com/ksoot/problem/core/ErrorType.java) interface
578
578
with attributes for error scenarios and creating exceptions as follows. Default error attributes `detail`, `status` etc. can be customized in `properties` file for given `errorKey`,
579
-
otherwise the enum only is sufficient.
579
+
otherwise the enum only is enough.
580
580
```java
581
581
@Getter
582
582
publicenumAppErrorsimplementsErrorType {
@@ -803,7 +803,7 @@ class CustomMethodArgumentNotValidExceptionHandler implements MethodArgumentNotV
803
803
There should not be any need to create any custom exception hence new advices, but if there is a pressing need to do so,
804
804
custom exception can be created and corresponding custom `ControllerAdvice` implementing [**`AdviceTrait`**](src/main/java/com/ksoot/problem/spring/advice/AdviceTrait.java)
805
805
can be defined for the same, though not recommended.
806
-
Following example demonstrates a new advice for some custom exception `MyCustomException`.
806
+
Following example demonstrates new advice for some custom exception `MyCustomException`.
807
807
808
808
> For Spring Web applications
809
809
```java
@@ -1179,4 +1179,4 @@ as examples to see usage and **example error responses** for different kind of e
1179
1179
1180
1180
## Known Issues
1181
1181
* If an application uses multiple vendor relational databases then the [**`ConstraintNameResolver`**](src/main/java/com/ksoot/problem/spring/advice/dao/ConstraintNameResolver.java)
1182
-
may not work properly, needs further testing. For example if it is using Postgres and SQL Server both.
1182
+
may not work properly, needs further testing. For example, if it is using Postgres and SQL Server both.
0 commit comments