Skip to content

Commit aa2bb4d

Browse files
authored
Merge pull request kvnZero#10 from kvnZero/feat/完善错误返回
update 修改一些错误信息 同步官方
2 parents df5599b + d0e6076 commit aa2bb4d

File tree

4 files changed

+72
-25
lines changed

4 files changed

+72
-25
lines changed

app/ApiJson/ApiJson.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ public function Query(): array
1616
{
1717
if (!is_array(json_decode($this->request->getBody()->getContents(), true))) {
1818
return [
19-
'code' => ResponseCode::SERVER_ERROR,
20-
'msg' => ResponseCode::getMessage(ResponseCode::SERVER_ERROR)
19+
'code' => ResponseCode::CODE_UNSUPPORTED_ENCODING,
20+
'msg' => ResponseCode::getMessage(ResponseCode::CODE_UNSUPPORTED_ENCODING)
2121
];
2222
}
2323
$parse = new Parse(json_decode($this->request->getBody()->getContents(), true), $this->method, $this->request->input('tag', ''));
2424
return array_merge([
25-
'code' => ResponseCode::SUCCESS,
26-
'msg' => ResponseCode::getMessage(ResponseCode::SUCCESS)
25+
'code' => ResponseCode::CODE_SUCCESS,
26+
'msg' => ResponseCode::getMessage(ResponseCode::CODE_SUCCESS)
2727
], $parse->handle());
2828
}
2929
}

app/Constants/ResponseCode.php

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,73 @@
2020
class ResponseCode extends AbstractConstants
2121
{
2222
/**
23-
* @Message("Success!")
23+
* @Message("success")
24+
* copy:https://github.com/Tencent/APIJSON/blob/master/APIJSONORM/src/main/java/apijson/JSONResponse.java#L40
2425
*/
25-
public const SUCCESS = 200;
26+
public const CODE_SUCCESS = 200;
2627

2728
/**
28-
* @Message("Server Error!")
29+
* @Message("编码错误")
2930
*/
30-
public const SERVER_ERROR = 500;
31+
public const CODE_UNSUPPORTED_ENCODING = 400;
32+
33+
/**
34+
* @Message("权限错误")
35+
*/
36+
public const CODE_ILLEGAL_ACCESS = 401;
37+
38+
/**
39+
* @Message("禁止操作")
40+
*/
41+
public const CODE_UNSUPPORTED_OPERATION = 403;
42+
43+
/**
44+
* @Message("未找到")
45+
*/
46+
public const CODE_NOT_FOUND = 404;
47+
48+
/**
49+
* @Message("参数错误")
50+
*/
51+
public const CODE_ILLEGAL_ARGUMENT = 406;
52+
53+
/**
54+
* @Message("未登录")
55+
*/
56+
public const CODE_NOT_LOGGED_IN = 407;
57+
58+
/**
59+
* @Message("超时")
60+
*/
61+
public const CODE_TIME_OUT = 408;
62+
63+
/**
64+
* @Message("重复,已存在")
65+
*/
66+
public const CODE_CONFLICT = 409;
67+
68+
/**
69+
* @Message("条件错误,如密码错误")
70+
*/
71+
public const CODE_CONDITION_ERROR = 412;
72+
73+
/**
74+
* @Message("类型错误")
75+
*/
76+
public const CODE_UNSUPPORTED_TYPE = 415;
77+
78+
/**
79+
* @Message("超出范围")
80+
*/
81+
public const CODE_OUT_OF_RANGE = 416;
82+
83+
/**
84+
* @Message("对象为空")
85+
*/
86+
public const CODE_NULL_POINTER = 417;
87+
88+
/**
89+
* @Message("服务器内部错误")
90+
*/
91+
public const CODE_SERVER_ERROR = 500;
3192
}

app/Exception/Handler/AppExceptionHandler.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,18 @@
1212
namespace App\Exception\Handler;
1313

1414
use App\Constants\ResponseCode;
15-
use Hyperf\Contract\StdoutLoggerInterface;
1615
use Hyperf\ExceptionHandler\ExceptionHandler;
1716
use Hyperf\HttpMessage\Stream\SwooleStream;
1817
use Psr\Http\Message\ResponseInterface;
1918
use Throwable;
2019

2120
class AppExceptionHandler extends ExceptionHandler
2221
{
23-
/**
24-
* @var StdoutLoggerInterface
25-
*/
26-
protected $logger;
27-
28-
public function __construct(StdoutLoggerInterface $logger)
29-
{
30-
$this->logger = $logger;
31-
}
32-
3322
public function handle(Throwable $throwable, ResponseInterface $response)
3423
{
35-
$this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()));
36-
$this->logger->error($throwable->getTraceAsString());
37-
return $response->withHeader('Server', 'Hyperf')->withStatus(500)->withBody(new SwooleStream(json_encode([
38-
'code' => ResponseCode::SERVER_ERROR,
39-
'msg' => ResponseCode::getMessage(ResponseCode::SERVER_ERROR)
24+
return $response->withHeader('Server', 'Hyperf')->withHeader('Content-Type', 'application/json')->withStatus(500)->withBody(new SwooleStream(json_encode([
25+
'code' => $throwable->getCode() ? $throwable->getCode() : ResponseCode::CODE_SERVER_ERROR,
26+
'msg' => $throwable->getCode() ? $throwable->getMessage() : ResponseCode::getMessage(ResponseCode::CODE_SERVER_ERROR)
4027
])));
4128
}
4229

config/autoload/exceptions.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
return [
1313
'handler' => [
1414
'http' => [
15-
Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler::class,
1615
App\Exception\Handler\AppExceptionHandler::class,
1716
],
1817
],

0 commit comments

Comments
 (0)