|
| 1 | +# Default types |
| 2 | + |
| 3 | +When you raise your own exceptions implementing `Zend\ProblemDetails\Exception\ProblemDetailsExceptionInterface` you |
| 4 | +will always be in control of all the properties returned as part of the response payload, naming `status`, `type`, |
| 5 | +`title`, `detail`, etc. |
| 6 | + |
| 7 | +However, there are some use cases in which this library will have to infer some of those values. |
| 8 | + |
| 9 | +The main situations in which this can happen are: |
| 10 | + |
| 11 | +* When an exception not implementing `ProblemDetailsExceptionInterface` is captured by the `ProblemDetailsMiddleware`. |
| 12 | +* When the `ProblemDetailsNotFoundHandler` is executed. |
| 13 | + |
| 14 | +In these two cases, the `title` and `type` properties will be inferred from the status code, which will usually be |
| 15 | +`500` in the first case and `404` in the second one. |
| 16 | + |
| 17 | +> To be more precise, the `ProblemDetailsMiddleware` will use the exception's error code when `debug` is `true`, |
| 18 | +> and `500` otherwise. |
| 19 | +
|
| 20 | +Because of this, in any of those cases, you will end up with values like `https://httpstatus.es/404` or |
| 21 | +`https://httpstatus.es/500` for the `type` property. |
| 22 | + |
| 23 | +## Configuring custom default types |
| 24 | + |
| 25 | +Since the `type` property will usually be used by API consumers to uniquely identify an error, you might want to be |
| 26 | +able to provide your own custom values for the `type` property. |
| 27 | + |
| 28 | +In order to do that, this library lets you configure the default `type` value to be used for every status code |
| 29 | +when some of the cases listed above happens. |
| 30 | + |
| 31 | +```php |
| 32 | +return [ |
| 33 | + |
| 34 | + 'problem-details' => [ |
| 35 | + 'default_types_map' => [ |
| 36 | + 404 => 'https://example.com/problem-details/error/not-found', |
| 37 | + 500 => 'https://example.com/problem-details/error/internal-server-error', |
| 38 | + ], |
| 39 | + ], |
| 40 | + |
| 41 | +]; |
| 42 | +``` |
| 43 | + |
| 44 | +If this configuration is found, it will be consumed by the |
| 45 | +[ProblemDetailsResponseFactoryFactory](response.md#problemdetailsresponsefactoryfactory) and your custom values will |
| 46 | +be used when the `type` was not explicitly provided. |
0 commit comments