1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Violet88\BugsnagModule; |
4
|
|
|
|
5
|
|
|
use Bugsnag\Client; |
6
|
|
|
use Bugsnag\Report; |
7
|
|
|
use Composer\InstalledVersions; |
8
|
|
|
use Exception; |
9
|
|
|
use SilverStripe\Core\Environment; |
10
|
|
|
use SilverStripe\Security\Security; |
11
|
|
|
|
12
|
|
|
class Bugsnag |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
public Client $bugsnag; |
16
|
|
|
|
17
|
|
|
protected $EXTRA_OPTIONS = array(); |
18
|
|
|
|
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
if (Environment::getEnv('BUGSNAG_ACTIVE') === "true") { |
22
|
|
|
$this->bugsnag = Client::make(Environment::getEnv('BUGSNAG_API_KEY')); |
23
|
|
|
$this->bugsnag->setAppType('Silverstripe'); |
24
|
|
|
|
25
|
|
|
$this->bugsnag->setReleaseStage(Environment::getEnv('BUGSNAG_RELEASE_STAGE') ?? 'development'); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Reset the custom metadata |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function reset() |
35
|
|
|
{ |
36
|
|
|
$this->EXTRA_OPTIONS = []; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the standard severity set in the config. |
41
|
|
|
* |
42
|
|
|
* @return mixed |
43
|
|
|
*/ |
44
|
|
|
public function getStandardSeverity() |
45
|
|
|
{ |
46
|
|
|
return Environment::getEnv('BUGSNAG_STANDARD_SEVERITY'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the current custom metadata |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function getExtraOptions() |
55
|
|
|
{ |
56
|
|
|
return $this->EXTRA_OPTIONS; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Add a key value pair to the metadata |
61
|
|
|
* |
62
|
|
|
* @param $key |
63
|
|
|
* @param $value |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function addExtraOption($key, $value) |
67
|
|
|
{ |
68
|
|
|
$this->EXTRA_OPTIONS[$key] = $value; |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Remove a key value pair from the metadata |
74
|
|
|
* |
75
|
|
|
* @param $key |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function removeExtraOption($key) |
79
|
|
|
{ |
80
|
|
|
unset($this->EXTRA_OPTIONS[$key]); |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the Bugsnag client |
86
|
|
|
* |
87
|
|
|
* @return Client |
88
|
|
|
*/ |
89
|
|
|
public function getBugsnag(): Client |
90
|
|
|
{ |
91
|
|
|
return $this->bugsnag; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* This method send the exception to Bugsnag. Perform any configuration to your error report BEFORE you call this |
96
|
|
|
* method. |
97
|
|
|
* |
98
|
|
|
* @param Exception $exception |
99
|
|
|
* @param string|null $severity |
100
|
|
|
* @param bool $resetExtraOptions |
101
|
|
|
* @param bool $handled |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
public function sendException( |
105
|
|
|
Exception $exception, |
106
|
|
|
string $severity = null, |
107
|
|
|
bool $resetExtraOptions = true, |
108
|
|
|
bool $handled = true |
109
|
|
|
) { |
110
|
|
|
$active = Environment::getEnv('BUGSNAG_ACTIVE'); |
111
|
|
|
if ($active === "true") { |
112
|
|
|
if (empty($severity)) { |
113
|
|
|
$severity = $this->getStandardSeverity(); |
114
|
|
|
} |
115
|
|
|
if ($handled) { |
116
|
|
|
$this->getBugsnag()->notifyException( |
117
|
|
|
$exception, |
118
|
|
|
function (Report $report) use ($severity) { |
119
|
|
|
$this->notifyCallback($report, $severity); |
120
|
|
|
} |
121
|
|
|
); |
122
|
|
|
} else { |
123
|
|
|
$this->getBugsnag()->notify( |
124
|
|
|
Report::fromPHPThrowable( |
125
|
|
|
$this->getBugsnag()->getConfig(), |
126
|
|
|
$exception |
127
|
|
|
)->setUnhandled(true), |
128
|
|
|
function (Report $report) use ($severity) { |
129
|
|
|
$this->notifyCallback($report, $severity); |
130
|
|
|
} |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
if ($resetExtraOptions) { |
134
|
|
|
$this->reset(); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* This method is for internal use only. It is called by sendException() to configure the error report |
141
|
|
|
* |
142
|
|
|
* @param Report $report |
143
|
|
|
* @param $severity |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
protected function notifyCallback(Report $report, $severity) |
147
|
|
|
{ |
148
|
|
|
$report->setSeverity($severity); |
149
|
|
|
$report->setMetaData($this->getExtraOptions()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Add the logged-in user to the error report. This user is automatically retrieved. When given no parameter, |
154
|
|
|
* it will add the user to the error report. When given false, it will remove the user from the error report. |
155
|
|
|
* |
156
|
|
|
* @param $bool |
157
|
|
|
* @return $this |
158
|
|
|
*/ |
159
|
|
|
public function addUserInfo($bool = true) |
160
|
|
|
{ |
161
|
|
|
if ($bool) { |
162
|
|
|
if ($member = Security::getCurrentUser()) { |
163
|
|
|
$this->addExtraOption('User', array( |
164
|
|
|
'Email' => $member->Email, |
165
|
|
|
'FirstName' => $member->FirstName, |
166
|
|
|
'Surname' => $member->Surname, |
167
|
|
|
'ID' => $member->ID, |
168
|
|
|
'Groups' => $member->Groups() ? $member->Groups()->column('Title') : [], |
169
|
|
|
)); |
170
|
|
|
} |
171
|
|
|
} else { |
172
|
|
|
$this->removeExtraOption('User'); |
173
|
|
|
} |
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Add the given version to the error report as the app version. |
179
|
|
|
* |
180
|
|
|
* @param $version |
181
|
|
|
* @return $this |
182
|
|
|
*/ |
183
|
|
|
public function setAppVersion($version) |
184
|
|
|
{ |
185
|
|
|
$this->bugsnag->setAppVersion($version); |
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Add the given type to the error report as the app type. |
191
|
|
|
* |
192
|
|
|
* @param $type |
193
|
|
|
* @return $this |
194
|
|
|
*/ |
195
|
|
|
public function setAppType($type) |
196
|
|
|
{ |
197
|
|
|
$this->bugsnag->setAppType($type); |
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Add the given stage to the error report as the release stage of the application. |
203
|
|
|
* |
204
|
|
|
* @param $version |
205
|
|
|
* @return $this |
206
|
|
|
*/ |
207
|
|
|
public function setReleaseStage($stage) |
208
|
|
|
{ |
209
|
|
|
$this->bugsnag->setReleaseStage($stage); |
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Send an error with the given message to Bugsnag |
215
|
|
|
* |
216
|
|
|
* @param $error |
217
|
|
|
* @return void |
218
|
|
|
*/ |
219
|
|
|
public function sendError($error) |
220
|
|
|
{ |
221
|
|
|
$active = Environment::getEnv('BUGSNAG_ACTIVE'); |
222
|
|
|
if ($active === "true") { |
223
|
|
|
$this->bugsnag->notifyError('Error', $error); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Set the endpoint to which the error report is sent. This is useful for on premise bugsnag. |
229
|
|
|
* |
230
|
|
|
* @param $endpoint |
231
|
|
|
* @return $this |
232
|
|
|
*/ |
233
|
|
|
public function setEndpoint($endpoint) |
234
|
|
|
{ |
235
|
|
|
$this->bugsnag->setNotifyEndpoint($endpoint); |
236
|
|
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Add the version of the application, set in composer.json, to the error report. When given no parameter, |
241
|
|
|
* it will add the version to the error report. When given false, it will remove the version from the error report. |
242
|
|
|
* |
243
|
|
|
* @param bool $bool |
244
|
|
|
* @return $this |
245
|
|
|
*/ |
246
|
|
|
public function addVersion(bool $bool = true) |
247
|
|
|
{ |
248
|
|
|
if ($bool) { |
249
|
|
|
$version = InstalledVersions::getRootPackage()['pretty_version']; |
250
|
|
|
$this->setAppVersion($version); |
251
|
|
|
} else { |
252
|
|
|
$this->removeExtraOption('Version'); |
253
|
|
|
} |
254
|
|
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Add the installed packages, without versions, to the error report. When given no parameter, |
259
|
|
|
* it will add the packages to the error report. When given false, |
260
|
|
|
* it will remove the packages from the error report. |
261
|
|
|
* |
262
|
|
|
* @param bool $bool |
263
|
|
|
* @return $this |
264
|
|
|
*/ |
265
|
|
|
public function addPackages(bool $bool = true) |
266
|
|
|
{ |
267
|
|
|
if ($bool) { |
268
|
|
|
$packages = InstalledVersions::getInstalledPackages(); |
269
|
|
|
$this->addExtraOption('Packages', $packages); |
270
|
|
|
} else { |
271
|
|
|
$this->removeExtraOption('Packages'); |
272
|
|
|
} |
273
|
|
|
return $this; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Add the installed packages, with their versions, to the error report. When given no parameter, |
278
|
|
|
* it will add the packages to the error report. When given false, |
279
|
|
|
* it will remove the packages from the error report. |
280
|
|
|
* |
281
|
|
|
* @param bool $bool |
282
|
|
|
* @return $this |
283
|
|
|
*/ |
284
|
|
|
public function addPackagesWithVersions(bool $bool = true) |
285
|
|
|
{ |
286
|
|
|
if ($bool) { |
287
|
|
|
$packages = InstalledVersions::getInstalledPackages(); |
288
|
|
|
$packagesWithVersions = []; |
289
|
|
|
foreach ($packages as $package) { |
290
|
|
|
$packagesWithVersions[$package] = InstalledVersions::getPrettyVersion($package); |
291
|
|
|
} |
292
|
|
|
$this->addExtraOption('Packages', $packagesWithVersions); |
293
|
|
|
} else { |
294
|
|
|
$this->removeExtraOption('Packages'); |
295
|
|
|
} |
296
|
|
|
return $this; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Send a new build release to Bugsnag. This is useful for matching versions with releases. |
301
|
|
|
* |
302
|
|
|
* @param $repository |
303
|
|
|
* @param $revision |
304
|
|
|
* @param $provider |
305
|
|
|
* @param $builderName |
306
|
|
|
* @return $this |
307
|
|
|
*/ |
308
|
|
|
public function notifyBuild($repository, $revision, $provider, $builderName) |
309
|
|
|
{ |
310
|
|
|
$this->bugsnag->build($repository, $revision, $provider, $builderName); |
311
|
|
|
return $this; |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|