aslnbxrz / simple-exception
A comprehensive exception handling package for Laravel with custom error responses and enum-based error codes
Installs: 43
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/aslnbxrz/simple-exception
Requires
- php: ^8.2
- illuminate/console: ^10.0|^11.0|^12.0
- illuminate/http: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
- illuminate/validation: ^10.0|^11.0|^12.0
- symfony/http-foundation: ^6.0|^7.0
Requires (Dev)
- mockery/mockery: ^1.4
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.0|^11.0
- dev-main
- 2.0.2
- 2.0.1
- 2.0.0
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.35
- 1.0.34
- 1.0.33
- 1.0.32
- 1.0.31
- 1.0.30
- 1.0.29
- 1.0.28
- 1.0.27
- 1.0.26
- 1.0.25
- 1.0.24
- 1.0.23
- 1.0.22
- 1.0.21
- 1.0.20
- 1.0.19
- 1.0.18
- 1.0.17
- 1.0.16
- 1.0.15
- 1.0.14
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2025-10-26 05:11:11 UTC
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