diff --git a/modules/http/src/backends/xhr_backend.ts b/modules/http/src/backends/xhr_backend.ts index c25f1fe445..b2ae729889 100644 --- a/modules/http/src/backends/xhr_backend.ts +++ b/modules/http/src/backends/xhr_backend.ts @@ -1,5 +1,5 @@ import {ConnectionBackend, Connection} from '../interfaces'; -import {ReadyStates, RequestMethods, RequestMethodsMap} from '../enums'; +import {ReadyStates, RequestMethods, RequestMethodsMap, ResponseTypes} from '../enums'; import {Request} from '../static_request'; import {Response} from '../static_response'; import {ResponseOptions, BaseResponseOptions} from '../base_response_options'; @@ -59,6 +59,14 @@ export class XHRConnection implements Connection { // TODO(gdi2290): defer complete if array buffer until done ObservableWrapper.callReturn(this.response); }); + + this._xhr.addEventListener('error', (err) => { + var responseOptions = new ResponseOptions({body: err, type: ResponseTypes.Error}); + if (isPresent(baseResponseOptions)) { + responseOptions = baseResponseOptions.merge(responseOptions); + } + ObservableWrapper.callThrow(this.response, new Response(responseOptions)) + }); // TODO(jeffbcross): make this more dynamic based on body type if (isPresent(req.headers)) { diff --git a/modules/http/test/backends/xhr_backend_spec.ts b/modules/http/test/backends/xhr_backend_spec.ts index e84f0a77be..1ad67c2319 100644 --- a/modules/http/test/backends/xhr_backend_spec.ts +++ b/modules/http/test/backends/xhr_backend_spec.ts @@ -118,6 +118,15 @@ export function main() { expect(abortSpy).toHaveBeenCalled(); }); + it('should create an error Response on error', inject([AsyncTestCompleter], async => { + var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(), + new ResponseOptions({type: ResponseTypes.Error})); + ObservableWrapper.subscribe(connection.response, null, res => { + expect(res.type).toBe(ResponseTypes.Error); + async.done(); + }); + existingXHRs[0].dispatchEvent('error'); + })); it('should automatically call open with method and url', () => { new XHRConnection(sampleRequest, new MockBrowserXHR());