异常增强插件

v1.0.7 版本
2024-08-16 版本更新时间
47 安装
1 star

简介

该插件扩展了webman的Handler类,webman的Handler只能对同一应用下的异常进行统一处理,而该插件可以针对同一应用的不同异常进行不同的处理。

安装

composer require jnewer/exception-handler

配置

config/exception.php

return [
    // 这里配置异常处理类,有单独配置Handler的异常,会自动分发给对应的Handler处理
    '' => \Jnewer\ExceptionHandler\Handler::class,
];

config/plugin/jnewer/exception-handler.php


use Illuminate\Validation\ValidationException;
use Jnewer\ExceptionHandler\Exception\BaseException;
use Jnewer\ExceptionHandler\BaseExceptionHandler;
use Jnewer\ExceptionHandler\ValidationExceptionHandler;

return [
    'enable' => true,
    'exception' => [
        // 这里配置异常类和对应的处理类
        'handlers' => [
            ValidationException::class => ValidationExceptionHandler::class,
            BaseException::class => BaseExceptionHandler::class
        ],
        'dont_report' => [
            BusinessException::class,
            BaseException::class,
            ValidationException::class,
        ]
    ]
];

多应用模式时,你可以为每个应用单独配置异常处理类,参见多应用

基本用法

use support\Request;
use support\Response;
use Jnewer\ExceptionHandler\Exception\NotFoundHttpException;

class UserController{
    public function view($id): Response
    {
        $user = User::find($id);
        if (is_null($user)) {
            throw new NotFoundHttpException('用户不存在');
        }
    }
}

以上异常抛出错误信息,如下格式:

HTTP/1.1 404 Not Found
Content-Type: application/json;charset=utf-8

{
    "code": 0,
    "success": false,
    "message": "用户不存在",
    "data": [],
}

内置异常类

  • 客户端异常类(HTTP Status 400):NotFoundHttpException
  • 身份认证异常类(HTTP Status 401):UnauthorizedHttpException
  • 资源授权异常类(HTTP Status 403):ForbiddenHttpException
  • 资源未找到异常类(HTTP Status 404):NotFoundHttpException
  • 请求方法不允许异常类(HTTP Status 405):MethodNotAllowedHttpException
  • 请求内容类型不支持异常类(HTTP Status 406):NotAcceptableHttpException
  • 请求限流在异常类(HTTP Status 429):TooManyRequestsHttpException
  • 服务器内部错误异常类(HTTP Status 500):ServerErrorHttpException

更多参考:https://datatracker.ietf.org/doc/html/rfc7231#page-47

自定义异常类

<?php

namespace support\exception;

use Jnewer\ExceptionHandler\Exception\BaseException;

class InvalidArgumentException extends BaseException
{
}

使用异常类

use support\Request;
use support\Response;
use support\exception\InvalidArgumentException;

class UserController{
    public function create(Request $request): Response
    {
        if (!$request->post('name')) {
            throw new InvalidArgumentException('参数有误');
        }
    }
}

内置异常类Handler

  • 扩展自webman的Handler:Handler,可以针对不同的异常类进行不同的处理。
  • BaseException异常处理:BaseExceptionHandler
  • laravel验证器异常处理:ValidationExceptionHandler

自定义异常类Handler

<?php

namespace support\exception;
use support\exception\Handler;
use Illuminate\Validation\ValidationException;
use Throwable;
use function json_encode;

class InvalidArgumentExceptionHandler extends Handler
{
    public function render(Request $request, Throwable $exception): Response
    {
        $message = $exception->getMessage();
        $statusCode = $exception->statusCode ?? 500;
        if (!$request->expectsJson()) {
            return new Response($statusCode, [], $message);
        }

        $jsonMessage = ['code' => $exception->code ?: $exception->statusCode, 'message' => $message, 'success' => false, 'data' => []];
        return new Response(
            $statusCode,
            ['Content-Type' => 'application/json'],
            json_encode($jsonMessage, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
        );
    }
}

配置Handler

config/plugin/jnewer/exception-handler.php


use app\exception\InvalidArgumentException;
use app\exception\InvalidArgumentExceptionHandler;

return [
    'exception' => [
        // 这里配置异常类和对应的处理类
        'handlers' => [
            ...// 其他异常和处理类
            InvalidArgumentException::class => InvalidArgumentExceptionHandler::class
        ]
    ]
];

效果图(可选)

赞助商