Skip to content

Commit c6d55f5

Browse files
jikangjikang
authored andcommitted
init:完整功能
0 parents  commit c6d55f5

17 files changed

Lines changed: 522 additions & 0 deletions

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
build
2+
vendor
3+
.idea
4+
.vscode
5+
.phpunit*
6+
composer.lock

README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# webman exception handler 异常插件
2+
3+
## 安装
4+
5+
```php
6+
composer require jnewer/exception-handler
7+
```
8+
9+
## 配置
10+
11+
`config/exception.php`
12+
13+
```php
14+
return [
15+
// 这里配置异常处理类
16+
'' => \Jnewer\ExceptionHandler\Handler::class,
17+
];
18+
```
19+
20+
> 多应用模式时,你可以为每个应用单独配置异常处理类,参见[多应用](https://www.workerman.net/doc/webman/multiapp.html)
21+
22+
## 基本用法
23+
24+
请求参数错误
25+
26+
```php
27+
use support\Request;
28+
use support\Response;
29+
use Jnewer\ExceptionHandler\Exception\NotFoundHttpException;
30+
31+
class UserController{
32+
public function view($id): Response
33+
{
34+
$user = User::find($id);
35+
if (is_null($user)) {
36+
throw new NotFoundHttpException('用户不存在');
37+
}
38+
}
39+
}
40+
```
41+
42+
以上异常抛出错误信息,如下格式:
43+
44+
```json
45+
HTTP/1.1 404 Not Found
46+
Content-Type: application/json;charset=utf-8
47+
48+
{
49+
"code": 0,
50+
"success": false,
51+
"message": "用户不存在",
52+
"data": [],
53+
}
54+
```
55+
56+
## 内置异常类
57+
58+
- 客户端异常类(HTTP Status 400):NotFoundHttpException
59+
- 身份认证异常类(HTTP Status 401):UnauthorizedHttpException
60+
- 资源授权异常类(HTTP Status 403):ForbiddenHttpException
61+
- 资源未找到异常类(HTTP Status 404):NotFoundHttpException
62+
- 请求方法不允许异常类(HTTP Status 405):MethodNotAllowedHttpException
63+
- 请求内容类型不支持异常类(HTTP Status 406):NotAcceptableHttpException
64+
- 请求限流在异常类(HTTP Status 429):TooManyRequestsHttpException
65+
- 服务器内部错误异常类(HTTP Status 500):ServerErrorHttpException
66+
67+
[更多参考:https://datatracker.ietf.org/doc/html/rfc7231#page-47](https://datatracker.ietf.org/doc/html/rfc7231#page-47)
68+
69+
## 内置异常类Handler
70+
71+
- 扩展自webman的Handler:Handler,可以针对不同的异常类进行不同的处理。
72+
- Http异常处理:HttpExceptionHandler
73+
- laravel验证器异常处理:ValidationExceptionHandler

composer.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "jnewer/exception-handler",
3+
"type": "library",
4+
"license": "MIT",
5+
"description": "Webman plugin jnewer/exception-handler",
6+
"require": {
7+
},
8+
"autoload": {
9+
"psr-4": {
10+
"Jnewer\\ExceptionHandler\\": "src"
11+
}
12+
}
13+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Jnewer\ExceptionHandler\Exception;
4+
5+
/**
6+
* BadRequestHttpException represents a "Bad Request" HTTP exception with status code 400.
7+
*
8+
* Use this exception to represent a generic client error. In many cases, there
9+
* may be an HTTP exception that more precisely describes the error. In that
10+
* case, consider using the more precise exception to provide the user with
11+
* additional information.
12+
*
13+
* @see https://tools.ietf.org/html/rfc7231#section-6.5.1
14+
* @author Jnewer <jeekang@qq.com>
15+
*/
16+
class BadRequestHttpException extends HttpException{
17+
/**
18+
* Constructor.
19+
* @param string $message error message
20+
* @param int $code error code
21+
* @param \Exception $previous The previous exception used for the exception chaining.
22+
*/
23+
public function __construct($message = null, $code = 0, \Exception $previous = null)
24+
{
25+
parent::__construct(400, $message, $code, $previous);
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Jnewer\ExceptionHandler\Exception;
4+
5+
/**
6+
* ForbiddenHttpException represents a "Forbidden" HTTP exception with status code 403.
7+
*
8+
* Use this exception when a user is not allowed to perform the requested action.
9+
* Using different credentials might or might not allow performing the requested action.
10+
* If you do not want to expose authorization information to the user, it is valid
11+
* to respond with a 404 [[NotFoundHttpException]].
12+
*
13+
* @see https://tools.ietf.org/html/rfc7231#section-6.5.3
14+
* @author Jnewer <jeekang@qq.com>
15+
*/
16+
class ForbiddenHttpException extends HttpException{
17+
/**
18+
* Constructor.
19+
* @param string $message error message
20+
* @param int $code error code
21+
* @param \Exception $previous The previous exception used for the exception chaining.
22+
*/
23+
public function __construct($message = null, $code = 0, \Exception $previous = null)
24+
{
25+
parent::__construct(403, $message, $code, $previous);
26+
}
27+
}

src/Exception/HttpException.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
namespace Jnewer\ExceptionHandler\Exception;
3+
use Webman\Http\Request;
4+
use Webman\Http\Response;
5+
6+
class HttpException extends \Exception{
7+
/**
8+
* @var int HTTP status code, such as 403, 404, 500, etc.
9+
*/
10+
public $statusCode;
11+
12+
/**
13+
* Constructor.
14+
* @param int $status HTTP status code, such as 404, 500, etc.
15+
* @param string $message error message
16+
* @param int $code error code
17+
* @param \Exception $previous The previous exception used for the exception chaining.
18+
*/
19+
public function __construct($statusCode, $message = null, $code = 0, \Exception $previous = null)
20+
{
21+
$this->statusCode = $statusCode;
22+
if(!$message){
23+
$message = Response::$_phrases[$statusCode] ?? '';
24+
}
25+
26+
parent::__construct($message, $code, $previous);
27+
}
28+
29+
public function render(Request $request, \Throwable $exception): Response
30+
{
31+
/** @var ValidationException $exception */
32+
$message = $exception->validator->errors()->first();
33+
if (!$request->expectsJson()) {
34+
return new Response($exception->statusCode, [], $message);
35+
}
36+
37+
$jsonMessage = ['code' => $exception->code ?: $exception->statusCode, 'message' => $message, 'success' => false, 'data' => []];
38+
return new Response(
39+
$exception->statusCode,
40+
['Content-Type' => 'application/json'],
41+
json_encode($jsonMessage, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
42+
);
43+
}
44+
45+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Jnewer\ExceptionHandler\Exception;
4+
5+
/**
6+
* MethodNotAllowedHttpException represents a "Method Not Allowed" HTTP exception with status code 405.
7+
*
8+
* @see https://tools.ietf.org/html/rfc7231#section-6.5.5
9+
* @author Jnewer <jeekang@qq.com>
10+
*/
11+
class MethodNotAllowedHttpException extends HttpException{
12+
/**
13+
* Constructor.
14+
* @param string $message error message
15+
* @param int $code error code
16+
* @param \Exception $previous The previous exception used for the exception chaining.
17+
*/
18+
public function __construct($message = null, $code = 0, \Exception $previous = null)
19+
{
20+
parent::__construct(405, $message, $code, $previous);
21+
}
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Jnewer\ExceptionHandler\Exception;
4+
5+
/**
6+
* NotAcceptableHttpException represents a "Not Acceptable" HTTP exception with status code 406.
7+
*
8+
* Use this exception when the client requests a Content-Type that your
9+
* application cannot return. Note that, according to the HTTP 1.1 specification,
10+
* you are not required to respond with this status code in this situation.
11+
*
12+
* @see https://tools.ietf.org/html/rfc7231#section-6.5.6
13+
* @author Jnewer <jeekang@qq.com>
14+
*/
15+
class NotAcceptableHttpException extends HttpException{
16+
/**
17+
* Constructor.
18+
* @param string $message error message
19+
* @param int $code error code
20+
* @param \Exception $previous The previous exception used for the exception chaining.
21+
*/
22+
public function __construct($message = null, $code = 0, \Exception $previous = null)
23+
{
24+
parent::__construct(406, $message, $code, $previous);
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Jnewer\ExceptionHandler\Exception;
4+
5+
/**
6+
* NotFoundHttpException represents a "Not Found" HTTP exception with status code 404.
7+
*
8+
* @see https://tools.ietf.org/html/rfc7231#section-6.5.4
9+
* @author Jnewer <jeekang@qq.com>
10+
*/
11+
class NotFoundHttpException extends HttpException{
12+
/**
13+
* Constructor.
14+
* @param string $message error message
15+
* @param int $code error code
16+
* @param \Exception $previous The previous exception used for the exception chaining.
17+
*/
18+
public function __construct($message = null, $code = 0, \Exception $previous = null)
19+
{
20+
parent::__construct(404, $message, $code, $previous);
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Jnewer\ExceptionHandler\Exception;
4+
5+
/**
6+
* ServerErrorHttpException represents an "Internal Server Error" HTTP exception with status code 500.
7+
*
8+
* @see https://tools.ietf.org/html/rfc7231#section-6.6.1
9+
* @author Jnewer <jeekang@qq.com>
10+
*/
11+
class ServerErrorHttpException extends HttpException{
12+
/**
13+
* Constructor.
14+
* @param string $message error message
15+
* @param int $code error code
16+
* @param \Exception $previous The previous exception used for the exception chaining.
17+
*/
18+
public function __construct($message = null, $code = 0, \Exception $previous = null)
19+
{
20+
parent::__construct(500, $message, $code, $previous);
21+
}
22+
}

0 commit comments

Comments
 (0)