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
- Have in app.module setup like in documentation:
export function errorHandlerFactory() {
return new BugsnagErrorHandler()
}
In the module providers:
{ provide: ErrorHandler, useFactory: errorHandlerFactory },
- Set
"builder": "@angular-devkit/build-angular:browser-esbuild",in angular.json - Start the app
- 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
- Create a minimal repository that can reproduce the issue
- Link to it here: https://stackblitz.com/edit/angular-ivy-7x7fkm?file=src%2Fapp%2Fapp.module.ts
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)
@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
fesmdistribution, 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-angularis in my case@bugsnag/plugin-angular/dist/esm5/index.js, which has the following code in it:This code literally imports
ErrorHandlerfrom@angular/core. At runtime, the module@angular/core/fesm2022/core.mjsis resolved for this import, which providesErrorHandleras a Javascriptclass. The error is thrown at the linevar _this = _super.call(this) || this;in theBugsnagErrorHandlerconstructor function. It calls_superwithoutnewwhich is illegal because_superis a class.I presume the issue can temporarily be circumvented by setting the
targettoES5in 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-angularpackage, the problem can be fixed by following the latest package development standards, i.e. building aesm2022as well as afesm2022distribution. 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
NgModulerather than standalone components. As suggested by the error message, this meant that thenewkeyword is required before invokingErrorHandler. In yourapp.module.ts, you should edit theNgModuleso thatproviderslooks 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/jsinsideallowedCommonJsDependencieswithin yourangular.json. This will look something like: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 startwhich isng 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.jsonis"aot": true,Can’t share the project.
I’ve found a workaround:
Please let me know if you need something else from me.