|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nord\Lumen\NewRelic; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Contracts\Debug\ExceptionHandler; |
|
7
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class NewRelicExceptionHandler |
|
11
|
|
|
* @package Nord\Lumen\NewRelic |
|
12
|
|
|
*/ |
|
13
|
|
|
class NewRelicExceptionHandler implements ExceptionHandler |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var array list of class names of exceptions that should not be reported to New Relic. Defaults to the |
|
18
|
|
|
* NotFoundHttpException class used for 404 requests. |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $ignoredExceptions = [ |
|
21
|
|
|
NotFoundHttpException::class, |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* NewRelicExceptionHandler constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param array|false $ignoredExceptions (optional) a list of exceptions to ignore, or false to use the default |
|
29
|
|
|
* set |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct($ignoredExceptions = false) |
|
32
|
|
|
{ |
|
33
|
|
|
if (is_array($ignoredExceptions)) { |
|
34
|
|
|
$this->ignoredExceptions = $ignoredExceptions; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @inheritdoc |
|
41
|
|
|
*/ |
|
42
|
|
|
public function report(Exception $e) |
|
43
|
|
|
{ |
|
44
|
|
|
if ($this->shouldReport($e)) { |
|
45
|
|
|
$this->logException($e); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @inheritdoc |
|
52
|
|
|
*/ |
|
53
|
|
|
public function render($request, Exception $e) |
|
54
|
|
|
{ |
|
55
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @inheritdoc |
|
61
|
|
|
*/ |
|
62
|
|
|
public function renderForConsole($output, Exception $e) |
|
63
|
|
|
{ |
|
64
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @inheritdoc |
|
69
|
|
|
*/ |
|
70
|
|
|
public function shouldReport(Exception $e) |
|
71
|
|
|
{ |
|
72
|
|
|
foreach ($this->ignoredExceptions as $type) { |
|
73
|
|
|
if ($e instanceof $type) { |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Logs the exception to New Relic (if the extension is loaded) |
|
82
|
|
|
* |
|
83
|
|
|
* @param Exception $e |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function logException(Exception $e) |
|
86
|
|
|
{ |
|
87
|
|
|
if (extension_loaded('newrelic')) { |
|
88
|
|
|
newrelic_notice_error($e->getMessage(), $e); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|