Skip to content

Commit c6e70c6

Browse files
Fixing the services.yaml
1 parent dfeca32 commit c6e70c6

File tree

4 files changed

+103
-27
lines changed

4 files changed

+103
-27
lines changed

config/services.yaml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,20 @@ services:
88
Phauthentic\Symfony\ProblemDetails\FromExceptionEventFactoryInterface:
99
class: 'Phauthentic\Symfony\ProblemDetails\ProblemDetailsFactory'
1010

11-
Phauthentic\Symfony\ProblemDetails\Validation\ValidationErrorsToProblemDetailsKernelEventSubscriber:
11+
Phauthentic\Symfony\ProblemDetails\ExceptionConversion\ValidationFailedExceptionConverter:
1212
arguments:
1313
$validationErrorsBuilder: '@Phauthentic\Symfony\ProblemDetails\Validation\ValidationErrorsBuilder'
1414
$problemDetailsResponseFactory: '@Phauthentic\Symfony\ProblemDetails\FromExceptionEventFactoryInterface'
15+
16+
Phauthentic\Symfony\ProblemDetails\ExceptionConversion\HttpExceptionConverter:
17+
arguments:
18+
$problemDetailsFactory: '@Phauthentic\Symfony\ProblemDetails\ProblemDetailsFactoryInterface'
19+
20+
Phauthentic\Symfony\ProblemDetails\ExceptionConversion\ThrowableToProblemDetailsKernelListener:
21+
arguments:
22+
$exceptionConverters:
23+
- '@Phauthentic\Symfony\ProblemDetails\ExceptionConversion\ValidationFailedExceptionConverter'
24+
- '@Phauthentic\Symfony\ProblemDetails\ExceptionConversion\HttpExceptionConverter'
25+
- '@Phauthentic\Symfony\ProblemDetails\ExceptionConversion\ThrowableToProblemDetailsKernelListener'
1526
tags:
16-
- { name: 'kernel.event_subscriber' }
27+
- { name: 'kernel.event_listener', event: 'kernel.exception', priority: 0 }

docs/Create-your-own-Converter.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## How to Implement and Use Your Own Exception Converters
2+
3+
Implementing and using custom exception converters can make exception handling in your application more structured and versatile. With the `ProblemDetailsSymfonyBundle`, it is possible to extend and customize the way exceptions are converted to `ProblemDetails` responses. Here is how you can create and use your own exception converters:
4+
5+
### Steps to Implement a Custom Exception Converter
6+
1. **Understand the Exception Conversion**
7+
- Exception converters are responsible for transforming an exception into a structured `ProblemDetails` response adhering to RFC 9457.
8+
- The `ProblemDetailsFactory` can be used to create such responses within the converter.
9+
2. **Create Your Custom Exception Converter**
10+
- Create a class that handles the logic for converting specific exception types or scenarios into `ProblemDetails`.
11+
12+
```php
13+
namespace App\ExceptionConverter;
14+
15+
use Psr\Log\LoggerInterface;
16+
use Phauthentic\ProblemDetails\ExceptionConverterInterface;
17+
use Phauthentic\ProblemDetails\ProblemDetailsFactoryInterface;
18+
use Symfony\Component\HttpFoundation\Response;
19+
20+
class CustomExceptionConverter implements ExceptionConverterInterface
21+
{
22+
public function __construct(
23+
private ProblemDetailsFactoryInterface $problemDetailsFactory,
24+
private LoggerInterface $logger
25+
) {
26+
}
27+
28+
/**
29+
* Converts the given exception to a ProblemDetails instance.
30+
*/
31+
public function convert(\Throwable $exception): Response
32+
{
33+
// Example exception check
34+
if ($exception instanceof \DomainException) {
35+
$this->logger->error('Domain Exception occurred: '.$exception->getMessage());
36+
37+
return $this->problemDetailsFactory->createResponse(
38+
type: 'https://example.net/domain-error',
39+
detail: $exception->getMessage(),
40+
status: 400,
41+
title: 'Domain Error'
42+
);
43+
}
44+
45+
// Default: throw the exception further if it cannot be converted
46+
throw $exception;
47+
}
48+
}
49+
```
50+
51+
3. **Register the Exception Converter in Your Application**
52+
- Register your custom exception converter as a service in Symfony, and ensure it integrates into the exception handling workflow.
53+
54+
```yaml
55+
# config/services.yaml
56+
services:
57+
App\ExceptionConverter\CustomExceptionConverter:
58+
arguments:
59+
$problemDetailsFactory: '@Phauthentic\ProblemDetails\ProblemDetailsFactoryInterface'
60+
$logger: '@logger'
61+
```

readme.md

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,31 @@ This bundle provides support for [RFC 9457](https://www.rfc-editor.org/rfc/rfc94
1212
composer require phauthentic/problem-details-symfony-bundle
1313
```
1414

15-
## Docs
15+
## Problem Details Example
16+
17+
```text
18+
HTTP/1.1 422 Unprocessable Content
19+
Content-Type: application/problem+json
20+
Content-Language: en
21+
22+
{
23+
"type": "https://example.net/validation-error",
24+
"title": "Your request is not valid.",
25+
"errors": [
26+
{
27+
"detail": "must be a positive integer",
28+
"pointer": "#/age"
29+
},
30+
{
31+
"detail": "must be 'green', 'red' or 'blue'",
32+
"pointer": "#/profile/color"
33+
}
34+
]
35+
}
36+
```
37+
38+
39+
## Documentation
1640

1741
```php
1842
class ExampleController
@@ -37,28 +61,8 @@ class ExampleController
3761
}
3862
```
3963

40-
## Problem Details Example
4164

42-
```text
43-
HTTP/1.1 422 Unprocessable Content
44-
Content-Type: application/problem+json
45-
Content-Language: en
4665

47-
{
48-
"type": "https://example.net/validation-error",
49-
"title": "Your request is not valid.",
50-
"errors": [
51-
{
52-
"detail": "must be a positive integer",
53-
"pointer": "#/age"
54-
},
55-
{
56-
"detail": "must be 'green', 'red' or 'blue'",
57-
"pointer": "#/profile/color"
58-
}
59-
]
60-
}
61-
```
6266
## Alternatives
6367

6468
If you favor a different style of implementation check out the following bundles:

src/ExceptionConversion/ValidationFailedExceptionConverter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use RuntimeException;
1010
use Symfony\Component\HttpFoundation\Response;
1111
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
12-
use Symfony\Component\HttpKernel\Exception\HttpException;
1312
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
1413
use Symfony\Component\Validator\Exception\ValidationFailedException;
1514
use Throwable;
@@ -52,8 +51,9 @@ public function convertExceptionToErrorDetails(Throwable $throwable, ExceptionEv
5251
{
5352
$throwable = $this->extractValidationFailedException($throwable);
5453

55-
$errors = $this->validationErrorsBuilder->buildErrors($throwable);
56-
57-
return $this->problemDetailsResponseFactory->createResponseFromKernelExceptionEvent($event, $errors);
54+
return $this->problemDetailsResponseFactory->createResponseFromKernelExceptionEvent(
55+
$event,
56+
$this->validationErrorsBuilder->buildErrors($throwable)
57+
);
5858
}
5959
}

0 commit comments

Comments
 (0)