承接 jccoca/laravel-api-exceptions 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

jccoca/laravel-api-exceptions

Composer 安装命令:

composer require jccoca/laravel-api-exceptions

包简介

A clean and highly customizable REST API Exception Handler for Laravel.

README 文档

README

Latest Version on Packagist Total Downloads License

🇧🇷 Para a versão em Português, clique aqui

A lightweight and elegant package to standardize and customize HTTP error and exception responses in Laravel 11, 12, and 13+ REST APIs.

✨ Features

  • Auto-Format Errors: Catch common API errors (401, 403, 404, 405, 422, 500) and return a consistent JSON payload structure.
  • ⚙️ Highly Customizable: Easily change both the error title (error) and the explanation (message) for any exception using a simple config file.
  • 🛠️ Force JSON Middleware: Automatically forces all /api prefixed routes to return application/json transparently.
  • 🐛 Smart Debugging: Shows file, line, and a clean, simplified stack trace when APP_DEBUG=true.
  • 🛡️ Production-Safe: Automatically hides database details and PHP error messages when the application is in production mode.

🚀 Installation

You can install the package via Composer:

composer require jccoca/laravel-api-exceptions

🛠️ Configuration

1. Bind the Handler in Laravel

Open your Laravel application's bootstrap/app.php file and call the handler inside the withExceptions block:

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Foundation\Configuration\Exceptions;
use JCCoca\ApiExceptions\Http\Middleware\ForceJsonResponse;
use JCCoca\ApiExceptions\Exceptions\Handlers\ApiExceptionHandler;

return Application::configure(basePath: dirname(__DIR__))
    // ...
    ->withMiddleware(function (Middleware $middleware): void {
        $middleware->api(append: [
            // Append the Force JSON middleware to the api group
            ForceJsonResponse::class,
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        // Binds the smart API exception handler
        ApiExceptionHandler::bind($exceptions);
    })->create();

2. Publish Configuration (Optional)

If you want to translate or change the default error titles and messages, publish the config file using the Artisan command:

php artisan vendor:publish --tag=api-exceptions-config

This will create a config/api-exceptions.php file in your project.

📝 Default Output Formats (Examples)

Validation Error (422 Unprocessable Content)

{
  "error": "Unprocessable Content.",
  "message": "The provided data failed validation checks.",
  "errors": {
    "sort_direction": [
      "The selected sort direction is invalid."
    ]
  }
}

Too Many Requests Error (429 Rate Limit)

{
  "error": "Too Many Requests.",
  "message": "You have exceeded your rate limit. Please try again later."
}

Integration / External API Error (502 Bad Gateway - Debug Enabled)

{
  "error": "Bad Gateway.",
  "message": "An error occurred while communicating with an upstream service.",
  "debug": {
    "upstream_status": 500,
    "upstream_url": "https://api.external-service.com/v1/users",
    "upstream_response": {
      "success": false,
      "message": "Fatal database failure in external service."
    }
  }
}

Internal Server Error (500 with Debug Enabled)

{
  "error": "Internal Server Error.",
  "message": "Division by zero",
  "debug": {
    "exception": "DivisionByZeroError",
    "file": "/var/www/html/app/Http/Controllers/UserController.php",
    "line": 42,
    "trace": [
      {
        "file": "/var/www/html/app/Http/Controllers/UserController.php",
        "line": 42,
        "function": "index",
        "class": "App\\Http\\Controllers\\UserController"
      }
    ]
  }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-07-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固