PHPackages                             jnewer/exception-handler - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Debugging &amp; Profiling](/categories/debugging)
4. /
5. jnewer/exception-handler

ActiveLibrary[Debugging &amp; Profiling](/categories/debugging)

jnewer/exception-handler
========================

Webman plugin jnewer/exception-handler

v1.1.0(12mo ago)2141MITPHPPHP &gt;=8.1

Since Jun 7Pushed 12mo ago1 watchersCompare

[ Source](https://github.com/jnewer/exception-handler)[ Packagist](https://packagist.org/packages/jnewer/exception-handler)[ RSS](/packages/jnewer-exception-handler/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (3)Versions (10)Used By (0)

webman exception handler 异常插件
=============================

[](#webman-exception-handler-异常插件)

[![Latest Version on Packagist](https://camo.githubusercontent.com/6ed747b4eb5e2a3f6d6c00d5bc1634c0625243f05b604f78c6dc9e998f55cd45/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a6e657765722f657863657074696f6e2d68616e646c65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jnewer/exception-handler)[![Total Downloads](https://camo.githubusercontent.com/a5501712528fa29ee22664692c08625e2108a21143d7b6fa319ede3cb460d64c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a6e657765722f657863657074696f6e2d68616e646c65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jnewer/exception-handler)[![License](https://camo.githubusercontent.com/84a886ed7d1be93f3dccfa9f17249c822931a876327d426df4de5c69244cead5/687474703a2f2f706f7365722e707567782e6f72672f6a6e657765722f657863657074696f6e2d68616e646c65722f6c6963656e7365)](https://packagist.org/packages/jnewer/exception-handler)[![PHP Version Require](https://camo.githubusercontent.com/73ba46ae318395c4272575505052f92c7bcbbc0b13312e7c31530435298dd8d8/687474703a2f2f706f7365722e707567782e6f72672f6a6e657765722f657863657074696f6e2d68616e646c65722f726571756972652f706870)](https://packagist.org/packages/jnewer/exception-handler)

该插件扩展了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,
        ]
    ]
];
```

> 多应用模式时，你可以为每个应用单独配置异常处理类，参见[多应用](https://www.workerman.net/doc/webman/multiapp.html)

基本用法
----

[](#基本用法)

```
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](https://datatracker.ietf.org/doc/html/rfc7231#page-47)

自定义异常类
------

[](#自定义异常类)

```
