From 5c52a7337942dc157ae425d2f0362c57f222ecfa Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Fri, 27 Jul 2018 19:14:10 -0700 Subject: [PATCH] fix(IE10): Remove dependency on Object.setPrototypeOf (#3967) 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 | 40 ++++++++------ src/internal/util/UnsubscriptionError.ts | 32 +++++++---- 6 files changed, 129 insertions(+), 76 deletions(-) diff --git a/src/internal/observable/dom/AjaxObservable.ts b/src/internal/observable/dom/AjaxObservable.ts index cedfb3a60c..e258e6ddc7 100644 --- a/src/internal/observable/dom/AjaxObservable.ts +++ b/src/internal/observable/dom/AjaxObservable.ts @@ -461,7 +461,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; @@ -476,22 +476,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(message: string, xhr: XMLHttpRequest, request: AjaxRequest): 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 parseJson(xhr: XMLHttpRequest) { // HACK(benlesh): TypeScript shennanigans // tslint:disable-next-line:no-any XMLHttpRequest is defined to always have 'response' inferring xhr as never for the else clause. @@ -517,17 +523,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 90cd3324e3..949bfcc9da 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 743e43dafb..6dacfc6aa8 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 3c694a48be..e068c54347 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 e0a347c79b..a36162d9be 100644 --- a/src/internal/util/TimeoutError.ts +++ b/src/internal/util/TimeoutError.ts @@ -1,16 +1,24 @@ -/** - * An error thrown when duetime elapses. - * - * @see {@link timeout} - * - * @class TimeoutError - */ -export class TimeoutError extends Error { - - public readonly name = 'TimeoutError'; - - constructor() { - super('Timeout has occurred'); - (Object as any).setPrototypeOf(this, TimeoutError.prototype); - } -} +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. + * + * @see {@link timeout} + * + * @class TimeoutError + */ +export const TimeoutError: TimeoutErrorCtor = TimeoutErrorImpl as any; diff --git a/src/internal/util/UnsubscriptionError.ts b/src/internal/util/UnsubscriptionError.ts index 060096b719..293b9678a9 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