aslnbxrz/simple-exception 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 195
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-05