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
*`problem.open-api.req-validation-enabled`:- To enable or disable OpenAPI specification validation for request, default is `false`.
325
326
*`problem.open-api.res-validation-enabled`:- To enable or disable OpenAPI specification validation for response, default is `false`.
326
327
327
-
## Usage
328
328
329
-
###Error Key
329
+
## Error Key
330
330
The main concept behind specifying the error attributes in `properties` file is **Error key**, which is mandatory to be unique for each error scenario.
331
331
**It is either derived or specified by application** while throwing exception and used to externalize the error attributes in `properties` file.
332
332
@@ -350,7 +350,7 @@ status.some.error.key=400
350
350
* When controller method name changes or controller argument Object class name or any of its property name changes then `jakarta.validation.*` violation error keys may change.
351
351
* When database constraint name or index name changes then any `DuplicateKeyException` or `DataIntegrityViolationException` error key may change.
352
352
353
-
###Error response structure
353
+
## Error response
354
354
Following is an example response body for an error.
355
355
```json
356
356
{
@@ -373,8 +373,8 @@ Response Header when service is configured for XML `HttpMessageConverters`
373
373
content-type: application/problem+xml
374
374
```
375
375
376
-
**Error Response description**
377
-
*`type`:- A URI reference that identifies the problem type. When dereferenced, it provides human-readable documentation for this error.
376
+
**Description**
377
+
*`type`:- A `URI` reference that identifies the problem type. When dereferenced, it provides human-readable documentation for this error.
378
378
If not set `about:blank` is taken as default.
379
379
*`title`:- A short, human-readable summary of the error such as `Bad Request`.
380
380
*`status`:- The HTTP status code, int value such as `500`.
To minimize the number of properties following defaults are taken if `HttpStatus` is specified as `status.`(error key) property.
454
-
* 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`
455
-
* 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`
456
-
* Detail default is taken from thrown exception's `exception.getMessage()`.
454
+
***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`
455
+
***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`
456
+
***Detail** default is taken from thrown exception's `exception.getMessage()`.
457
457
458
458
> [!NOTE]
459
459
> `status.`(error key) property is considered only for exceptions where no explicit advice is defined,
460
460
otherwise `HttpStatus` is specified in the java code.
461
461
462
-
###Creating and throwing exceptions
462
+
## Creating and throwing exceptions
463
463
464
464
Apart from exceptions thrown by frameworks or java, every application need to throw custom exceptions.
465
465
[**`ApplicationProblem`**](src/main/java/com/ksoot/problem/core/ApplicationProblem.java) and
But exceptions come with some default attributes as follows, to minimize the number of properties required to be defined in `properties` file
487
487
488
488
If the messages are not found in `properties` files, defaults are taken as follows.
489
-
* 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`
490
-
* Title is taken as specified `HttpStatus`'s reason phrase e.g. if `HttpStatus` is given as `EXPECTATION_FAILED` like the Title default would be `Expectation Failed`
491
-
* Detail default is taken as thrown exception's `exception.getMessage()`
489
+
***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`
490
+
***Title** is taken as specified `HttpStatus`'s reason phrase e.g. if `HttpStatus` is given as `EXPECTATION_FAILED` like the Title default would be `Expectation Failed`
491
+
***Detail** default is taken as thrown exception's `exception.getMessage()`
492
492
493
493
There are multiple other methods available while creating and throwing exceptions in [**`Problems`**](src/main/java/com/ksoot/problem/Problems.java),
494
494
for details refers to its source code and java docs.
@@ -531,7 +531,7 @@ private static final class MyException extends RuntimeException {
531
531
}
532
532
```
533
533
534
-
###Stack traces
534
+
## Stack traces
535
535
Set following property to `true` to get the `stacktrace` in error response,
536
536
should only be used on local for debugging purpose and strictly prohibited elsewhere as it may expose application internals.
537
537
```properties
@@ -563,7 +563,7 @@ Example response
563
563
}
564
564
```
565
565
566
-
###Cause chains
566
+
## Cause chains
567
567
An exception may have a cause, which in tern may also have another and so on.
568
568
The complete cause chain can also be viewed in error response, again it should just be used for local debugging purposes only.
569
569
@@ -697,7 +697,7 @@ class CustomMethodArgumentNotValidExceptionHandler implements MethodArgumentNotV
697
697
}
698
698
```
699
699
700
-
###Define new advices
700
+
## Define new advices
701
701
There should not be any need to create any custom exception hence new advices, but if there is a pressing need to do so,
702
702
custom exception can be created and corresponding custom `ControllerAdvice` can be defined for the same, though not recommended.
703
703
Following example demonstrates a new advice for some custom exception `MyCustomException`.
0 commit comments