Skip to content

Commit a603b26

Browse files
committed
issue #97: route grouping, problem details
Signed-off-by: horea <[email protected]>
1 parent ab6239e commit a603b26

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

docs/book/v6/extended-features/handler-structure.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ In this way, the developer can easily figure out the functionality of each handl
3838

3939
## Mapping of the handlers
4040

41-
The mapping of the handlers and their current paths and actions can be found [here](https://docs.dotkernel.org/img/api/naming-convention.png).
41+
The full mapping of the handlers and their current paths and actions can be found [here](https://docs.dotkernel.org/img/api/naming-convention.png).
42+
![naming-convention-thumbnail](https://docs.dotkernel.org/img/api/naming-convention-thumbnail.png)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Problem details
2+
3+
With the usage of `mezzio/mezzio-problem-details` we have implemented a way to help the developers understand better the errors that they are getting from their APIs based on the [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457.html) standards.
4+
5+
Example of a response with details:
6+
7+
```json
8+
{
9+
"title": "Unauthorized",
10+
"type": "https://docs.dotkernel.org/api-documentation/v5/core-features/error-reporting/",
11+
"status": 401,
12+
"detail": "You are not allowed to report errors."
13+
}
14+
```
15+
16+
Usually the response includes:
17+
18+
- A title related to the error
19+
- The type of error
20+
- The status of the request (e.g `404`)
21+
- Different error messages
22+
23+
More fields can be added based on the preference of the developer.
24+
25+
## Our changes
26+
27+
In order for us to implement this new feature, a new middleware component was required.
28+
We have created `ProblemDetailsMiddleware` along with `ProblemDetailsNotFoundHandler` which is being called in the `config/pipeline.php` file.
29+
Our exceptions have also been modified in order to be slimmed around the requirement for the `problem-details` package.
30+
31+
Example from `src/App/src/Exception/BadRequestException.php`:
32+
33+
```php
34+
public static function create(string $detail, string $type = '', string $title = '', array $additional = []): self
35+
{
36+
$exception = new self();
37+
38+
$exception->type = $type;
39+
$exception->detail = $detail;
40+
$exception->status = StatusCodeInterface::STATUS_BAD_REQUEST;
41+
$exception->title = $title;
42+
$exception->additional = $additional;
43+
44+
return $exception;
45+
}
46+
```
47+
48+
An example configuration file for setting custom links has also been created in `config/autoload/problem-details.global.php`.
49+
Here the statuses of the API calls are being attributed to a link.
50+
51+
```php
52+
return [
53+
'problem-details' => [
54+
'default_types_map' => [
55+
StatusCodeInterface::STATUS_BAD_REQUEST
56+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-400-bad-request',
57+
StatusCodeInterface::STATUS_UNAUTHORIZED
58+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-401-unauthorized',
59+
StatusCodeInterface::STATUS_FORBIDDEN
60+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-403-forbidden',
61+
StatusCodeInterface::STATUS_NOT_FOUND
62+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-404-not-found',
63+
StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED
64+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-405-method-not-allowed',
65+
StatusCodeInterface::STATUS_NOT_ACCEPTABLE
66+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-406-not-acceptable',
67+
StatusCodeInterface::STATUS_CONFLICT
68+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-409-conflict',
69+
StatusCodeInterface::STATUS_GONE
70+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-410-gone',
71+
StatusCodeInterface::STATUS_UNSUPPORTED_MEDIA_TYPE
72+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-415-unsupported-media-type',
73+
StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR
74+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-500-internal-server-error',
75+
],
76+
],
77+
];
78+
```

0 commit comments

Comments
 (0)