Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 7a20329

Browse files
committed
Documented new feature to provide default types
1 parent 210bba7 commit 7a20329

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

docs/book/default-types.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.

docs/book/response.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ This package also provides a factory for generating the
146146
- If the service contains a `problem-details` key with an array value
147147
containing a `json_flags` key, and that value is an integer, that value is
148148
provided as the `$jsonFlags` parameter.
149+
- If the service contains a `problem-details` key with an array value
150+
containing a `default_types_map` key, and that value is an array, that value is
151+
provided as the `$defaultTypesMap` parameter.
152+
> More information about defining [default types](default-types.md).
149153
150154
If any of the above config values are not present, a `null` value will be
151155
passed, allowing the default value to be used.

0 commit comments

Comments
 (0)