Skip to content

Commit

Permalink
fix(IE10): Remove dependency on Object.setPrototypeOf
Browse files Browse the repository at this point in the history
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
  • Loading branch information
benlesh committed Jul 28, 2018
1 parent 310b9f5 commit 08326ea
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 76 deletions.
55 changes: 33 additions & 22 deletions src/internal/observable/dom/AjaxObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
Expand All @@ -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;
26 changes: 17 additions & 9 deletions src/internal/util/ArgumentOutOfRangeError.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
26 changes: 17 additions & 9 deletions src/internal/util/EmptyError.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
26 changes: 17 additions & 9 deletions src/internal/util/ObjectUnsubscribedError.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
40 changes: 24 additions & 16 deletions src/internal/util/TimeoutError.ts
Original file line number Diff line number Diff line change
@@ -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;
32 changes: 21 additions & 11 deletions src/internal/util/UnsubscriptionError.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 08326ea

Please sign in to comment.