|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Honeybadger\HoneybadgerLaravel; |
|
4
|
|
|
|
|
5
|
|
|
use Honeybadger\Contracts\Reporter; |
|
6
|
|
|
use Honeybadger\Exceptions\ServiceException; |
|
7
|
|
|
use Honeybadger\Honeybadger; |
|
8
|
|
|
use Honeybadger\HoneybadgerLaravel\Exceptions\TestException; |
|
9
|
|
|
use Illuminate\Support\Facades\Log; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
11
|
|
|
use Throwable; |
|
12
|
|
|
|
|
13
|
|
|
class HoneybadgerLaravel extends Honeybadger |
|
14
|
|
|
{ |
|
15
|
|
|
const VERSION = '3.18.2'; |
|
16
|
|
|
|
|
17
|
|
|
// Don't forget to sync changes to this with the config file defaults |
|
18
|
|
|
const DEFAULT_BREADCRUMBS = [ |
|
19
|
|
|
Breadcrumbs\DatabaseQueryExecuted::class, |
|
20
|
|
|
Breadcrumbs\DatabaseTransactionStarted::class, |
|
21
|
|
|
Breadcrumbs\DatabaseTransactionCommitted::class, |
|
22
|
|
|
Breadcrumbs\DatabaseTransactionRolledBack::class, |
|
23
|
|
|
Breadcrumbs\CacheHit::class, |
|
24
|
|
|
Breadcrumbs\CacheMiss::class, |
|
25
|
|
|
Breadcrumbs\JobQueued::class, |
|
26
|
|
|
Breadcrumbs\MailSending::class, |
|
27
|
|
|
Breadcrumbs\MailSent::class, |
|
28
|
|
|
Breadcrumbs\MessageLogged::class, |
|
29
|
|
|
Breadcrumbs\NotificationSending::class, |
|
30
|
|
|
Breadcrumbs\NotificationSent::class, |
|
31
|
|
|
Breadcrumbs\NotificationFailed::class, |
|
32
|
|
|
Breadcrumbs\RedisCommandExecuted::class, |
|
33
|
|
|
Breadcrumbs\RouteMatched::class, |
|
34
|
|
|
Breadcrumbs\ViewRendered::class, |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
public static function make(array $config): Reporter |
|
38
|
|
|
{ |
|
39
|
|
|
return static::new(array_merge([ |
|
|
|
|
|
|
40
|
|
|
'notifier' => [ |
|
41
|
|
|
'name' => 'honeybadger-laravel', |
|
42
|
|
|
'url' => 'https://github.com/honeybadger-io/honeybadger-laravel', |
|
43
|
|
|
'version' => self::VERSION.'/'.Honeybadger::VERSION, |
|
44
|
|
|
], |
|
45
|
|
|
'service_exception_handler' => function (ServiceException $e) { |
|
46
|
|
|
// Note: If you are using Honeybadger as a Logger, this exception |
|
47
|
|
|
// can end up being reported to Honeybadger depending on your log level configuration. |
|
48
|
|
|
Log::warning($e); |
|
49
|
|
|
}, |
|
50
|
|
|
], $config)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function notify(Throwable $throwable, Request $request = null, array $additionalParams = []): array |
|
54
|
|
|
{ |
|
55
|
|
|
$this->setRouteActionAndUserContext($request ?: request()); |
|
56
|
|
|
|
|
57
|
|
|
$result = parent::notify($throwable, $request, $additionalParams); |
|
58
|
|
|
|
|
59
|
|
|
// Persist the most recent error for the rest of the request, so we can display on error page. |
|
60
|
|
|
if (app()->bound('session')) { |
|
61
|
|
|
// Lumen doesn't come with sessions. |
|
62
|
|
|
session()->now('honeybadger_last_error', $result['id'] ?? null); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $result; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function shouldReport(Throwable $throwable): bool |
|
69
|
|
|
{ |
|
70
|
|
|
// Always report if the user is running a test. |
|
71
|
|
|
if ($throwable instanceof TestException) { |
|
72
|
|
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return parent::shouldReport($throwable); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected function setRouteActionAndUserContext(Request $request): void |
|
79
|
|
|
{ |
|
80
|
|
|
// For backwards compatibility, check if context has already been set by the middleware |
|
81
|
|
|
if ($this->context->get('user_id') === null |
|
82
|
|
|
&& $this->context->get('honeybadger_component') === null |
|
83
|
|
|
&& $this->context->get('honeybadger_action') === null) { |
|
84
|
|
|
$contextManager = new ContextManager($this); |
|
85
|
|
|
$contextManager->setRouteAction($request); |
|
86
|
|
|
$contextManager->setUserContext($request); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|