Skip to content

Commit

Permalink
Fixing the services.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
floriankraemer committed Jan 6, 2025
1 parent dfeca32 commit c6e70c6
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 27 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'
```
46 changes: 25 additions & 21 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,31 @@ This bundle provides support for [RFC 9457](https://www.rfc-editor.org/rfc/rfc94
composer require phauthentic/problem-details-symfony-bundle
```

## Docs
## Problem Details Example

```text
HTTP/1.1 422 Unprocessable Content
Content-Type: application/problem+json
Content-Language: en
{
"type": "https://example.net/validation-error",
"title": "Your request is not valid.",
"errors": [
{
"detail": "must be a positive integer",
"pointer": "#/age"
},
{
"detail": "must be 'green', 'red' or 'blue'",
"pointer": "#/profile/color"
}
]
}
```


## Documentation

```php
class ExampleController
Expand All @@ -37,28 +61,8 @@ class ExampleController
}
```

## Problem Details Example

```text
HTTP/1.1 422 Unprocessable Content
Content-Type: application/problem+json
Content-Language: en

{
"type": "https://example.net/validation-error",
"title": "Your request is not valid.",
"errors": [
{
"detail": "must be a positive integer",
"pointer": "#/age"
},
{
"detail": "must be 'green', 'red' or 'blue'",
"pointer": "#/profile/color"
}
]
}
```
## 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 c6e70c6

Please sign in to comment.