Skip to content

Commit

Permalink
fix(error): custom error inherits via setPrototypeof
Browse files Browse the repository at this point in the history
BREAKING CHANGE: IE10 and lower will need to polyfill `Object.setPrototypeOf`
  • Loading branch information
kwonoj committed Oct 27, 2017
1 parent 6fe5af3 commit 9a1ff90
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 24 deletions.
4 changes: 1 addition & 3 deletions spec/util/UnsubscriptionError-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ describe('UnsubscriptionError', () => {
subscription.unsubscribe();
} catch (err) {
expect(err instanceof UnsubscriptionError).to.equal(true);
expect(err.message).to.equal(`2 errors occurred during unsubscription:
1) ${err1}
2) ${err2}`);
expect(err.errors).to.deep.equal([err1, err2]);
expect(err.name).to.equal('UnsubscriptionError');
}
});
Expand Down
4 changes: 4 additions & 0 deletions src/observable/dom/AjaxObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,9 @@ export class AjaxError extends Error {
this.status = xhr.status;
this.responseType = xhr.responseType || request.responseType;
this.response = parseXhrResponse(this.responseType, xhr);

this.name = 'AjaxError';
(Object as any).setPrototypeOf(this, AjaxError.prototype);
}
}

Expand Down Expand Up @@ -480,5 +483,6 @@ function parseXhrResponse(responseType: string, xhr: XMLHttpRequest) {
export class AjaxTimeoutError extends AjaxError {
constructor(xhr: XMLHttpRequest, request: AjaxRequest) {
super('ajax timeout', xhr, request);
(Object as any).setPrototypeOf(this, AjaxTimeoutError.prototype);
}
}
7 changes: 3 additions & 4 deletions src/util/ArgumentOutOfRangeError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
*/
export class ArgumentOutOfRangeError extends Error {
constructor() {
const err: any = super('argument out of range');
(<any> this).name = err.name = 'ArgumentOutOfRangeError';
(<any> this).stack = err.stack;
(<any> this).message = err.message;
super('argument out of range');
this.name = 'ArgumentOutOfRangeError';
(Object as any).setPrototypeOf(this, ArgumentOutOfRangeError.prototype);
}
}
7 changes: 3 additions & 4 deletions src/util/EmptyError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
*/
export class EmptyError extends Error {
constructor() {
const err: any = super('no elements in sequence');
(<any> this).name = err.name = 'EmptyError';
(<any> this).stack = err.stack;
(<any> this).message = err.message;
super('no elements in sequence');
this.name = 'EmptyError';
(Object as any).setPrototypeOf(this, EmptyError.prototype);
}
}
7 changes: 3 additions & 4 deletions src/util/ObjectUnsubscribedError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
*/
export class ObjectUnsubscribedError extends Error {
constructor() {
const err: any = super('object unsubscribed');
(<any> this).name = err.name = 'ObjectUnsubscribedError';
(<any> this).stack = err.stack;
(<any> this).message = err.message;
super('object unsubscribed');
this.name = 'ObjectUnsubscribedError';
(Object as any).setPrototypeOf(this, ObjectUnsubscribedError.prototype);
}
}
7 changes: 3 additions & 4 deletions src/util/TimeoutError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
*/
export class TimeoutError extends Error {
constructor() {
const err: any = super('Timeout has occurred');
(<any> this).name = err.name = 'TimeoutError';
(<any> this).stack = err.stack;
(<any> this).message = err.message;
super('Timeout has occurred');

(Object as any).setPrototypeOf(this, TimeoutError.prototype);
}
}
8 changes: 3 additions & 5 deletions src/util/UnsubscriptionError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
*/
export class UnsubscriptionError extends Error {
constructor(public errors: any[]) {
super();
const err: any = Error.call(this, errors ?
super(errors ?
`${errors.length} errors occurred during unsubscription:
${errors.map((err, i) => `${i + 1}) ${err.toString()}`).join('\n ')}` : '');
(<any> this).name = err.name = 'UnsubscriptionError';
(<any> this).stack = err.stack;
(<any> this).message = err.message;
this.name = 'UnsubscriptionError';
(Object as any).setPrototypeOf(this, UnsubscriptionError.prototype);
}
}

0 comments on commit 9a1ff90

Please sign in to comment.