feat(http): xhr error listener invokes throw on EventEmitter

Closes #2667
This commit is contained in:
gdi2290 2015-08-08 14:17:43 -07:00 committed by Jeff Cross
parent 3b9411cbeb
commit f2d3bdb801
2 changed files with 18 additions and 1 deletions

View File

@ -1,5 +1,5 @@
import {ConnectionBackend, Connection} from '../interfaces'; import {ConnectionBackend, Connection} from '../interfaces';
import {ReadyStates, RequestMethods, RequestMethodsMap} from '../enums'; import {ReadyStates, RequestMethods, RequestMethodsMap, ResponseTypes} from '../enums';
import {Request} from '../static_request'; import {Request} from '../static_request';
import {Response} from '../static_response'; import {Response} from '../static_response';
import {ResponseOptions, BaseResponseOptions} from '../base_response_options'; 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 // TODO(gdi2290): defer complete if array buffer until done
ObservableWrapper.callReturn(this.response); 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 // TODO(jeffbcross): make this more dynamic based on body type
if (isPresent(req.headers)) { if (isPresent(req.headers)) {

View File

@ -118,6 +118,15 @@ export function main() {
expect(abortSpy).toHaveBeenCalled(); 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', () => { it('should automatically call open with method and url', () => {
new XHRConnection(sampleRequest, new MockBrowserXHR()); new XHRConnection(sampleRequest, new MockBrowserXHR());