Skip to content

Why Is Angular Still Throwing an Error Even Though It’s Caught in onError? #209

@jon9090

Description

@jon9090

Which @ngneat/query-* package(s) are the source of the bug?

query

Is this a regression?

No

Description

In my mutation function, I simulate an error:

mutationFn: () => {
  return of({ data: { status: 500 } }).pipe(
    tap(() => {
      console.log('in tap error');
      throw new Error('bla');
    })
  );
}

The error is caught in the onError function, but Angular still throws it, and nothing seems to catch it.
Check the console.

Is this expected behavior, or is it a bug?

[StackBlitz example](https://stackblitz.com/edit/stackblitz-starters-vzvvph3i)

import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';

import { of, tap, catchError, throwError, map } from 'rxjs';
import { injectMutation, injectQuery } from '@ngneat/query';
import { CommonModule } from '@angular/common';

console.clear();

@Component({
  selector: 'app-root',
  imports: [CommonModule],
  template: `
   in app
  `,
})
export class App {
  #query = injectQuery();

  #add = injectMutation();

  add = this.#add({
    mutationFn: () => {
      return of({ data: { status: 500 } }).pipe(
        tap(() => {
          console.log('in tap error');
          throw new Error('bla');
        })
      );
    },
    onSuccess: () => {
      console.log('onSuccess');
    },
    onError: () => {
      console.log('in error');
    },
  }).result;

  ngOnInit() {
    this.add().mutate({});
  }
}

bootstrapApplication(App);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions