1

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?

4
  • The issue is promises are async.. You can be certain of having the value only within then.. Commented Nov 27, 2018 at 10:18
  • @SurajRao I understand that, so I am looking for a solution - how can I modify/inject proper ErrorHandler. Commented Nov 27, 2018 at 10:19
  • seems like an open issue github.com/angular/angular/issues/23279 Commented Nov 27, 2018 at 10:23
  • also see stackoverflow.com/questions/46257184/… Commented Nov 27, 2018 at 10:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.