Skip to content

Commit

Permalink
fix(AjaxObservable): catch XHR send failures to observer (#2159)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj authored and jayphelps committed Nov 30, 2016
1 parent f51b8f9 commit 128fb9c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
55 changes: 55 additions & 0 deletions spec/observables/dom/ajax-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,33 @@ describe('Observable.ajax', () => {
expect(error).to.be.an('error', 'wokka wokka');
});

it('should error if send request throws', (done: MochaDone) => {
const expected = new Error('xhr send failure');

const obj = {
url: '/flibbertyJibbet',
responseType: 'text',
method: '',
createXHR: () => {
const ret = new MockXMLHttpRequest();
ret.send = () => {
throw expected;
};
return ret as any;
}
};

Rx.Observable.ajax(obj)
.subscribe(() => {
done(new Error('should not be called'));
}, (e: Error) => {
expect(e).to.be.equal(expected);
done();
}, () => {
done(new Error('should not be called'));
});
});

it('should succeed on 200', () => {
const expected = { foo: 'bar' };
let result;
Expand Down Expand Up @@ -410,6 +437,34 @@ describe('Observable.ajax', () => {
expect(MockXMLHttpRequest.mostRecent.url).to.equal('/flibbertyJibbet');
expect(MockXMLHttpRequest.mostRecent.data).to.equal('{"🌟":"🚀"}');
});

it('should error if send request throws', (done: MochaDone) => {
const expected = new Error('xhr send failure');

const obj = {
url: '/flibbertyJibbet',
responseType: 'text',
method: '',
body: 'foobar',
createXHR: () => {
const ret = new MockXMLHttpRequest();
ret.send = () => {
throw expected;
};
return ret as any;
}
};

Rx.Observable.ajax(obj)
.subscribe(() => {
done(new Error('should not be called'));
}, (e: Error) => {
expect(e).to.be.equal(expected);
done();
}, () => {
done(new Error('should not be called'));
});
});
});

describe('ajax.get', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/observable/dom/AjaxObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
this.setupEvents(xhr, request);

// finally send the request
if (body) {
xhr.send(body);
} else {
xhr.send();
result = body ? tryCatch(xhr.send).call(xhr, body) : tryCatch(xhr.send).call(xhr);
if (result === errorObject) {
this.error(errorObject.e);
return null;
}
}

Expand Down

0 comments on commit 128fb9c

Please sign in to comment.