From a8da8dcc899342d3bb6d2d913247d9e734095287 Mon Sep 17 00:00:00 2001 From: Jesse Jaara Date: Mon, 20 Jan 2020 22:38:23 +0200 Subject: [PATCH] fix(ajax): AjaxTimeoutErrorImpl extends AjaxError (#5226) * 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. --- CONTRIBUTING.md | 2 +- spec/observables/dom/ajax-spec.ts | 21 +++++++++++++++++++ src/internal/observable/dom/AjaxObservable.ts | 14 ++++++++----- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 606a3995c7..53cb39dcda 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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: diff --git a/spec/observables/dom/ajax-spec.ts b/spec/observables/dom/ajax-spec.ts index fcd8e1cfeb..3500e4d898 100644 --- a/spec/observables/dom/ajax-spec.ts +++ b/spec/observables/dom/ajax-spec.ts @@ -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 { diff --git a/src/internal/observable/dom/AjaxObservable.ts b/src/internal/observable/dom/AjaxObservable.ts index d1f3f91849..0bf7e4d3e2 100644 --- a/src/internal/observable/dom/AjaxObservable.ts +++ b/src/internal/observable/dom/AjaxObservable.ts @@ -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}