定制 vaened/laravception 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

Build Status Software License

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.

Carbon source code

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 vaened/laravception 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 133.63k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-07