-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathErrorHandlerTrait.php
115 lines (97 loc) · 3.29 KB
/
ErrorHandlerTrait.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
<?php
namespace baibaratsky\yii\rollbar;
use Rollbar\Payload\Level;
use Rollbar\Rollbar;
use Yii;
use yii\base\ErrorException;
use yii\helpers\ArrayHelper;
trait ErrorHandlerTrait
{
public $rollbarComponentName = 'rollbar';
/**
* @var callable Callback returning a payload data associative array or null
* Example:
* function (ErrorHandler $errorHandler) {
* return [
* 'foo' => 'bar',
* 'xyz' => getSomeData(),
* ];
* }
*/
public $payloadDataCallback;
/**
* @return void
*/
public function logException($exception)
{
$this->logExceptionRollbar($exception);
parent::logException($exception);
}
private function getPayloadData($exception)
{
$payloadData = $this->payloadCallback();
if ($exception instanceof WithPayload) {
$exceptionData = $exception->rollbarPayload();
if (is_array($exceptionData)) {
if (is_null($payloadData)) {
$payloadData = $exceptionData;
} else {
$payloadData = ArrayHelper::merge($exceptionData, $payloadData);
}
} elseif (!is_null($exceptionData)) {
throw new \Exception(get_class($exception) . '::rollbarPayload() returns an incorrect result');
}
}
return $payloadData;
}
private function payloadCallback(): array|null
{
if (!isset($this->payloadDataCallback)) {
return null;
}
if (!is_callable($this->payloadDataCallback)) {
throw new \Exception('Incorrect callback provided');
}
$payloadData = call_user_func($this->payloadDataCallback, $this);
if (!is_array($payloadData) && !is_null($payloadData)) {
throw new \Exception('Callback returns an incorrect result');
}
return $payloadData;
}
/**
* @return void
*/
protected function logExceptionRollbar($exception)
{
foreach (Yii::$app->get($this->rollbarComponentName)->ignoreExceptions as $ignoreRecord) {
if ($exception instanceof $ignoreRecord[0]) {
$ignoreException = true;
foreach (array_slice($ignoreRecord, 1) as $property => $range) {
if (!in_array($exception->$property, $range)) {
$ignoreException = false;
break;
}
}
if ($ignoreException) {
return;
}
}
}
// Check if an error coming from handleError() should be ignored.
if ($exception instanceof ErrorException && Rollbar::logger()->shouldIgnoreError($exception->getCode())) {
return;
}
$extra = $this->getPayloadData($exception);
if ($extra === null) {
$extra = [];
}
$level = $this->isFatal($exception) ? Level::CRITICAL : Level::ERROR;
Rollbar::log($level, $exception, $extra, true);
}
protected function isFatal($exception): bool
{
return $exception instanceof \Error
|| ($exception instanceof ErrorException
&& ErrorException::isFatalError(['type' => $exception->getSeverity()]));
}
}