Skip to content

Commit

Permalink
fix(ajax): AjaxTimeoutErrorImpl extends AjaxError (#5226)
Browse files Browse the repository at this point in the history
* fix ajax: AjaxTimeoutError should extend AjaxError

Fixed AjaxTimeoutError not inheriting AjaxError's and Eerror's
prototype. While the TypeScript typings were correct, actual code
using instanceof operator would fail.

The following statements are now true:
* AjaxError instanceof Error === true
* AjaxTimeoutError instanceof Error === true
* AjaxTimeoutError instanceof AjaxError === true

* docs: CONTRIBUTING.ms was referencing Jasmine instead of Chai.
  • Loading branch information
Huulivoide authored and benlesh committed Jan 20, 2020
1 parent 4e78acd commit a8da8dc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ from the main (upstream) repository:
## Unit Tests
Unit tests are located under the [spec directory](/spec). Unit tests over synchronous operators and operations
can be written in a standard [jasmine](http://jasmine.github.io/) style. Unit tests written against any
can be written in a standard [chai](https://www.chaijs.com/) style. Unit tests written against any
asynchronous operator should be written in [Marble Test Style outlined in detail here](doc/writing-marble-tests.md).
Each operator under test must be in its own file to cover the following cases:
Expand Down
21 changes: 21 additions & 0 deletions spec/observables/dom/ajax-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,27 @@ describe('ajax', () => {
expect(request.headers).to.equal(headers);
});
});

describe('ajax error classes', () => {
describe('AjaxError', () => {
it('should extend Error class', () => {
const error = new AjaxError('Test error', new XMLHttpRequest(), {});
expect(error).to.be.an.instanceOf(Error);
});
});

describe('AjaxTimeoutError', () => {
it('should extend Error class', () => {
const error = new AjaxTimeoutError(new XMLHttpRequest(), {});
expect(error).to.be.an.instanceOf(Error);
});

it('should extend AjaxError class', () => {
const error = new AjaxTimeoutError(new XMLHttpRequest(), {});
expect(error).to.be.an.instanceOf(AjaxError);
});
});
});
});

class MockXMLHttpRequest {
Expand Down
14 changes: 9 additions & 5 deletions src/internal/observable/dom/AjaxObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,15 @@ export interface AjaxTimeoutErrorCtor {
new(xhr: XMLHttpRequest, request: AjaxRequest): AjaxTimeoutError;
}

function AjaxTimeoutErrorImpl(this: any, xhr: XMLHttpRequest, request: AjaxRequest) {
AjaxError.call(this, 'ajax timeout', xhr, request);
this.name = 'AjaxTimeoutError';
return this;
}
const AjaxTimeoutErrorImpl = (() => {
function AjaxTimeoutErrorImpl(this: any, xhr: XMLHttpRequest, request: AjaxRequest) {
AjaxError.call(this, 'ajax timeout', xhr, request);
this.name = 'AjaxTimeoutError';
return this;
}
AjaxTimeoutErrorImpl.prototype = Object.create(AjaxError.prototype);
return AjaxTimeoutErrorImpl;
})();

/**
* @see {@link ajax}
Expand Down

0 comments on commit a8da8dc

Please sign in to comment.