diff --git a/modules/@angular/http/src/backends/xhr_backend.ts b/modules/@angular/http/src/backends/xhr_backend.ts index 5478d190e9..93530ed717 100644 --- a/modules/@angular/http/src/backends/xhr_backend.ts +++ b/modules/@angular/http/src/backends/xhr_backend.ts @@ -80,7 +80,12 @@ export class XHRConnection implements Connection { }; // error event handler let onError = (err: any) => { - var responseOptions = new ResponseOptions({body: err, type: ResponseType.Error}); + var responseOptions = new ResponseOptions({ + body: err, + type: ResponseType.Error, + status: _xhr.status, + statusText: _xhr.statusText, + }); if (isPresent(baseResponseOptions)) { responseOptions = baseResponseOptions.merge(responseOptions); } diff --git a/modules/@angular/http/test/backends/xhr_backend_spec.ts b/modules/@angular/http/test/backends/xhr_backend_spec.ts index 3569ca8e1b..313609041c 100644 --- a/modules/@angular/http/test/backends/xhr_backend_spec.ts +++ b/modules/@angular/http/test/backends/xhr_backend_spec.ts @@ -181,6 +181,24 @@ export function main() { existingXHRs[0].dispatchEvent('error'); })); + it('should set the status text and status code on error', + inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { + var connection = new XHRConnection( + sampleRequest, new MockBrowserXHR(), + new ResponseOptions({type: ResponseType.Error})); + connection.response.subscribe(null, (res: Response) => { + expect(res.type).toBe(ResponseType.Error); + expect(res.status).toEqual(0); + expect(res.statusText).toEqual(''); + async.done(); + }); + const xhr = existingXHRs[0]; + // status=0 with a text='' is common for CORS errors + xhr.setStatusCode(0); + xhr.setStatusText(''); + xhr.dispatchEvent('error'); + })); + it('should call open with method and url when subscribed to', () => { var connection = new XHRConnection(sampleRequest, new MockBrowserXHR()); expect(openSpy).not.toHaveBeenCalled(); @@ -188,7 +206,6 @@ export function main() { expect(openSpy).toHaveBeenCalledWith('GET', sampleRequest.url); }); - it('should call send on the backend with request body when subscribed to', () => { var body = 'Some body to love'; var base = new BaseRequestOptions();