forked from baibaratsky/yii2-rollbar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorHandlerTrait.php
More file actions
112 lines (91 loc) · 3.1 KB
/
Copy pathErrorHandlerTrait.php
File metadata and controls
112 lines (91 loc) · 3.1 KB
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
<?php
namespace baibaratsky\yii\rollbar;
use Rollbar\Rollbar;
use Rollbar\Payload\Level;
use Rollbar\Utilities;
use Yii;
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;
public function logException($exception)
{
$ignoreException = false;
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) {
break;
}
}
}
if (!$ignoreException) {
$extra = $this->getPayloadData($exception);
if ($extra === null) {
$extra = [Utilities::IS_UNCAUGHT_KEY => true];
} else {
$extra = array_merge($extra, [Utilities::IS_UNCAUGHT_KEY => true]);
}
Rollbar::log(Level::error(), $exception, $extra);
}
parent::logException($exception);
}
public function handleError($code, $message, $file, $line)
{
Rollbar::errorHandler($code, $message, $file, $line);
parent::handleError($code, $message, $file, $line);
}
public function handleFatalError()
{
Rollbar::fatalHandler();
parent::handleFatalError();
}
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()
{
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;
}
}