PHPackages                             kenny1911/symfony-http-exception - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. kenny1911/symfony-http-exception

ActiveLibrary[HTTP &amp; Networking](/categories/http)

kenny1911/symfony-http-exception
================================

The library provides a convenient way to convert PHP exceptions into Symfony HTTP exceptions with support for status codes, messages, headers, and translation capabilities.

0.3.0(11mo ago)01.5k—7.8%MITPHPPHP ^8.1CI passing

Since Jul 24Pushed 11mo agoCompare

[ Source](https://github.com/Kenny1911/symfony-http-exception)[ Packagist](https://packagist.org/packages/kenny1911/symfony-http-exception)[ RSS](/packages/kenny1911-symfony-http-exception/feed)WikiDiscussions 0.3.x Synced today

READMEChangelog (3)Dependencies (6)Versions (6)Used By (0)

Symfony HttpException
=====================

[](#symfony-httpexception)

\[ English | [Русский](./README-RU.md) \]

The `kenny1911/symfony-http-exception` library provides a convenient way to convert PHP exceptions into Symfony HTTP exceptions with support for status codes, messages, headers, and translation capabilities.

Key Features
------------

[](#key-features)

- Convert any exception to `HttpException` using attributes
- Support for HTTP status codes, messages, headers, and translation parameters
- Built-in aliases for common HTTP errors
- Symfony Translation component integration
- Dynamic values using Symfony ExpressionLanguage

Installation
------------

[](#installation)

Install via Composer:

```
composer require kenny1911/symfony-http-exception
```

Register the event subscriber in your Symfony configuration:

```
# config/services.yaml
services:
    Kenny1911\SymfonyHttpException\ErrorListener:
        arguments:
          - '@translator'
          - !service { class: Symfony\Component\ExpressionLanguage\ExpressionLanguage }
        tags:
            - { name: kernel.event_subscriber }
```

> Arguments `$translator` and `$expressionLanguage` is not required

As the third argument `$metadataLoader`, you can pass the instance of `Kenny1911\SymfonyHttpException\Metadata\MetadataLoader`. The default value is `AttributeMetadataLoader`. Apart from this, there is also:

- `ArrayMetadataLoader` - use map exception class to `BaseHttpException` instance.
- `ConfigMetadataLoader` - use associative array for describe configuration.
- `ChainMetadataLoader` - combine multiply `MetadataLoader` instances.

Эти загрузчики могут использоваться для описания сторонних классов исключения.

Example of advanced configuration:

```
# config/services.yaml
services:
    Kenny1911\SymfonyHttpException\ErrorListener:
        arguments:
          - '@translator'
          - !service { class: Symfony\Component\ExpressionLanguage\ExpressionLanguage }
          - !service
            class: Kenny1911\SymfonyHttpException\Metadata\ChainMetadataLoader
            arguments:
              - !service { class: Kenny1911\SymfonyHttpException\Metadata\ArrayMetadataLoader }
              - !service
                class: Kenny1911\SymfonyHttpException\Metadata\ConfigMetadataLoader
                arguments:
                  - App\Exceptions\SomeException:
                      statusCode: 401
                      message: 'Not authorized. Error code: { error_code }. { exception_message }'
                      parameters:
                        '{ error_code }': '10420'
                        '{ exception_message }': 'expr(e.getMessage())'
                      translationDomain: message
                      headers:
                        'X-Error-Code': '10420'
                        'X-Error-Message': 'expr(e.getMessage())'
                    App\Exceptions\OtherException:
                      statusCode: 400
        tags:
            - { name: kernel.event_subscriber }
```

Usage
-----

[](#usage)

### Basic Example

[](#basic-example)

```
use Kenny1911\SymfonyHttpException\Attribute\HttpException;
use Symfony\Component\HttpFoundation\Response;

#[HttpException(statusCode: Response::HTTP_FORBIDDEN)]
final class UserIsBlockedException extends Exception {}
```

### Advanced Example

[](#advanced-example)

```
use Kenny1911\SymfonyHttpException\Attribute\HttpException;
use Kenny1911\SymfonyHttpException\ExpressionLanguage\Expression;
use Symfony\Component\HttpFoundation\Response;

#[HttpException(
    statusCode: Response::HTTP_FORBIDDEN,
    message: 'User { username } is blocked. Error code: { error_code }. Reason: { reason }.',
    parameters: [
        '{ error_code }' => '1234',
        '{ username }' => new Expression('e.username'),
        '{ reason }' => new Expression('translator.trans(e.reason)'),
    ],
    translationDomain: 'message',
    headers: [
        'X-Error-Code' => '1024',
        'X-Username' => new Expression('e.username'),
        'X-Reason' => new Expression('translator.trans(e.reason)'),
    ],
)]
final class UserIsBlockedException extends Exception
{
    public function __construct(
        public readonly string $username,
        public readonly string $reason,
    ) {}
}
```

### Built-in Aliases

[](#built-in-aliases)

```
use Kenny1911\SymfonyHttpException\Attribute\AccessDeniedHttpException;

#[AccessDeniedHttpException]
final class UserIsBlockedException extends Exception {}
```

```
use Kenny1911\SymfonyHttpException\Attribute\NotFoundHttpException;

#[NotFoundHttpException]
final class UserNotFoundException extends Exception {}
```

```
use Kenny1911\SymfonyHttpException\Attribute\BadRequestHttpException;

#[BadRequestHttpException]
final class InvalidUsernameException extends Exception {}
```

License
-------

[](#license)

This library is released under the MIT License.

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance52

Moderate activity, may be stable

Popularity19

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

6

Last Release

340d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c929378f19c1e2e6a9525debcf490ec49c991a3d1ac6133e3c9fa45cef184102?d=identicon)[Kenny1911](/maintainers/Kenny1911)

---

Top Contributors

[![Kenny1911](https://avatars.githubusercontent.com/u/25887351?v=4)](https://github.com/Kenny1911 "Kenny1911 (30 commits)")

---

Tags

httpmessagesymfonyconvertcodeexceptionstatus

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/kenny1911-symfony-http-exception/health.svg)

```
[![Health](https://phpackages.com/badges/kenny1911-symfony-http-exception/health.svg)](https://phpackages.com/packages/kenny1911-symfony-http-exception)
```

###  Alternatives

[api-platform/core

Build a fully-featured hypermedia or GraphQL API in minutes!

2.6k51.2M339](/packages/api-platform-core)[easycorp/easyadmin-bundle

Admin generator for Symfony applications

4.3k17.9M387](/packages/easycorp-easyadmin-bundle)[symfony/framework-bundle

Provides a tight integration between Symfony components and the Symfony full-stack framework

3.6k251.7M11.6k](/packages/symfony-framework-bundle)[symfony/security-bundle

Provides a tight integration of the Security component into the Symfony full-stack framework

2.5k185.6M2.4k](/packages/symfony-security-bundle)[drupal/core-recommended

Locked core dependencies; require this project INSTEAD OF drupal/core.

6942.5M420](/packages/drupal-core-recommended)[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.5k5.9M738](/packages/sylius-sylius)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
