-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathError.php
222 lines (187 loc) · 5.52 KB
/
Error.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/*
* This file is part of the chomyeong/errorcode.
*
* (c) chomyeong <chomyeong.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* Error.php.
* 通用API 错误码
* @author chomyeong <mme7wan@gmail.com>
* @copyright 2020 chomyeong <mme7wan@gmail.com>
*
* @see https://github.com/chomyeong
* @see http://chomyeong.me
*/
namespace chomyeong\errorcode;
use Pimple\Container;
class Error extends Container
{
private $errCode = 0;
private $errMsg = "";
private $retData = [];
private $err2Msg = [];
public function __construct(array $options = [])
{
$this['config'] = function () use ($options) {
return new Config($options);
};
$this->init();
$errors = ErrorCode::$errors;
$nowIndex = -1;
$definedErr = [];
foreach ($errors as $key => $val) {
/* 检查是否没有错误定义或错误消息 */
if (!isset($val['errDefine']) || !isset($val['errMsg'])) {
throw new \Exception('errDefine not found!:'.print_r($val, true));
break;
}
/* 检查错误码是否有错 */
if (isset($val['index'])) {
if ($val['index'] <= $nowIndex) {
throw new \Exception("Index is not in order or same index set: ".$val['errDefine']);
break;
}
}
/* 检查错误定义是否重复 */
if (in_array($val['errDefine'], $definedErr)) {
throw new \Exception("Redefinition of ".$val['errDefine']);
break;
}
if (isset($val['index'])) {
$nowIndex = $val['index'];
} else {
$nowIndex++;
}
define($val['errDefine'], $nowIndex);
$this->err2Msg[$nowIndex] = $val['errMsg'];
}
}
/**
* 设置错误码,在其中会同时设置好错误信息
* @param $errCode 错误码
*/
private function setErr($errCode)
{
if ($this->errCode != ERROR_OK) {
// Error code is set, are you sure of setting error code again???
}
$this->errCode = $errCode;
$this->errMsg = $this->getErrMsginfo($errCode);
}
/**
* 设置返回消息体
* @param $retData 消息体
*/
private function setRetData($retData)
{
$this->retData = $retData;
}
/**
* 获取当前的错误码
* @return int 当前错误码
*/
private function getErrCode()
{
return $this->errCode;
}
/**
* 获取当前的错误描述
* @return string 当前错误描述
*/
private function getErrMsg()
{
return $this->errMsg;
}
/**
* 加载默认的View,其中只是返回json串
*/
private function loadDefaultView($isError = false)
{
$err = $this['config']->errBody ?? 'err';
$errMsg = $this['config']->errMsgBody ?? 'errMsg';
$data = $this['config']->dataBody ?? 'data';
$isErr = $this['config']->isErrorBody ?? 'isError';
$data = [
$err => $this->errCode,
$errMsg => $this->errMsg,
$data => (object) $this->retData,
$isErr => $isError,
];
echo json_encode($data);
}
/**
* 简化的回复方法(失败)
* @param unknown $errCode
*/
public function responseError($errCode)
{
$this->setErr($errCode);
$this->loadDefaultView(true);
}
/**
* 简化的回复方法(成功)
* @param array $data
*/
public function responseSuccess($data = null)
{
if ($data == null) {
$data = (object) [];
}
$this->setRetData($data);
$this->loadDefaultView();
}
/**
* 获取所有的错误信息数组
* @return array
*/
static function getAllErrors()
{
return ErrorCode::$errors;
}
/**
* 注册错误码
* @param $errors
*/
static function registerErrors($errors)
{
ErrorCode::$errors = array_merge(ErrorCode::$errors, $errors);
}
/**
* 初始化 查找用户自定义错误文件
*/
private function init()
{
require_once 'Exceptions/Exception.php';
$rootDir = str_replace('\\', '/', realpath(dirname(dirname(dirname(dirname(__DIR__)).'/'))))."/";
$target = $rootDir.'/'.'errors';
if (file_exists($target)) {
if (file_exists($target.'/'.'code.php')) {
$arr = require_once $target.'/'.'code.php';
self::registerErrors($arr);
} else {
// 错误文件不存在
throw new \Exception("code.php not exist!");
}
} else {
// 错误文件不存在
throw new \Exception("code.php not exist!");
}
return;
}
/**
* 根据错误码返回错误描述文本
* @param $errCode 错误码
* @return string 错误描述
*/
private function getErrMsginfo($errCode)
{
if (array_key_exists($errCode, $this->err2Msg)) {
return $this->err2Msg[$errCode];
}
return "";
}
}