aslnbxrz/simple-exception
Composer 安装命令:
composer require aslnbxrz/simple-exception
包简介
A comprehensive exception handling package for Laravel with custom error responses and enum-based error codes
README 文档
README
A modern exception handling package for Laravel with enum-based error codes, automatic translation sync, and clean JSON API responses.
🚀 Features
- ✅ Enum-based error codes (e.g.
MainRespCode,UserRespCode) - ✅ Helper functions:
error(),error_if(),error_unless(),error_response() - ✅ Automatic translation sync: keep enum cases in sync with
lang/files - ✅ Configurable: response structure, error keys, caching
- ✅ Laravel-ready: Service provider, config publish, artisan commands
- ✅ Works with Laravel 9 → 12+
📦 Installation
composer require aslnbxrz/simple-exception
Publish config:
php artisan vendor:publish --tag=simple-exception-config
This creates config/simple-exception.php.
⚙️ Configuration
Example
'response' => [ 'template' => 'default', 'templates' => [ 'default' => [ 'success' => ':success', 'data' => ':data', 'error' => [ 'message' => ':message', 'code' => ':code', ], 'meta' => ':meta', ], ], ], 'default_error_code' => -1, 'enum_generation' => [ 'resp_codes_dir' => 'Enums/RespCodes', // relative to app/ ], 'translations' => [ 'base_path' => 'vendor/simple-exception', ],
🎯 Quick Start
Step 1 – Generate an Enum
php artisan make:resp-code User --cases="NotFound=404,Forbidden=403" --locale=en,uz
This creates:
app/Enums/RespCodes/UserRespCode.phplang/vendor/simple-exception/en/user.phplang/vendor/simple-exception/uz/user.php
Step 2 – Throw Errors
use App\Enums\RespCodes\UserRespCode; // Always throws SimpleErrorResponse error(UserRespCode::NotFound); // Conditional helpers error_if(!$user, UserRespCode::NotFound); error_unless($user->can('update'), UserRespCode::Forbidden); // Custom string error error_response('Custom failure', 1001);
Step 3 – Example Controller
use App\Enums\RespCodes\UserRespCode; class UserController extends Controller { public function show($id) { $user = User::find($id); error_if(!$user, UserRespCode::NotFound); return response()->json(['user' => $user]); } }
🌍 Translation Management
Sync all enums
php artisan sync:resp-translations --all
Sync one enum
php artisan sync:resp-translations UserRespCode --locale=uz
Output:
📋 Found 1 enum(s).
🔄 Syncing App\Enums\RespCodes\UserRespCode
✅ lang/vendor/simple-exception/uz/user.php created/updated
Example Translation File
lang/vendor/simple-exception/en/user.php
<?php return [ 'not_found' => 'User not found', 'forbidden' => 'Access denied', ];
🎨 Response Format
Success
{
"success": true,
"data": { "id": 1, "name": "Bexruz" },
"error": null,
"meta": []
}
Error
{
"success": false,
"data": null,
"error": {
"message": "User not found",
"code": 404
},
"meta": []
}
Error (Debug mode)
{
"success": false,
"data": null,
"error": {
"message": "User not found",
"code": 404
},
"meta": {
"file": "/app/Http/Controllers/UserController.php",
"line": 15,
"trace": [...]
}
}
📋 Available Commands
| Command | Description |
|---|---|
php artisan make:resp-code {name} |
Generate a new error enum (with translations) |
php artisan sync:resp-translations {enum?} |
Sync translations for a single enum or all enums |
php artisan vendor:publish --tag=simple-exception-config |
Publish package config |
🧪 Testing
composer test
Runs all package unit tests (artisan commands, exception handling, translation sync).
📝 Changelog
-
v1.1.0
- Added
make:resp-codecommand with--casesand--locale - Unified translation folder structure:
lang/vendor/simple-exception/{locale}/{file}.php - Improved helper functions (
error_if,error_unless, etc.) - Cleaner
SimpleErrorResponseAPI:resolvedHttpCode(),resolvedCode()
- Added
-
v1.0.x
- Initial release with enum-based exceptions and helpers
🤝 Contributing
- Fork
- Create feature branch
- Commit changes
- Open PR
📄 License
MIT © aslnbxrz
aslnbxrz/simple-exception 适用场景与选型建议
aslnbxrz/simple-exception 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 195 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 09 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「exception」 「response」 「laravel」 「error-handling」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 aslnbxrz/simple-exception 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aslnbxrz/simple-exception 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 aslnbxrz/simple-exception 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
HTTP request logger middleware for Laravel
A PSR-7 compatible library for making CRUD API endpoints
Class to generate a standard structure for api json responses
Error and Exception override and observers.
Error handler based on Booboo with HTML and JSON support
Standard PHP Library Extension featuring an ExceptionInterface.
统计信息
- 总下载量: 195
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-05