1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Weew\HttpApp\ErrorHandler\Json; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Weew\ErrorHandler\Errors\IError; |
7
|
|
|
use Weew\ErrorHandler\IErrorHandler; |
8
|
|
|
use Weew\Http\HttpStatusCode; |
9
|
|
|
use Weew\Http\Responses\JsonResponse; |
10
|
|
|
use Weew\HttpApp\IHttpApp; |
11
|
|
|
|
12
|
|
|
class JsonErrorHandler { |
13
|
|
|
/** |
14
|
|
|
* @var IHttpApp |
15
|
|
|
*/ |
16
|
|
|
protected $httpApp; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var IErrorHandler |
20
|
|
|
*/ |
21
|
|
|
protected $errorHandler; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* JsonErrorHandler constructor. |
25
|
|
|
* |
26
|
|
|
* @param IHttpApp $httpApp |
27
|
|
|
* @param IErrorHandler $errorHandler |
28
|
|
|
*/ |
29
|
|
|
public function __construct(IHttpApp $httpApp, IErrorHandler $errorHandler) { |
30
|
|
|
$this->httpApp = $httpApp; |
31
|
|
|
$this->errorHandler = $errorHandler; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return IHttpApp |
36
|
|
|
*/ |
37
|
|
|
public function getHttpApp() { |
38
|
|
|
return $this->httpApp; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param IHttpApp $httpApp |
43
|
|
|
*/ |
44
|
|
|
public function setHttpApp(IHttpApp $httpApp) { |
45
|
|
|
$this->httpApp = $httpApp; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return IErrorHandler |
50
|
|
|
*/ |
51
|
|
|
public function getErrorHandler() { |
52
|
|
|
return $this->errorHandler; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param IErrorHandler $errorHandler |
57
|
|
|
*/ |
58
|
|
|
public function setErrorHandler(IErrorHandler $errorHandler) { |
59
|
|
|
$this->errorHandler = $errorHandler; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Enable error handling. |
64
|
|
|
*/ |
65
|
|
|
public function enableErrorHandling() { |
66
|
|
|
$this->errorHandler->addErrorHandler([$this, 'handleError']); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Enable exception handling. |
71
|
|
|
*/ |
72
|
|
|
public function enableExceptionHandling() { |
73
|
|
|
$this->errorHandler->addExceptionHandler([$this, 'handleException']); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param IError $error |
78
|
|
|
*/ |
79
|
|
|
public function handleError(IError $error) { |
80
|
|
|
$response = new JsonResponse(HttpStatusCode::INTERNAL_SERVER_ERROR); |
81
|
|
|
$response->getData()->set('error.message', $error->getMessage()); |
82
|
|
|
$response->getData()->set('error.code', $error->getCode()); |
83
|
|
|
$response->getData()->set('error.file', $error->getFile()); |
84
|
|
|
$response->getData()->set('error.line', $error->getLine()); |
85
|
|
|
|
86
|
|
|
$this->httpApp->shutdownWithResponse($response); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param Exception $ex |
91
|
|
|
*/ |
92
|
|
|
public function handleException(Exception $ex) { |
93
|
|
|
$trace = $ex->getTrace(); |
94
|
|
|
|
95
|
|
|
foreach ($trace as &$item) { |
96
|
|
|
foreach (array_keys($item) as $key) { |
97
|
|
|
if ( ! array_contains(['file', 'line', 'function', 'class'], $key)) { |
98
|
|
|
array_remove($item, $key); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$response = new JsonResponse(HttpStatusCode::INTERNAL_SERVER_ERROR); |
104
|
|
|
$response->getData()->set('error.message', $ex->getMessage()); |
105
|
|
|
$response->getData()->set('error.code', $ex->getCode()); |
106
|
|
|
$response->getData()->set('error.file', $ex->getFile()); |
107
|
|
|
$response->getData()->set('error.line', $ex->getLine()); |
108
|
|
|
$response->getData()->set('error.trace', $trace); |
109
|
|
|
|
110
|
|
|
$this->httpApp->shutdownWithResponse($response); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|