-
Notifications
You must be signed in to change notification settings - Fork 133
/
ExceptionHandle.php
97 lines (87 loc) · 2.07 KB
/
ExceptionHandle.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
<?php
/********************************************
* Easy PHP *
* *
* A lightweight PHP framework for studying *
* *
* TIERGB *
* <https://github.com/TIGERB> *
* *
********************************************/
namespace Framework\Handles;
use Framework\App;
use Framework\Handles\Handle;
use Framework\Exceptions\CoreHttpException;
/**
* 未补货异常处理机制
*
* @author TIERGB <https://github.com/TIGERB>
*/
class ExceptionHandle implements Handle
{
/**
* 运行模式
*
* fpm/swoole
*
* @var string
*/
private $mode = 'fmp';
/**
* 错误信息
*
* @var array
*/
private $info = [];
/**
* 构造函数
*/
public function __construct()
{
# code...
}
/**
* 注册未捕获异常函数
*
* @param App $app 框架实例
* @return void
*/
public function register(App $app)
{
set_exception_handler([$this, 'exceptionHandler']);
}
/**
* 未捕获异常函数
*
* @param object $exception 异常
* @return void
*/
public function exceptionHandler($exception)
{
$this->info = [
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'trace' => $exception->getTrace(),
'previous' => $exception->getPrevious()
];
$this->end();
}
/**
* 脚本结束
*
* @return mixed
*/
private function end()
{
switch ($this->mode) {
case 'swooole':
CoreHttpException::reponseErrSwoole($this->info);
break;
default:
CoreHttpException::reponseErr($this->info);
break;
}
}
}