question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Http request exceptions captured twice in Angular 9

See original GitHub issue

Package + Version

@sentry/browser@5.15.4

Angular Version 9

Description

Implementing Sentry with Angular following the docs captures two event for http requests that return a 401 error.

I implemented a minimal version in Stackblitz

In app.component.ts a request to a protected API throws a 401 error. The console logs show the beforeSend logs having two events captures, also visible in the networks tab.

Replace the sentry init config dsn to use a different Sentry Account to see the logs in sentry.

If Sentry.captureException is outcommented in app.module, still there is one event captured.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
kamilogorekcommented, Jul 7, 2021

@lujakob thanks for letting me know. Did some debugging around this issue and it appears that HttpClient (which is using XHR underneath) is interacting with Zone.js in a way, where request timeouts are triggered inside setTimeout loop. This means that whenever it throws, our setTimeout instrumentation will catch it, in addition to it being passed to Angular error handler.

There are 2 options. Disabling setTimeout instrumentation (not preferable):

import * as Sentry from '@sentry/angular';

Sentry.init({
  dsn: '...',
  integrations: [new Sentry.Integrations.TryCatch({
    setTimeout: false,
  })]
});

@NgModule({
  imports:      [ BrowserModule, FormsModule, HttpClientModule ],
  declarations: [ AppComponent, HelloComponent ],
  providers: [{ provide: ErrorHandler, useValue: Sentry.createErrorHandler() }],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

or filtering out HttpErrorResponse and migrating to @sentry/angular that handler deals with this type of error, extracting correct data (preferable). Keep in mind that it requires Angular update to at least v9, as we need TS3.5+ for a correct compilation - https://update.angular.io/?v=8.0-9.0 :

import * as Sentry from '@sentry/angular';

Sentry.init({
  dsn: 'https://6f4f6dee9fdb46a28e5df943df71bcaf@sentry.io/1352844',
  beforeSend(event, hint) {
    // Handled by NgModule's ErrorHandler
    if (hint?.originalException instanceof HttpErrorResponse) {
      return null;
    }
    return event;
  }
});

@NgModule({
  imports:      [ BrowserModule, FormsModule, HttpClientModule ],
  declarations: [ AppComponent, HelloComponent ],
  providers: [{ provide: ErrorHandler, useValue: Sentry.createErrorHandler() }],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

Repro: image

After update to @sentry/browser@6.8.0: image

After update to @sentry/angular@6.8.0 and using a correct error handler (https://github.com/getsentry/sentry-javascript/tree/master/packages/angular#errorhandler): image

After update to @sentry/angular@6.8.0, using a correct error handler and using beforeSend filter for HttpErrorResponse: image

Hope that helps! Cheers

0reactions
kamilogorekcommented, Mar 4, 2022

cc @Lms24

Read more comments on GitHub >

github_iconTop Results From Across the Web

Communicating with backend services using HTTP - Angular
Subscribing twice results in two HTTP requests. content_copy ... HttpClient captures both kinds of errors in its HttpErrorResponse .
Read more >
error handler is getting called twice angular http_interceptor
I am having two questions, when an error happens is there a way we can access request headers (so that I can get...
Read more >
Introduction to error handling in Angular 7: Part 2 - Pusher
This tutorial series will introduce you to errors in JavaScript. In part two, learn about HTTP errors, how to use the `HttpClient` with...
Read more >
Handling Exceptions Using the Angular HttpClient Service
We will discuss some of the best ways to manage HTTP exceptions within your app while using Angular's HttpClient service.
Read more >
RxJs Error Handling: Complete Practical Guide
So without further ado, let's get started with our RxJs Error Handling deep ... HTTP response {payload: Array(9)} HTTP request completed.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found