https://github.com/phauthentic/problem-details-symfony-bundle
RFC 9457 Problem Details Response Symfony Bundle
https://github.com/phauthentic/problem-details-symfony-bundle
error-response mit-license php problem-details rfc-9457 solid-principles symfony symfony-bundle
Last synced: 22 days ago
JSON representation
RFC 9457 Problem Details Response Symfony Bundle
- Host: GitHub
- URL: https://github.com/phauthentic/problem-details-symfony-bundle
- Owner: Phauthentic
- License: mit
- Created: 2024-03-27T20:53:38.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-19T09:46:22.000Z (about 2 months ago)
- Last Synced: 2025-03-26T11:03:23.203Z (about 1 month ago)
- Topics: error-response, mit-license, php, problem-details, rfc-9457, solid-principles, symfony, symfony-bundle
- Language: PHP
- Homepage:
- Size: 92.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Problem Details Symfony Bundle
This bundle provides support for [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457.html).
* Turns `ValidationFailedException` exceptions into a `ProblemDetails` responses complying with RFC 9457.
* The error building is fully customizable.
* Provides a `ProblemDetailsFactory` service to create `ProblemDetails` instances.## Installation
```bash
composer require phauthentic/problem-details-symfony-bundle
```## Documentation
Simply inject the `ProblemDetailsFactoryInterface` into your handlers and use it to create `ProblemDetails` responses.
```php
class ExampleController
{
public function __construct(
private ProblemDetailsFactoryInterface $problemDetailsFactory
) {
}/**
* @Route("/example", methods={"POST"})
*/
public function exampleAction(Request $request): Response
{
return $this->problemDetailsFactory->createResponse(
type: 'https://example.net/validation-error',
detail: 'Your request is not valid.',
status: 422,
title: 'Validation Error',
);
}
}
```### Service Configuration
To add more converters to the kernel listener, you can tag additional services with `phauthentic.problem_details.exception_converter`. They must implement the [ExceptionConverterInterface](src/ExceptionConversion/ExceptionConverterInterface.php).
```yaml
Phauthentic\Symfony\ProblemDetails\ExceptionConversion\ValidationFailedExceptionConverter:
arguments:
$validationErrorsBuilder: '@Phauthentic\Symfony\ProblemDetails\Validation\ValidationErrorsBuilder'
$problemDetailsResponseFactory: '@Phauthentic\Symfony\ProblemDetails\FromExceptionEventFactoryInterface'
tags: ['phauthentic.problem_details.exception_converter']
```To completely override the list of already configured listeners override
```yaml
Phauthentic\Symfony\ProblemDetails\ThrowableToProblemDetailsKernelListener:
public: true
arguments:
$exceptionConverters: !tagged_iterator phauthentic.problem_details.exception_converter
tags:
- { name: 'kernel.event_listener', event: 'kernel.exception', priority: 0 }
```in your `services.yaml`.
## 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:
* [phpro/api-problem-bundle](https://github.com/phpro/api-problem-bundle)
## License
This bundle is under the [MIT license](LICENSE).
Copyright Florian Krämer