From 7279f067b855fb1c835a88231f53d7d08875683e Mon Sep 17 00:00:00 2001 From: benlesh Date: Fri, 27 Jul 2018 17:59:56 -0700 Subject: [PATCH] fix(IE10): Remove dependency on Object.setPrototypeOf This reimplements all Error types as ES5 classes, and types them with TypeScript such that we no longer need to rely on Object.setPrototypeOf, which was causing issues for users who had not polyfilled it properly in IE10. fixes #3966 --- src/internal/observable/dom/AjaxObservable.ts | 55 +++++++++++-------- src/internal/util/ArgumentOutOfRangeError.ts | 26 ++++++--- src/internal/util/EmptyError.ts | 26 ++++++--- src/internal/util/ObjectUnsubscribedError.ts | 26 ++++++--- src/internal/util/TimeoutError.ts | 26 ++++++--- src/internal/util/UnsubscriptionError.ts | 32 +++++++---- 6 files changed, 122 insertions(+), 69 deletions(-) diff --git a/src/internal/observable/dom/AjaxObservable.ts b/src/internal/observable/dom/AjaxObservable.ts index 4b3a83e4715..954759aab47 100644 --- a/src/internal/observable/dom/AjaxObservable.ts +++ b/src/internal/observable/dom/AjaxObservable.ts @@ -441,7 +441,7 @@ export type AjaxErrorNames = 'AjaxError' | 'AjaxTimeoutError'; * * @class AjaxError */ -export class AjaxError extends Error { +export interface AjaxError extends Error { /** @type {XMLHttpRequest} The XHR instance associated with the error */ xhr: XMLHttpRequest; @@ -456,22 +456,28 @@ export class AjaxError extends Error { /** @type {string|ArrayBuffer|Document|object|any} The response data */ response: any; +} - public readonly name: AjaxErrorNames = 'AjaxError'; - - constructor(message: string, xhr: XMLHttpRequest, request: AjaxRequest) { - super(message); - this.message = message; - this.xhr = xhr; - this.request = request; - this.status = xhr.status; - this.responseType = xhr.responseType || request.responseType; - this.response = parseXhrResponse(this.responseType, xhr); +export interface AjaxErrorCtor { + new(): AjaxError; +} - (Object as any).setPrototypeOf(this, AjaxError.prototype); - } +function AjaxErrorImpl(this: any, message: string, xhr: XMLHttpRequest, request: AjaxRequest): AjaxError { + Error.call(this); + this.message = message; + this.name = 'AjaxError'; + this.xhr = xhr; + this.request = request; + this.status = xhr.status; + this.responseType = xhr.responseType || request.responseType; + this.response = parseXhrResponse(this.responseType, xhr); + return this; } +AjaxErrorImpl.prototype = Object.create(Error.prototype); + +export const AjaxError: AjaxErrorCtor = AjaxErrorImpl as any; + function parseXhrResponse(responseType: string, xhr: XMLHttpRequest) { switch (responseType) { case 'json': @@ -493,17 +499,22 @@ function parseXhrResponse(responseType: string, xhr: XMLHttpRequest) { } } +export interface AjaxTimeoutError extends AjaxError { +} + +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; +} + /** * @see {@link ajax} * * @class AjaxTimeoutError */ -export class AjaxTimeoutError extends AjaxError { - - public readonly name: AjaxErrorNames = 'AjaxTimeoutError'; - - constructor(xhr: XMLHttpRequest, request: AjaxRequest) { - super('ajax timeout', xhr, request); - (Object as any).setPrototypeOf(this, AjaxTimeoutError.prototype); - } -} +export const AjaxTimeoutError: AjaxTimeoutErrorCtor = AjaxTimeoutErrorImpl as any; diff --git a/src/internal/util/ArgumentOutOfRangeError.ts b/src/internal/util/ArgumentOutOfRangeError.ts index 90cd3324e38..949bfcc9da4 100644 --- a/src/internal/util/ArgumentOutOfRangeError.ts +++ b/src/internal/util/ArgumentOutOfRangeError.ts @@ -1,3 +1,19 @@ +export interface ArgumentOutOfRangeError extends Error { +} + +export interface ArgumentOutOfRangeErrorCtor { + new(): ArgumentOutOfRangeError; +} + +function ArgumentOutOfRangeErrorImpl(this: any) { + Error.call(this); + this.message = 'argument out of range'; + this.name = 'ArgumentOutOfRangeError'; + return this; +} + +ArgumentOutOfRangeErrorImpl.prototype = Object.create(Error.prototype); + /** * An error thrown when an element was queried at a certain index of an * Observable, but no such index or position exists in that sequence. @@ -8,12 +24,4 @@ * * @class ArgumentOutOfRangeError */ -export class ArgumentOutOfRangeError extends Error { - - public readonly name = 'ArgumentOutOfRangeError'; - - constructor() { - super('argument out of range'); - (Object as any).setPrototypeOf(this, ArgumentOutOfRangeError.prototype); - } -} +export const ArgumentOutOfRangeError: ArgumentOutOfRangeErrorCtor = ArgumentOutOfRangeErrorImpl as any; \ No newline at end of file diff --git a/src/internal/util/EmptyError.ts b/src/internal/util/EmptyError.ts index 743e43dafba..6dacfc6aa8a 100644 --- a/src/internal/util/EmptyError.ts +++ b/src/internal/util/EmptyError.ts @@ -1,3 +1,19 @@ +export interface EmptyError extends Error { +} + +export interface EmptyErrorCtor { + new(): EmptyError; +} + +function EmptyErrorImpl(this: any) { + Error.call(this); + this.message = 'no elements in sequence'; + this.name = 'EmptyError'; + return this; +} + +EmptyErrorImpl.prototype = Object.create(Error.prototype); + /** * An error thrown when an Observable or a sequence was queried but has no * elements. @@ -8,12 +24,4 @@ * * @class EmptyError */ -export class EmptyError extends Error { - - public readonly name = 'EmptyError'; - - constructor() { - super('no elements in sequence'); - (Object as any).setPrototypeOf(this, EmptyError.prototype); - } -} +export const EmptyError: EmptyErrorCtor = EmptyErrorImpl as any; \ No newline at end of file diff --git a/src/internal/util/ObjectUnsubscribedError.ts b/src/internal/util/ObjectUnsubscribedError.ts index 3c694a48be6..e068c543472 100644 --- a/src/internal/util/ObjectUnsubscribedError.ts +++ b/src/internal/util/ObjectUnsubscribedError.ts @@ -1,3 +1,19 @@ +export interface ObjectUnsubscribedError extends Error { +} + +export interface ObjectUnsubscribedErrorCtor { + new(): ObjectUnsubscribedError; +} + +function ObjectUnsubscribedErrorImpl(this: any) { + Error.call(this); + this.message = 'object unsubscribed'; + this.name = 'ObjectUnsubscribedError'; + return this; +} + +ObjectUnsubscribedErrorImpl.prototype = Object.create(Error.prototype); + /** * An error thrown when an action is invalid because the object has been * unsubscribed. @@ -7,12 +23,4 @@ * * @class ObjectUnsubscribedError */ -export class ObjectUnsubscribedError extends Error { - - public readonly name = 'ObjectUnsubscribedError'; - - constructor() { - super('object unsubscribed'); - (Object as any).setPrototypeOf(this, ObjectUnsubscribedError.prototype); - } -} +export const ObjectUnsubscribedError: ObjectUnsubscribedErrorCtor = ObjectUnsubscribedErrorImpl as any; \ No newline at end of file diff --git a/src/internal/util/TimeoutError.ts b/src/internal/util/TimeoutError.ts index 8996f6b011f..e239c38eac2 100644 --- a/src/internal/util/TimeoutError.ts +++ b/src/internal/util/TimeoutError.ts @@ -1,3 +1,19 @@ +export interface TimeoutError extends Error { +} + +export interface TimeoutErrorCtor { + new(): TimeoutError; +} + +function TimeoutErrorImpl(this: any) { + Error.call(this); + this.message = 'Timeout has occurred'; + this.name = 'TimeoutError'; + return this; +} + +TimeoutErrorImpl.prototype = Object.create(Error.prototype); + /** * An error thrown when duetime elapses. * @@ -5,12 +21,4 @@ * * @class TimeoutError */ -export class TimeoutError extends Error { - - public readonly name = 'TimeoutError'; - - constructor() { - super('Timeout has occurred'); - (Object as any).setPrototypeOf(this, TimeoutError.prototype); - } -} +export const TimeoutError: TimeoutErrorCtor = TimeoutErrorImpl as any; \ No newline at end of file diff --git a/src/internal/util/UnsubscriptionError.ts b/src/internal/util/UnsubscriptionError.ts index 060096b719f..293b9678a90 100644 --- a/src/internal/util/UnsubscriptionError.ts +++ b/src/internal/util/UnsubscriptionError.ts @@ -1,15 +1,25 @@ +export interface UnsubscriptionError extends Error { + readonly errors: any[]; +} + +export interface UnsubscriptionErrorCtor { + new(errors: any[]): UnsubscriptionError; +} + +function UnsubscriptionErrorImpl(this: any, errors: any[]) { + Error.call(this); + this.message = errors ? + `${errors.length} errors occurred during unsubscription: +${errors.map((err, i) => `${i + 1}) ${err.toString()}`).join('\n ')}` : ''; + this.name = 'UnsubscriptionError'; + this.errors = errors; + return this; +} + +UnsubscriptionErrorImpl.prototype = Object.create(Error.prototype); + /** * An error thrown when one or more errors have occurred during the * `unsubscribe` of a {@link Subscription}. */ -export class UnsubscriptionError extends Error { - - public readonly name = 'UnsubscriptionError'; - - constructor(public errors: any[]) { - super(errors ? - `${errors.length} errors occurred during unsubscription: - ${errors.map((err, i) => `${i + 1}) ${err.toString()}`).join('\n ')}` : ''); - (Object as any).setPrototypeOf(this, UnsubscriptionError.prototype); - } -} +export const UnsubscriptionError: UnsubscriptionErrorCtor = UnsubscriptionErrorImpl as any; \ No newline at end of file