|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* MikoPBX - free phone system for small business |
|
4
|
|
|
* Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov |
|
5
|
|
|
* |
|
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU General Public License as published by |
|
8
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* This program is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
|
17
|
|
|
* If not, see <https://www.gnu.org/licenses/>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
declare(strict_types=1); |
|
21
|
|
|
|
|
22
|
|
|
namespace MikoPBX\Common\Handlers; |
|
23
|
|
|
|
|
24
|
|
|
use MikoPBX\Common\Providers\SentryErrorHandlerProvider; |
|
25
|
|
|
use MikoPBX\Common\Providers\WhoopsErrorHandlerProvider; |
|
26
|
|
|
use MikoPBX\Core\System\Util; |
|
27
|
|
|
use Phalcon\Di; |
|
28
|
|
|
use Throwable; |
|
29
|
|
|
/** |
|
30
|
|
|
* Collects errors and send them to Sentry cloud for software improvement reasons |
|
31
|
|
|
*/ |
|
32
|
|
|
class CriticalErrorsHandler |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* Captures an exception, sends it to the Sentry cloud, and writes syslog. |
|
36
|
|
|
* |
|
37
|
|
|
* @param Throwable $exception The exception to capture. |
|
38
|
|
|
* @param string $moduleTag The name of the current module. |
|
39
|
|
|
* |
|
40
|
|
|
* @return string Pretty error description |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function handleExceptionWithSyslog(Throwable $exception, string $moduleTag=''): string |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
// Sentry |
|
45
|
|
|
$di = Di::getDefault(); |
|
46
|
|
|
$sentryErrorHandler = $di->get(SentryErrorHandlerProvider::SERVICE_NAME); |
|
47
|
|
|
if ($sentryErrorHandler){ |
|
48
|
|
|
$sentryErrorHandler->captureException($exception); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// Whoops |
|
52
|
|
|
$message = WhoopsErrorHandlerProvider::makePrettyErrorDescription($exception, false); |
|
53
|
|
|
Util::sysLogMsg("EXCEPTION", $message, LOG_ERR); |
|
54
|
|
|
return $message; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Handle an exception, sends it to the Sentry cloud, and shows error description to user. |
|
59
|
|
|
* |
|
60
|
|
|
* @param Throwable $exception The exception to capture. |
|
61
|
|
|
* @param string $moduleTag The name of the current module. |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function handleException(Throwable $exception, string $moduleTag=''): void |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
// Sentry |
|
66
|
|
|
$di = Di::getDefault(); |
|
67
|
|
|
$sentryErrorHandler = $di->get(SentryErrorHandlerProvider::SERVICE_NAME); |
|
68
|
|
|
if ($sentryErrorHandler){ |
|
69
|
|
|
$sentryErrorHandler->captureException($exception); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// Whoops |
|
73
|
|
|
$whoops = $di->get(WhoopsErrorHandlerProvider::SERVICE_NAME); |
|
74
|
|
|
if ($whoops){ |
|
75
|
|
|
$whoops->handleException($exception); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.