|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Web service exception file. |
|
5
|
|
|
* |
|
6
|
|
|
* @package API |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright YetiForce S.A. |
|
9
|
|
|
* @license YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com) |
|
10
|
|
|
* @author Mariusz Krzaczkowski <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Api\Core; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Web service exception class. |
|
17
|
|
|
*/ |
|
18
|
|
|
class Exception extends \Exception |
|
19
|
|
|
{ |
|
20
|
|
|
/** {@inheritdoc} */ |
|
21
|
|
|
public function __toString(): string |
|
22
|
|
|
{ |
|
23
|
|
|
return rtrim(str_replace(ROOT_DIRECTORY . \DIRECTORY_SEPARATOR, '', parent::__toString()), PHP_EOL); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** {@inheritdoc} */ |
|
27
|
|
|
public function __construct($message, $code = 500, \Throwable $previous = null) |
|
28
|
|
|
{ |
|
29
|
|
|
parent::__construct($message, $code, $previous); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Show exception error JSON. |
|
34
|
|
|
* |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
|
|
public function showError(): void |
|
38
|
|
|
{ |
|
39
|
|
|
$this->logError(); |
|
40
|
|
|
$message = rtrim(str_replace(ROOT_DIRECTORY . \DIRECTORY_SEPARATOR, '', $this->getMessage()), PHP_EOL); |
|
41
|
|
|
if ($previous = $this->getPrevious()) { |
|
42
|
|
|
$this->file = $previous->getFile(); |
|
43
|
|
|
$this->line = $previous->getLine(); |
|
44
|
|
|
} |
|
45
|
|
|
if (empty($this->message)) { |
|
46
|
|
|
$this->message = $message; |
|
47
|
|
|
} |
|
48
|
|
|
$code = $this->getCode(); |
|
49
|
|
|
if (!\App\Config::debug('apiShowExceptionMessages') && 406 !== $code) { |
|
50
|
|
|
$message = 'Internal Server Error'; |
|
51
|
|
|
} |
|
52
|
|
|
$body = [ |
|
53
|
|
|
'status' => 0, |
|
54
|
|
|
'error' => [ |
|
55
|
|
|
'message' => $message, |
|
56
|
|
|
'code' => $code, |
|
57
|
|
|
], |
|
58
|
|
|
]; |
|
59
|
|
|
if (\App\Config::debug('apiShowExceptionBacktrace')) { |
|
60
|
|
|
$body['error']['file'] = rtrim(str_replace(ROOT_DIRECTORY . \DIRECTORY_SEPARATOR, '', $this->getFile()), PHP_EOL); |
|
61
|
|
|
$body['error']['line'] = $this->getLine(); |
|
62
|
|
|
if (!empty($previous)) { |
|
63
|
|
|
$body['error']['previous'] = rtrim(str_replace(ROOT_DIRECTORY . \DIRECTORY_SEPARATOR, '', $previous->__toString()), PHP_EOL); |
|
64
|
|
|
} |
|
65
|
|
|
$body['error']['backtrace'] = \App\Debuger::getBacktrace(); |
|
66
|
|
|
} |
|
67
|
|
|
$response = Response::getInstance(); |
|
68
|
|
|
$response->setContentType('application/json'); |
|
69
|
|
|
$response->setRequest(Request::init()); |
|
70
|
|
|
$response->setBody($body); |
|
71
|
|
|
$response->setStatus($code); |
|
72
|
|
|
if (\App\Config::debug('apiShowExceptionReasonPhrase')) { |
|
73
|
|
|
$response->setReasonPhrase($this->message); |
|
74
|
|
|
} |
|
75
|
|
|
$response->send(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Log error function. |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
public function logError(): void |
|
84
|
|
|
{ |
|
85
|
|
|
if (\App\Config::debug('apiLogException')) { |
|
86
|
|
|
$request = Request::init(); |
|
87
|
|
|
$error = "code: {$this->getCode()} | message: {$this->getMessage()}\n"; |
|
88
|
|
|
$error .= "file: {$this->getFile()} ({$this->getLine()})\n"; |
|
89
|
|
|
$error .= '============ stacktrace: ' . PHP_EOL . $this->getTraceAsString() . PHP_EOL; |
|
90
|
|
|
$error .= '============ Request ' . \App\RequestUtil::requestId() . ' ====== ' . date('Y-m-d H:i:s') . " ======\n"; |
|
91
|
|
|
$error .= 'REQUEST_METHOD: ' . \App\Request::getRequestMethod() . PHP_EOL; |
|
92
|
|
|
$error .= 'REQUEST_URI: ' . $_SERVER['REQUEST_URI'] . PHP_EOL; |
|
93
|
|
|
$error .= 'QUERY_STRING: ' . $_SERVER['QUERY_STRING'] . PHP_EOL; |
|
94
|
|
|
$error .= 'PATH_INFO: ' . ($_SERVER['PATH_INFO'] ?? '') . PHP_EOL; |
|
95
|
|
|
$error .= 'IP: ' . $_SERVER['REMOTE_ADDR'] . PHP_EOL; |
|
96
|
|
|
$error .= '----------- Headers -----------' . PHP_EOL; |
|
97
|
|
|
foreach ($request->getHeaders() as $key => $header) { |
|
98
|
|
|
$error .= $key . ': ' . $header . PHP_EOL; |
|
99
|
|
|
} |
|
100
|
|
|
$error .= '----------- Request data -----------' . PHP_EOL; |
|
101
|
|
|
$error .= print_r($request->getAllRaw(), true) . PHP_EOL; |
|
|
|
|
|
|
102
|
|
|
if ($_GET) { |
|
103
|
|
|
$error .= "----------- _GET -----------\n"; |
|
104
|
|
|
$error .= print_r($_GET, true) . PHP_EOL; |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
if ($_POST) { |
|
107
|
|
|
$error .= "----------- _POST -----------\n"; |
|
108
|
|
|
$error .= print_r($_POST, true) . PHP_EOL; |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
if ($payload = file_get_contents('php://input')) { |
|
111
|
|
|
$error .= "----------- Request payload -----------\n"; |
|
112
|
|
|
$error .= print_r($payload, true) . PHP_EOL; |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
$path = ROOT_DIRECTORY . '/cache/logs/webserviceErrors.log'; |
|
115
|
|
|
if (isset(\Api\Controller::$container)) { |
|
116
|
|
|
$path = ROOT_DIRECTORY . '/cache/logs/webservice' . \Api\Controller::$container . 'Errors.log'; |
|
117
|
|
|
} |
|
118
|
|
|
file_put_contents($path, '============ Error exception ====== ' . date('Y-m-d H:i:s') . ' ======' |
|
|
|
|
|
|
119
|
|
|
. PHP_EOL . $error . PHP_EOL, FILE_APPEND); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|