aslnbxrz/simple-exception

A comprehensive exception handling package for Laravel with custom error responses and enum-based error codes

2.0.2 2025-09-26 04:56 UTC

README

Latest Version on Packagist Total Downloads License

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.php
  • lang/vendor/simple-exception/en/user.php
  • lang/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-code command with --cases and --locale
    • Unified translation folder structure: lang/vendor/simple-exception/{locale}/{file}.php
    • Improved helper functions (error_if, error_unless, etc.)
    • Cleaner SimpleErrorResponse API: resolvedHttpCode(), resolvedCode()
  • v1.0.x

    • Initial release with enum-based exceptions and helpers

๐Ÿค Contributing

  1. Fork
  2. Create feature branch
  3. Commit changes
  4. Open PR

๐Ÿ“„ License

MIT ยฉ aslnbxrz