hamidreza2005/laravel-api-error-handler 问题修复 & 功能扩展

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

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

hamidreza2005/laravel-api-error-handler

Composer 安装命令:

composer require hamidreza2005/laravel-api-error-handler

包简介

a package for handle api errors

README 文档

README

a useful package for handling exception in laravel.

📥 Installation

you can install this package via Composer:

composer require hamidreza2005/laravel-api-error-handler

and after installation you can run following command to publish config files

php artisan vendor:publish --tag laravel-api-error-handler

⚙️ Configuration

to configure this package go to config/api-error-handler.php

<?php  
  
return [  
    /*
     * this is where you define which handler deal with which errors. each handler can handle multiple errors
     */

    "handlers" =>[
        NotFoundExceptionHandler::class => [
            "Symfony\Component\HttpKernel\Exception\NotFoundHttpException",
            "Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException"
        ],
        ServerInternalExceptionHandler::class => [
            "ErrorException",
            "Illuminate\Database\QueryException"
        ],
        AccessDeniedExceptionHandler::class => [
            "Illuminate\Auth\AuthenticationException",
            "Symfony\Component\HttpKernel\Exception\HttpException"
        ],
        ValidationExceptionHandler::class => [
            "Illuminate\Validation\ValidationException"
        ],
    ],

    /*
     * if the app is not in debug mode. all unknown exceptions will be handled by this.
     */
    "internal_error_handler" => ServerInternalExceptionHandler::class,
];

this package provides some handlers for common exceptions like ModelNotFound But if you want to customize it you can do like this :

<?php  
  
return [  
  YourHandler::class => [
        // exceptions
    ],   
];
Class Status Code Message
NotFoundException 404 Not Found
ServerInternalException 500 Server Internal Error
AccessDeniedException 403 Access Denied
ValidationException 422 all validation errors
DefaultException the status code of the Exception the message of the Exception

🚀 how to let handlers do their job ?

add ApiErrorHandler trait to ExceptionHandler located in app\Exceptions\Handler.php:

<?php  
  
namespace App\Exceptions;  
  
use hamidreza2005\LaravelApiErrorHandler\Traits\ApiErrorHandler;  
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;  
use Throwable;  
  
class Handler extends ExceptionHandler  
{  
	  use ApiErrorHandler;  
	  /**  
	 * A list of the exception types that are not reported. * * @var 		array  
	 */  
	 protected $dontReport = [  
		  //  
	  ];  
  
	  /**  
	 * A list of the inputs that are never flashed for validation exceptions. * * @var array  
	 */  
	 protected $dontFlash = [  
		  'password',  
		  'password_confirmation',  
	 ];  
	  /**  
	 * Register the exception handling callbacks for the application. * * @return void  
	 */
	 public function register()  
	 {
	   //  
	 }  
  
	 public function render($request, Throwable $e)  
	 {
		  return $this->handle($this->prepareException($e));  
	 }
 }

Register app\Exceptions\Handler.php in AppServiceProvider:

<?php  
  
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Debug\ExceptionHandler;
use App\Exceptions\Handler;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        $this->app->singleton(
            ExceptionHandler::class,
            Handler::class
        );
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        //
    }
}

Make Your Own Exception Handler!

if you want to make your own handler your class has to extend hamidreza2005\LaravelApiErrorHandler\Handlers\ExceptionHandler

i.e:

<?php  

class MyException extends ExceptionHandler  
{  
	  public function handleStatusCode():void  
	  {  
		  $this->statusCode = 499;  
	  }
	   
	  public function handleMessage():void  
	  {  
		  $this->message = "My Message";  
	  }
 }

📜 License

The MIT License (MIT). Please see License File for more information.

🙋 Contributing

If you find an issue, or have a better way to do something, feel free to open an issue , or a pull request.

hamidreza2005/laravel-api-error-handler 适用场景与选型建议

hamidreza2005/laravel-api-error-handler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.32k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2020 年 10 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「error-handling」 「api-error-handler」 「laravel-api-error-handler」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 hamidreza2005/laravel-api-error-handler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hamidreza2005/laravel-api-error-handler 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-25