/
ErrorHandlerTrait.php
70 lines (59 loc) · 1.64 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
<?php
namespace nkovacs\errbit;
use Yii;
use Errbit\Errbit;
use yii\base\InvalidConfigException;
/**
* ErrorHandlerTrait should be attached to an error handler.
* It sends errors to errbit.
*/
trait ErrorHandlerTrait
{
/**
* @var string errbit api key
*/
public $errbitApiKey;
/**
* @var string errbit host
*/
public $errbitHost;
/**
* @var array errbit configuration
*/
public $errbit;
public function register()
{
$config = [
'api_key' => $this->errbitApiKey,
'host' => $this->errbitHost,
];
if (is_array($this->errbit)) {
$this->errbit = array_merge($config, $this->errbit);
}
if ($this->errbit['api_key'] === null) {
throw new InvalidConfigException('Errbit API key is required.');
}
if ($this->errbit['host'] === null) {
throw new InvalidConfigException('Errbit host is required.');
}
Errbit::instance()
->configure($this->errbit);
parent::register();
}
public function logException($exception)
{
$opts = [];
$controller = Yii::$app->controller;
if ($controller !== null) {
$opts['controller'] = $controller->uniqueId;
if ($controller->action !== null) {
$opts['action'] = $controller->action->id;
}
if ($controller instanceof UserInfoInterface) {
$opts['user'] = $controller->getErrbitUserInfo();
}
}
Errbit::instance()->notify($exception, $opts);
parent::logException($exception);
}
}