From f2d3bdb80109893ebf93dd22ae229de7adbb2e3e Mon Sep 17 00:00:00 2001 From: gdi2290 Date: Sat, 8 Aug 2015 14:17:43 -0700 Subject: [PATCH] feat(http): xhr error listener invokes throw on EventEmitter Closes #2667 --- modules/http/src/backends/xhr_backend.ts | 10 +++++++++- modules/http/test/backends/xhr_backend_spec.ts | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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());