|
@@ -2,6 +2,7 @@ |
|
@@ -2,6 +2,7 @@ |
2
|
|
2
|
|
3
|
namespace app\api\library;
|
3
|
namespace app\api\library;
|
4
|
|
4
|
|
|
|
5
|
+use app\api\library\Exception\BaseException;
|
5
|
use Exception;
|
6
|
use Exception;
|
6
|
use think\exception\Handle;
|
7
|
use think\exception\Handle;
|
7
|
|
8
|
|
|
@@ -10,28 +11,53 @@ use think\exception\Handle; |
|
@@ -10,28 +11,53 @@ use think\exception\Handle; |
10
|
*/
|
11
|
*/
|
11
|
class ExceptionHandle extends Handle
|
12
|
class ExceptionHandle extends Handle
|
12
|
{
|
13
|
{
|
13
|
-
|
14
|
+ private $code;
|
|
|
15
|
+ private $msg;
|
|
|
16
|
+ private $errorCode;
|
14
|
public function render(Exception $e)
|
17
|
public function render(Exception $e)
|
15
|
{
|
18
|
{
|
16
|
// 在生产环境下返回code信息
|
19
|
// 在生产环境下返回code信息
|
17
|
- if (!\think\Config::get('app_debug')) {
|
|
|
18
|
- $statuscode = $code = 500;
|
|
|
19
|
- $msg = 'An error occurred';
|
|
|
20
|
- // 验证异常
|
|
|
21
|
- if ($e instanceof \think\exception\ValidateException) {
|
|
|
22
|
- $code = 0;
|
|
|
23
|
- $statuscode = 200;
|
|
|
24
|
- $msg = $e->getError();
|
|
|
25
|
- }
|
|
|
26
|
- // Http异常
|
|
|
27
|
- if ($e instanceof \think\exception\HttpException) {
|
|
|
28
|
- $statuscode = $code = $e->getStatusCode();
|
|
|
29
|
- }
|
|
|
30
|
- return json(['code' => $code, 'msg' => $msg, 'time' => time(), 'data' => null], $statuscode);
|
20
|
+ if($e instanceof BaseException){
|
|
|
21
|
+ $this->code = $e->code;
|
|
|
22
|
+ $this->msg = $e->msg;
|
|
|
23
|
+ $this->errorCode = $e->errCode;
|
|
|
24
|
+ }elseif($e instanceof \think\exception\ValidateException){
|
|
|
25
|
+ $this->code = -2;
|
|
|
26
|
+ $this->errorCode = 200;
|
|
|
27
|
+ $this->msg = $e->getError();
|
31
|
}
|
28
|
}
|
|
|
29
|
+ else{
|
|
|
30
|
+ if(config('app_debug')){
|
|
|
31
|
+ return parent::render($e);
|
|
|
32
|
+ }else{
|
|
|
33
|
+ $this->code = 500;
|
|
|
34
|
+ $this->msg ='服务器内部异常';
|
|
|
35
|
+ $this->errorCode = 500;
|
|
|
36
|
+ }
|
32
|
|
37
|
|
|
|
38
|
+ }
|
|
|
39
|
+ return writeJson([],$this->code,$this->msg,$this->errorCode);
|
|
|
40
|
+// $url = $_SERVER['SERVER_NAME'];
|
|
|
41
|
+// switch ($url){
|
|
|
42
|
+// case config("environment.local_url"):
|
|
|
43
|
+// $this->code = $this->errorCode = 500;
|
|
|
44
|
+// $this->msg = 'An error occurred';
|
|
|
45
|
+// // 验证异常
|
|
|
46
|
+// if ($e instanceof \think\exception\ValidateException) {
|
|
|
47
|
+// $this->code = -2;
|
|
|
48
|
+// $this->errorCode = 200;
|
|
|
49
|
+// $this->msg = $e->getError();
|
|
|
50
|
+// }
|
|
|
51
|
+// // Http异常
|
|
|
52
|
+// else if($e instanceof BaseException){
|
|
|
53
|
+// $this->code = $e->code;
|
|
|
54
|
+// $this->msg = $e->msg;
|
|
|
55
|
+// $this->errorCode = $e->errCode;
|
|
|
56
|
+// }
|
|
|
57
|
+// return json(['code' => $this->code, 'msg' => $this->msg, 'time' => time(), 'data' => null], $this->errorCode);
|
|
|
58
|
+// }
|
33
|
//其它此交由系统处理
|
59
|
//其它此交由系统处理
|
34
|
- return parent::render($e);
|
60
|
+// return parent::render($e);
|
35
|
}
|
61
|
}
|
36
|
|
62
|
|
37
|
} |
63
|
} |