Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(AjaxObservable): catch XHR send failures to observer #2159

Merged
merged 1 commit into from
Nov 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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