bugsnag-js: Angular 17 app can't run with bugsnag/plugin-angular and esbuild

Describe the bug

Angular app fails on start with error Class constructor ErrorHandler cannot be invoked without 'new'

Steps to reproduce

  1. Have in app.module setup like in documentation:
export function errorHandlerFactory() {
  return new BugsnagErrorHandler()
}

In the module providers:

{ provide: ErrorHandler, useFactory: errorHandlerFactory },
  1. Set "builder": "@angular-devkit/build-angular:browser-esbuild", in angular.json
  2. Start the app
  3. An error logged in the console Class constructor ErrorHandler cannot be invoked without 'new'

Btw, it works great with the "builder": "@angular-devkit/build-angular:browser",

Environment

  • Bugsnag version: 7.22.2
  • bugsnag/plugin-angular: 7.22.2,
  • Browser framework version (if any):
    • Angular: 17.0.7
  • Browser version (e.g. chrome, safari):
  • Device (e.g. iphonex):

Example Repo

Example code snippet

# (Insert code sample to reproduce the problem)
Error messages:
Class constructor ErrorHandler cannot be invoked without 'new'

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 23 (8 by maintainers)

Most upvoted comments

@mclack is there any update on this? I have this problem on several projects and this is preventing me from an essential upgrade.

Hi @JJWesterkampAtEndeavour,

This has been fixed in bugsnag-js v8.2.0.

If you experience new issues, please feel free to reach out again. Alternatively, you can also open a ticket with us directly by contacting support@bugsnag.com if you have any other questions, or would like assistance with anything else.

Hi @JJWesterkampAtEndeavour

Thanks for highlighting that.

We are looking into the fesm distribution, and have added a task to our backlog to include this. As such, I’m going to reopen this issue, and while we don’t currently have an ETA, we will make sure to share any updates when we have them.

I’m experiencing the same issue.

FWIW: The issue appears to be caused by the following:

The resolved module for @bugsnag/plugin-angular is in my case @bugsnag/plugin-angular/dist/esm5/index.js, which has the following code in it:

import { ErrorHandler, Injectable } from '@angular/core';
import Bugsnag, { Client } from '@bugsnag/js';
// zones are optional, so we need to detect if they are being used
// see https://angular.io/guide/zone#noopzone
var isNgZoneEnabled = typeof Zone !== 'undefined' && !!Zone.current;
var BugsnagErrorHandler = /** @class */ (function (_super) {
    __extends(BugsnagErrorHandler, _super);
    function BugsnagErrorHandler(client) {
        var _this = _super.call(this) || this;
        if (client) {
            _this.bugsnagClient = client;
        }
        else {
            _this.bugsnagClient = Bugsnag._client;
        }
        return _this;
    }
    BugsnagErrorHandler.prototype.handleError = function (error) {
        var handledState = {
            severity: 'error',
            severityReason: { type: 'unhandledException' },
            unhandled: true
        };
        var event = this.bugsnagClient.Event.create(error, true, handledState, 'angular error handler', 1);
        if (error.ngDebugContext) {
            event.addMetadata('angular', {
                component: error.ngDebugContext.component,
                context: error.ngDebugContext.context
            });
        }
        this.bugsnagClient._notify(event);
        ErrorHandler.prototype.handleError.call(this, error);
    };
    BugsnagErrorHandler = __decorate([
        Injectable(),
        __metadata("design:paramtypes", [Client])
    ], BugsnagErrorHandler);
    return BugsnagErrorHandler;
}(ErrorHandler));
export { BugsnagErrorHandler };

This code literally imports ErrorHandler from @angular/core. At runtime, the module @angular/core/fesm2022/core.mjs is resolved for this import, which provides ErrorHandler as a Javascript class. The error is thrown at the line var _this = _super.call(this) || this; in the BugsnagErrorHandler constructor function. It calls _super without new which is illegal because _super is a class.


I presume the issue can temporarily be circumvented by setting the target to ES5 in the angular project’s tsconfig, but that’s a pretty unacceptable situation given that will produce more bloated and less performant code.


On the side of the @bugsnag/plugin-angular package, the problem can be fixed by following the latest package development standards, i.e. building a esm2022 as well as a fesm2022 distribution. The Angular compiler will itself down-level these formats as required by respective Angular projects.

Hi @mivanyna

Thank you for your patience here.

We have been able to reproduce the error you were seeing, and have made the necessary changes on the StackBlitz project you provided in order to fix all the errors appearing in the console.

The issue seems like it was down to using the old NgModule rather than standalone components. As suggested by the error message, this meant that the new keyword is required before invoking ErrorHandler. In your app.module.ts, you should edit the NgModule so that providers looks like: providers:[{ provide: new ErrorHandler, useFactory: errorHandlerFactory }]

You are likely to run into some more errors after this. If so, this should be fixed by adding @bugsnag/js inside allowedCommonJsDependencies within your angular.json. This will look something like:

"architect": {
...
          "options": {
            "allowedCommonJsDependencies": [
              "@bugsnag/js"
            ],
...

Please try the recommendations above and let us know whether that solves the problem.

No rush! We can live w/o good error logging 😃

Hi, I have the same issue after upgrading to the angular 17 error is thrown: TypeError: Class constructor ErrorHandler cannot be invoked without 'new' at new BugsnagErrorHandler2 (index.js:28:28) at new CustomErrorHandler (CustomErrorHandler.ts:7:5) at Object.customErrorHandlerFactory [as useFactory] (app.module.ts:30:10) at Object.factory (core.mjs:6288:38) at core.mjs:6189:43 at runInInjectorProfilerContext (core.mjs:867:9) at R3Injector.hydrate (core.mjs:6188:17) at R3Injector.get (core.mjs:6058:33) at injectInjectorOnly (core.mjs:911:40) at ɵɵinject (core.mjs:917:42)

Hi @mclack, I run yarn start which is ng serve --hmr --port 3000 --open=true. I don’t change anything else when I switch to "builder": "@angular-devkit/build-angular:browser" Yes, the error stops when comment out { provide: ErrorHandler, useFactory: errorHandlerFactory },

Maybe something to mention is in my angular.json is "aot": true,

"serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
...
}

Can’t share the project.

I’ve found a workaround:

{ provide: ErrorHandler, useClass: AppErrorHandler}

...

export class AppErrorHandler implements ErrorHandler {
   handleError(error: any) {
    Bugsnag.notify(error)
    console.error(error)
  }
}

Please let me know if you need something else from me.