承接 harrisonratcliffe/laravel-api-error-handler 相关项目开发

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

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

harrisonratcliffe/laravel-api-error-handler

Composer 安装命令:

composer require harrisonratcliffe/laravel-api-error-handler

包简介

A useful package to handle your Laravel API exceptions.

README 文档

README

Laravel API Error Handler is a package designed to handle exceptions when developing APIs in Laravel.

This package is a fork of hamidreza2005/laravel-api-error-handler to work with Laravel 9 and below.

📥 Installation

You can install this package via Composer:

composer require harrisonratcliffe/laravel-api-error-handler

After installation, run the following command to publish the configuration files:

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

⚙️ Configuration

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

<?php  
  
return [  
  "Symfony\Component\HttpKernel\Exception\NotFoundHttpException" => "\harrisonratcliffe\LaravelApiErrorHandler\Exceptions\NotFoundException",  
  "ErrorException" => "\harrisonratcliffe\LaravelApiErrorHandler\Exceptions\ServerInternalException",  
  "Illuminate\Database\QueryException" => "\harrisonratcliffe\LaravelApiErrorHandler\Exceptions\ServerInternalException",  
  "Illuminate\Auth\AuthenticationException" => "\harrisonratcliffe\LaravelApiErrorHandler\Exceptions\AccessDeniedException",  
  "Symfony\Component\HttpKernel\Exception\HttpException" => "\harrisonratcliffe\LaravelApiErrorHandler\Exceptions\AccessDeniedException",  
  "Illuminate\Validation\ValidationException" => "\harrisonratcliffe\LaravelApiErrorHandler\Exceptions\ValidationException", 
  "Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException"=>"\harrisonratcliffe\LaravelApiErrorHandler\Exceptions\NotFoundException", 
];

This package provides some common exceptions like ModelNotFound. If you want to customize it, you can do it like this:

<?php  
  
return [  
  "Your Error Namespace" => "the class that handle this error",   
];
Class Status Code Message
NotFoundException 404 Not Found
ServerInternalException 500 Server Internal Error
AccessDeniedException 403 Access Denied
ValidationException 422 it returns all errors in validation
DefaultException the status code of the Error the message of the Error

🚀 Let This Package Handle Your Errors

Go to app\Exceptions\Handler.php and add the following code:

<?php  
  
namespace App\Exceptions;  
  
use harrisonratcliffe\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->handleError($this->prepareException($e));  
	 }
 }

✍️ Create Your Own Error Handler If you want to create your own handler instead of using the default handler, you can create a class that extends harrisonratcliffe\LaravelApiErrorHandler\Exceptions\ExceptionAbstract in any location.

For example:

<?php  
  
  
namespace myNamespace;  
  
  
class MyException extends ExceptionAbstract  
{  
	  public function handleStatusCode():void  
	  {  
		  $this->statusCode = 2018;  
	  }
	   
	  public function handleMessage():void  
	  {  
		  $this->message = "My Message";  
	  }
 }

And you can then declare your handler in config/api-error-handler.php:

<?php

return [
	"ErrorException" => "myNamespace\MyException"
];

❗ Notice

If an unknown exception appears, this package automatically shows it in the response. However, you can prevent this by setting APP_DEBUG to false in your .env file. When APP_DEBUG is false, "Server Internal Error" is shown in the response.

📜 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.

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-09-25