Skip to content

Commit

Permalink
Refactoring (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
floriankraemer authored Jan 6, 2025
1 parent 1aa3358 commit 2a25fb4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
15 changes: 13 additions & 2 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@ services:
Phauthentic\Symfony\ProblemDetails\FromExceptionEventFactoryInterface:
class: 'Phauthentic\Symfony\ProblemDetails\ProblemDetailsFactory'

Phauthentic\Symfony\ProblemDetails\Validation\ValidationErrorsToProblemDetailsKernelEventSubscriber:
Phauthentic\Symfony\ProblemDetails\ExceptionConversion\ValidationFailedExceptionConverter:
arguments:
$validationErrorsBuilder: '@Phauthentic\Symfony\ProblemDetails\Validation\ValidationErrorsBuilder'
$problemDetailsResponseFactory: '@Phauthentic\Symfony\ProblemDetails\FromExceptionEventFactoryInterface'

Phauthentic\Symfony\ProblemDetails\ExceptionConversion\HttpExceptionConverter:
arguments:
$problemDetailsFactory: '@Phauthentic\Symfony\ProblemDetails\ProblemDetailsFactoryInterface'

Phauthentic\Symfony\ProblemDetails\ExceptionConversion\ThrowableToProblemDetailsKernelListener:
arguments:
$exceptionConverters:
- '@Phauthentic\Symfony\ProblemDetails\ExceptionConversion\ValidationFailedExceptionConverter'
- '@Phauthentic\Symfony\ProblemDetails\ExceptionConversion\HttpExceptionConverter'
- '@Phauthentic\Symfony\ProblemDetails\ExceptionConversion\ThrowableToProblemDetailsKernelListener'
tags:
- { name: 'kernel.event_subscriber' }
- { name: 'kernel.event_listener', event: 'kernel.exception', priority: 0 }
61 changes: 61 additions & 0 deletions docs/Create-your-own-Converter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
## How to Implement and Use Your Own Exception Converters

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:

### Steps to Implement a Custom Exception Converter
1. **Understand the Exception Conversion**
- Exception converters are responsible for transforming an exception into a structured `ProblemDetails` response adhering to RFC 9457.
- The `ProblemDetailsFactory` can be used to create such responses within the converter.
2. **Create Your Custom Exception Converter**
- Create a class that handles the logic for converting specific exception types or scenarios into `ProblemDetails`.

```php
namespace App\ExceptionConverter;

use Psr\Log\LoggerInterface;
use Phauthentic\ProblemDetails\ExceptionConverterInterface;
use Phauthentic\ProblemDetails\ProblemDetailsFactoryInterface;
use Symfony\Component\HttpFoundation\Response;

class CustomExceptionConverter implements ExceptionConverterInterface
{
public function __construct(
private ProblemDetailsFactoryInterface $problemDetailsFactory,
private LoggerInterface $logger
) {
}

/**
* Converts the given exception to a ProblemDetails instance.
*/
public function convert(\Throwable $exception): Response
{
// Example exception check
if ($exception instanceof \DomainException) {
$this->logger->error('Domain Exception occurred: '.$exception->getMessage());

return $this->problemDetailsFactory->createResponse(
type: 'https://example.net/domain-error',
detail: $exception->getMessage(),
status: 400,
title: 'Domain Error'
);
}

// Default: throw the exception further if it cannot be converted
throw $exception;
}
}
```

3. **Register the Exception Converter in Your Application**
- Register your custom exception converter as a service in Symfony, and ensure it integrates into the exception handling workflow.

```yaml
# config/services.yaml
services:
App\ExceptionConverter\CustomExceptionConverter:
arguments:
$problemDetailsFactory: '@Phauthentic\ProblemDetails\ProblemDetailsFactoryInterface'
$logger: '@logger'
```
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Content-Language: en
]
}
```

## Alternatives

If you favor a different style of implementation check out the following bundles:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use RuntimeException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Symfony\Component\Validator\Exception\ValidationFailedException;
use Throwable;
Expand Down Expand Up @@ -52,8 +51,9 @@ public function convertExceptionToErrorDetails(Throwable $throwable, ExceptionEv
{
$throwable = $this->extractValidationFailedException($throwable);

$errors = $this->validationErrorsBuilder->buildErrors($throwable);

return $this->problemDetailsResponseFactory->createResponseFromKernelExceptionEvent($event, $errors);
return $this->problemDetailsResponseFactory->createResponseFromKernelExceptionEvent(
$event,
$this->validationErrorsBuilder->buildErrors($throwable)
);
}
}

0 comments on commit 2a25fb4

Please sign in to comment.