Ionic&Angular&Bugsnag - Custom error handler and async AppVersion

302 Views Asked by At

In my Ionic project I want to use Bugsnag to automatically report all exceptions.

To achieve this, custom error handler is being used. However, it works for handled exceptions only (so its not catching unhandled exceptions). I've found that its caused by async ErrorHandlerFactory:

async function getAppVersionNumber() {
  return await new AppVersion().getVersionNumber().then(ver => ver) || null;
}

export async function errorHandlerFactory() {

  let version = await getAppVersionNumber();
  const bugsnagClient = bugsnag(
    {
      apiKey: CONFIG.bugsnagApiKey,
      appVersion: version,
    });

  return new BugsnagErrorHandler(bugsnagClient);
}

My app.module.ts:

@NgModule({
  declarations: [
    // commented out
  ],
  imports: [
    // commented out
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    // commented out
  ],
  providers: [
    { provide: ErrorHandler, useFactory: errorHandlerFactory },
    // commented out
  ]
})
export class AppModule {
}

If I remove await/async from the above, error reporting works as expected but my version being reported in bugsnag is always "UNKNOWN":

export function errorHandlerFactory() {
  let version = 'UNKNOWN';
  new AppVersion().getVersionNumber().then(ver => {
    version = ver;
  });

  const bugsnagClient = bugsnag(
    {
      apiKey: CONFIG.bugsnagApiKey,
      appVersion: version || null,
    });

  return new BugsnagErrorHandler(bugsnagClient);
}

Any idea how can I use app version in above code with having all errors reported correctly?

0

There are 0 best solutions below