Skip to content

httpResource on error doesn't trigger the ErrorHandler #60219

@cesco69

Description

@cesco69

Which @angular/* package(s) are the source of the bug?

common

Is this a regression?

No

Description

Hi, httpResource on error don't trigger the handleError of ErrorHandler like classic HttpClient

import { Component, ErrorHandler, inject } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import {
  HttpInterceptor,
  HttpRequest,
  HttpHandler,
  HttpEvent,
  HttpErrorResponse,
  HTTP_INTERCEPTORS,
  httpResource,
  provideHttpClient,
  withInterceptorsFromDi,
  HttpClient,
} from '@angular/common/http';

@Component({
  selector: 'app-root',
  template: `
   <button (click)="onHttpClient()">httpClient</button>
   <button (click)="onError()">error</button>
  `,
})
export class App {
  httpClient = inject(HttpClient);

  httpResource = httpResource(
    {
      url: `http://111/api/httpResource`,
      method: 'GET',
    },
    {
      defaultValue: null,
    }
  );

  onHttpClient() {
    this.httpClient.get(`http://111/api/httpClient`).subscribe(console.log);
  }

  onError() {
    throw Error('test');
  }
}

class MyInterceptor implements HttpInterceptor {
  intercept(
    req: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    return next.handle(req).pipe(
      catchError((error: HttpErrorResponse) => {
        console.log('MyInterceptor', error);
        return throwError(() => error);
      })
    );
  }
}

class MyErrorHandler implements ErrorHandler {
  handleError(error: any) {
    console.log('MyErrorHandler', error);
    alert('Error!' + error.message);
  }
}

bootstrapApplication(App, {
  providers: [
    provideHttpClient(withInterceptorsFromDi()),
    {
      provide: ErrorHandler,
      useClass: MyErrorHandler,
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: MyInterceptor,
      multi: true,
    },
  ],
});

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-cb9qbwgt

Please provide the exception or error you saw


Please provide the environment you discovered this bug in (run ng version)

Angular 19.0.2

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreIssues related to the framework runtimecross-cutting: resourceIssues related to the newly introduced resource / httpResource

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions