vaened/laravception
Composer 安装命令:
composer require vaened/laravception
包简介
Standardize your error responses in Laravel with Laravception. Get consistent error codes, messages, metadata, and translation support.
README 文档
README
Laravception standardizes error responses in Laravel, ensuring all exceptions have a unique code, clear message, and metadata. It supports error message translation, promotes specific exception classes, and adapts details based on the environment.
Installation
Laravception supports Laravel 11, 12, and 13.
Version compatibility
- Laravel 11 / 12: PHP 8.2+
- Laravel 13: PHP 8.3+
To get the latest version, simply require the project using Composer:
composer require vaened/laravception
Now. Publish the configuration file.
php artisan vendor:publish --tag='laravception'
Usage
Laravception captures exceptions thrown by the application and standardizes their structure. The unified structure is as follows:
{
"code": "Exception error code",
"message": "Exception message",
"params": {
"Exception parameters"
},
"meta": [
"Metadata that the exception might export"
],
"tags": [
"Classifications tags for the exception"
],
"exception": "Exception name",
"file": "File where exception was thrown",
"line": "Line that threw the exception",
"trace": [
"Full stack trace of the exception"
]
}
This structure will hide certain details based on whether the environment is set to development or production.
Translations
To enable exception translation, implement the interface TranslatableException. This is sufficient to start translating child exceptions.
final class InvalidCreditCardException extends RuntimeException implements TranslatableException { // this is enough }
It’s recommended to create a custom exception class for each error to allow more specific translations. For example, with the exception “InvalidCreditCardException”, you can have a translation entry in the exceptions file:exceptions:
[ "invalid_credit_card_exception" => "La tarjeta de credito ingresada no es válida". ]
This functionality is enabled by default. Any exceptions that do not implement Codeable will be treated in the same manner, converting the class name to snake_case and using this transformation as the error code and key for the translation.
For better error code management, you can implement Codeable. In the exception "InvalidCreditCardException", you can return a custom error code such as payments.invalid_credit_card, and in your translation file:
[ "payments" => [ "invalid_credit_card" => "La tarjeta de crédito ingresada no es válida". ] ]
You can also add parameters by implementing the Parametrizable interface. Return an array with the properties to be used, and your translation file will look like this:
[ "payments" => [ "invalid_credit_card" => "La tarjeta de crédito <:card_number> no es válida". ] ]
We can finally have this implementation:
final class InvalidCreditCardException extends RuntimeException implements TranslatableException, Codeable, Parametrizable { public function __construct(private readonly string $cardNumber) { parent::__construct("The credit card number <$cardNumber> is invalid"); } public function errorCode(): string { return 'payments.invalid_credit_card'; } public function parameters(): array { return [ 'card_number' => $this->cardNumber, ]; } }
Configuration
To understand the various configuration options, please refer to the comments in the configuration file laravception.php.
Principles
The core idea behind this library is to standardize error messages in a Laravel project, ensuring that all exceptions follow a common structure.
Specific Exceptions
To improve API organization and documentation, it is recommended to create custom exceptions with a specific purpose, such as "PaymentFailedException" or "ClientNotFoundException" This allows for more precise and effective handling of exceptions.
Translation of Messages
With specific exceptions, presenting error messages can be automated using unique codes. This makes it easier to translate and decorate error messages, ensuring they are clear and useful for both developers and end-users.
License
This library is licensed under the MIT License. For more information, please see the license file.
vaened/laravception 适用场景与选型建议
vaened/laravception 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 133.63k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 08 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「exceptions」 「error-handling」 「laravel-library」 「exceptions-handling」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vaened/laravception 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vaened/laravception 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vaened/laravception 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
php7ify is a project that brings new php7 classes and exceptions to php 5.x.
An enhanced Laravel extension to export named routes to JavaScript, allowing you to use route names instead of URLs. Supports route grouping for modular applications.
Logging and Notifications for Laravel Console Commands.
The CodenamePHP Platform core that contains basic interfaces, exceptions etc..
Enables simple 404 Page NotFound handling, also for multilingual sites.
Prevent sensitive exception details from leaking in Laravel applications
统计信息
- 总下载量: 133.63k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-08-07