Skip to content

Commit 20e3b54

Browse files
committed
fix(error): custom error inherits via setPrototypeof
BREAKING CHANGE: IE10 and lower will need to polyfill `Object.setPrototypeOf`
1 parent 2a16312 commit 20e3b54

7 files changed

Lines changed: 20 additions & 24 deletions

File tree

spec/util/UnsubscriptionError-spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ describe('UnsubscriptionError', () => {
1919
subscription.unsubscribe();
2020
} catch (err) {
2121
expect(err instanceof UnsubscriptionError).to.equal(true);
22-
expect(err.message).to.equal(`2 errors occurred during unsubscription:
23-
1) ${err1}
24-
2) ${err2}`);
22+
expect(err.errors).to.deep.equal([err1, err2]);
2523
expect(err.name).to.equal('UnsubscriptionError');
2624
}
2725
});

src/observable/dom/AjaxObservable.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,9 @@ export class AjaxError extends Error {
452452
this.status = xhr.status;
453453
this.responseType = xhr.responseType || request.responseType;
454454
this.response = parseXhrResponse(this.responseType, xhr);
455+
456+
this.name = 'AjaxError';
457+
(Object as any).setPrototypeOf(this, AjaxError.prototype);
455458
}
456459
}
457460

@@ -480,5 +483,6 @@ function parseXhrResponse(responseType: string, xhr: XMLHttpRequest) {
480483
export class AjaxTimeoutError extends AjaxError {
481484
constructor(xhr: XMLHttpRequest, request: AjaxRequest) {
482485
super('ajax timeout', xhr, request);
486+
(Object as any).setPrototypeOf(this, AjaxTimeoutError.prototype);
483487
}
484488
}

src/util/ArgumentOutOfRangeError.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
*/
1111
export class ArgumentOutOfRangeError extends Error {
1212
constructor() {
13-
const err: any = super('argument out of range');
14-
(<any> this).name = err.name = 'ArgumentOutOfRangeError';
15-
(<any> this).stack = err.stack;
16-
(<any> this).message = err.message;
13+
super('argument out of range');
14+
this.name = 'ArgumentOutOfRangeError';
15+
(Object as any).setPrototypeOf(this, ArgumentOutOfRangeError.prototype);
1716
}
1817
}

src/util/EmptyError.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
*/
1111
export class EmptyError extends Error {
1212
constructor() {
13-
const err: any = super('no elements in sequence');
14-
(<any> this).name = err.name = 'EmptyError';
15-
(<any> this).stack = err.stack;
16-
(<any> this).message = err.message;
13+
super('no elements in sequence');
14+
this.name = 'EmptyError';
15+
(Object as any).setPrototypeOf(this, EmptyError.prototype);
1716
}
1817
}

src/util/ObjectUnsubscribedError.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
*/
1010
export class ObjectUnsubscribedError extends Error {
1111
constructor() {
12-
const err: any = super('object unsubscribed');
13-
(<any> this).name = err.name = 'ObjectUnsubscribedError';
14-
(<any> this).stack = err.stack;
15-
(<any> this).message = err.message;
12+
super('object unsubscribed');
13+
this.name = 'ObjectUnsubscribedError';
14+
(Object as any).setPrototypeOf(this, ObjectUnsubscribedError.prototype);
1615
}
1716
}

src/util/TimeoutError.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
*/
88
export class TimeoutError extends Error {
99
constructor() {
10-
const err: any = super('Timeout has occurred');
11-
(<any> this).name = err.name = 'TimeoutError';
12-
(<any> this).stack = err.stack;
13-
(<any> this).message = err.message;
10+
super('Timeout has occurred');
11+
12+
(Object as any).setPrototypeOf(this, TimeoutError.prototype);
1413
}
1514
}

src/util/UnsubscriptionError.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
*/
55
export class UnsubscriptionError extends Error {
66
constructor(public errors: any[]) {
7-
super();
8-
const err: any = Error.call(this, errors ?
7+
super(errors ?
98
`${errors.length} errors occurred during unsubscription:
109
${errors.map((err, i) => `${i + 1}) ${err.toString()}`).join('\n ')}` : '');
11-
(<any> this).name = err.name = 'UnsubscriptionError';
12-
(<any> this).stack = err.stack;
13-
(<any> this).message = err.message;
10+
this.name = 'UnsubscriptionError';
11+
(Object as any).setPrototypeOf(this, UnsubscriptionError.prototype);
1412
}
1513
}

0 commit comments

Comments
 (0)