Skip to content

Commit 2a25fb4

Browse files
Refactoring (#4)
1 parent 1aa3358 commit 2a25fb4

File tree

4 files changed

+79
-6
lines changed

4 files changed

+79
-6
lines changed

config/services.yaml

+13-2
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

+61
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

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Content-Language: en
5959
]
6060
}
6161
```
62+
6263
## Alternatives
6364

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

src/ExceptionConversion/ValidationFailedExceptionConverter.php

+4-4
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)