aslnbxrz/simple-exception
最新稳定版本:2.0.2
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
统计信息
- 总下载量: 57
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-05