Skip to content

Commit

Permalink
fix(xmlhttprequest): catch errors (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts authored Jul 26, 2020
1 parent 6b721ca commit 7ed320a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/xmlhttprequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,14 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget {
this.#readyState = XMLHttpRequest.UNSENT;
});

this.#client.addListener('error', () => {
this.dispatchEvent(new ProgressEvent('error'));
this.upload.dispatchEvent(new ProgressEvent('error'));

this.#client = null;
this.#readyState = XMLHttpRequest.DONE;
});

this.#client.addListener('response', (response) => {
this.#readyState = XMLHttpRequest.HEADERS_RECEIVED;
this.dispatchEvent(new Event('readystatechange'));
Expand Down
13 changes: 13 additions & 0 deletions test/integration/xmlhttprequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ describe('XMLHttpRequest', () => {
baseURL = null;
});

describe("addEventListener(event: 'error')", () => {
it('basic use case', (done) => {
const client = new XMLHttpRequest();

client.addEventListener('error', () => {
done();
});

client.open('GET', 'http://example.invalid/path/to');
client.send(null);
});
});

describe("addEventListener(event: 'load')", () => {
it('basic use case', (done) => {
const client = new XMLHttpRequest();
Expand Down

0 comments on commit 7ed320a

Please sign in to comment.