-
Notifications
You must be signed in to change notification settings - Fork 133
/
ErrorHandle.php
143 lines (128 loc) · 3.08 KB
/
ErrorHandle.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
<?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 ErrorHandle implements Handle
{
/**
* 运行模式
*
* fpm/swoole
*
* @var string
*/
private $mode = 'fmp';
/**
* app instance
*
* @var Framework\App
*/
private $app = null;
/**
* 错误信息
*
* @var array
*/
private $info = [];
/**
* 构造函数
*/
public function __construct()
{
# code...
}
/**
* 注册错误处理机制
*
* @param $app 框架实例
*
* @return void
*/
public function register(App $app)
{
$this->mode = $app->runningMode;
$this->app = $app;
// do not report the error by php self
// E_ERROR | E_WARNING | E_PARSE | E_NOTICE | E_ALL
error_reporting(0);
// report the by the set_error_handler®ister_shutdown_function::error_get_last
set_error_handler([$this, 'errorHandler']);
register_shutdown_function([$this, 'shutdown']);
}
/**
* 脚本结束
*
* @return mixed
*/
public function shutdown()
{
$error = error_get_last();
if (empty($error)) {
return;
}
$this->info = [
'type' => $error['type'],
'message' => $error['message'],
'file' => $error['file'],
'line' => $error['line'],
];
$this->end();
}
/**
* 错误捕获
*
* @param int $errorNumber 错误码
* @param int $errorMessage 错误信息
* @param string $errorFile 错误文件
* @param string $errorLine 错误行
* @param string $errorContext 错误文本
* @return mixed
*/
public function errorHandler(
$errorNumber,
$errorMessage,
$errorFile,
$errorLine,
$errorContext)
{
$this->info = [
'type' => $errorNumber,
'message' => $errorMessage,
'file' => $errorFile,
'line' => $errorLine,
'context' => $errorContext,
];
$this->end();
}
/**
* 脚本结束
*
* @return mixed
*/
private function end()
{
switch ($this->mode) {
case 'swoole':
CoreHttpException::reponseErrSwoole($this->info);
break;
default:
CoreHttpException::reponseErr($this->info);
break;
}
}
}