Skip to content

Added a null check to BugsnagErrorHandler #2295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased]

### Fixed

- (plugin-angular) Added a null check so BugsnagErrorHandler fails silently when misconfigured [#2295](https://github.com/bugsnag/bugsnag-js/pull/2295)

## [8.2.0] - 2025-01-27

### Added
Expand Down
48 changes: 26 additions & 22 deletions packages/plugin-angular/src/lib/bugsnag-error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { ErrorHandler, VERSION } from '@angular/core'
import Bugsnag, { Client } from '@bugsnag/js'

type BugsnagWithInternals = typeof Bugsnag & {
_client: Client
_client: Client | null
}

class BugsnagErrorHandler implements ErrorHandler {
public bugsnagClient: Client;
public bugsnagClient: Client | null;

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

// Add angular runtime to device metadata
const device = { runtimeVersions: { angular: VERSION.full } }
this.bugsnagClient.addOnSession(session => {
session.device = { ...session.device, ...device }
})
this.bugsnagClient.addOnError((event) => {
event.device = { ...event.device, ...device }
})
if (this.bugsnagClient) {
this.bugsnagClient.addOnSession(session => {
session.device = { ...session.device, ...device }
})
this.bugsnagClient.addOnError((event) => {
event.device = { ...event.device, ...device }
})
}
}

handleError (error: any): void {
const handledState = {
severity: 'error',
severityReason: { type: 'unhandledException' },
unhandled: true
if (this.bugsnagClient) {
const handledState = {
severity: 'error',
severityReason: { type: 'unhandledException' },
unhandled: true
}

const event = this.bugsnagClient.Event.create(
error,
true,
handledState,
'angular error handler',
1
)

this.bugsnagClient._notify(event)
}

const event = this.bugsnagClient.Event.create(
error,
true,
handledState,
'angular error handler',
1
)

this.bugsnagClient._notify(event)

// Replicate the default behaviour of the angular error handler by calling console.error
// previously this called ErrorHandler.prototype.handleError but this caused a mismatch between
// the compiled code and the angular version used in the application.
Expand Down
Loading