8000 fix(http): Include HTTP status code and headers when HTTP requests errored in `httpResource` by wartab · Pull Request #60802 · angular/angular · GitHub
[go: up one dir, main page]

Skip to content

fix(http): Include HTTP status code and headers when HTTP requests errored in httpResource #60802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(http): Include HTTP status code and headers when HTTP requests er…
…rored in `httpResource`

Currently the HTTP status code and headers are only included if the request succeeded. Given status codes convey more information in case of a request error vs. success, this makes it more useful than inspecting what is contained in `.error()`.
  • Loading branch information
wartab committed Apr 9, 2025
commit 424ea3473249061c82d21a376487c235cb84e7fb
11 changes: 9 additions & 2 deletions packages/common/http/src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {Subscription} from 'rxjs';

import {HttpRequest} from './request';
import {HttpClient} from './client';
import {HttpEventType, HttpProgressEvent, HttpResponseBase} from './response';
import {HttpErrorResponse, HttpEventType, HttpProgressEvent, HttpResponseBase} from './response';
import {HttpHeaders} from './headers';
import {HttpParams} from './params';
import {HttpResourceRef, HttpResourceOptions, HttpResourceRequest} from './resource_api';
Expand Down Expand Up @@ -353,7 +353,14 @@ class HttpResourceImpl<T>
break;
}
},
error: (error) => send({error}),
error: (error) => {
if (error instanceof HttpErrorResponse) {
this._headers.set(error.headers);
this._statusCode.set(error.status);
}

send({error});
},
complete: () => {
if (resolve) {
send({error: new Error('Resource completed before producing a value')});
Expand Down
18 changes: 18 additions & 0 deletions packages/common/http/test/resource_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,24 @@ describe('httpResource', () => {
expect(res.statusCode()).toBe(200);
});

it('should return response headers & status when request errored', async () => {
const backend = TestBed.inject(HttpTestingController);
const res = httpResource(() => '/data', {injector: TestBed.inject(Injector)});
TestBed.flushEffects();
const req = backend.expectOne('/data');
req.flush([], {
headers: {
'X-Special': '123',
},
status: 429,
statusText: 'Too many requests',
});
await TestBed.inject(ApplicationRef).whenStable();
expect((res.error() as any).error).toEqual([]);
expect(res.headers()?.get('X-Special')).toBe('123');
expect(res.statusCode()).toBe(429);
});

it('should support progress events', async () => {
const backend = TestBed.inject(HttpTestingController);
const res = httpResource(
Expand Down
0