Skip to content

Commit 2481c2d

Browse files
gingerbenwhannah-smartbear
authored andcommitted
Added a null check to BugsnagErrorHandler (#2295)
* fix: 🐛 fail quietly when client is null * docs: 📝 update CHANGELOG.md
1 parent 3d68d6e commit 2481c2d

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [Unreleased]
4+
5+
### Fixed
6+
7+
- (plugin-angular) Added a null check so BugsnagErrorHandler fails silently when misconfigured [#2295](https://github.com/bugsnag/bugsnag-js/pull/2295)
8+
39
## [8.2.0] - 2025-01-27
410

511
### Added

packages/plugin-angular/src/lib/bugsnag-error-handler.ts

+26-22
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { ErrorHandler, VERSION } from '@angular/core'
22
import Bugsnag, { Client } from '@bugsnag/js'
33

44
type BugsnagWithInternals = typeof Bugsnag & {
5-
_client: Client
5+
_client: Client | null
66
}
77

88
class BugsnagErrorHandler implements ErrorHandler {
9-
public bugsnagClient: Client;
9+
public bugsnagClient: Client | null;
1010

1111
constructor (client?: Client) {
1212
if (client) {
@@ -17,31 +17,35 @@ class BugsnagErrorHandler implements ErrorHandler {
1717

1818
// Add angular runtime to device metadata
1919
const device = { runtimeVersions: { angular: VERSION.full } }
20-
this.bugsnagClient.addOnSession(session => {
21-
session.device = { ...session.device, ...device }
22-
})
23-
this.bugsnagClient.addOnError((event) => {
24-
event.device = { ...event.device, ...device }
25-
})
20+
if (this.bugsnagClient) {
21+
this.bugsnagClient.addOnSession(session => {
22+
session.device = { ...session.device, ...device }
23+
})
24+
this.bugsnagClient.addOnError((event) => {
25+
event.device = { ...event.device, ...device }
26+
})
27+
}
2628
}
2729

2830
handleError (error: any): void {
29-
const handledState = {
30-
severity: 'error',
31-
severityReason: { type: 'unhandledException' },
32-
unhandled: true
31+
if (this.bugsnagClient) {
32+
const handledState = {
33+
severity: 'error',
34+
severityReason: { type: 'unhandledException' },
35+
unhandled: true
36+
}
37+
38+
const event = this.bugsnagClient.Event.create(
39+
error,
40+
true,
41+
handledState,
42+
'angular error handler',
43+
1
44+
)
45+
46+
this.bugsnagClient._notify(event)
3347
}
3448

35-
const event = this.bugsnagClient.Event.create(
36-
error,
37-
true,
38-
handledState,
39-
'angular error handler',
40-
1
41-
)
42-
43-
this.bugsnagClient._notify(event)
44-
4549
// Replicate the default behaviour of the angular error handler by calling console.error
4650
// previously this called ErrorHandler.prototype.handleError but this caused a mismatch between
4751
// the compiled code and the angular version used in the application.

0 commit comments

Comments
 (0)