Skip to content

Commit 579cc46

Browse files
refactor(Error): Rewrite custom derived errors with Object.create().
1 parent 6922b16 commit 579cc46

6 files changed

+129
-83
lines changed

src/observable/dom/AjaxObservable.ts

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,52 @@ export class AjaxObservable<T> extends Observable<T> {
173173
}
174174
}
175175

176+
/**
177+
* A normalized AJAX error.
178+
*
179+
* @see {@link ajax}
180+
*
181+
* @class AjaxError
182+
*/
183+
export interface AjaxError extends Error {
184+
/** @type {XMLHttpRequest} The XHR instance associated with the error */
185+
xhr: XMLHttpRequest;
186+
187+
/** @type {AjaxRequest} The AjaxRequest associated with the error */
188+
request: AjaxRequest;
189+
190+
/** @type {number} The HTTP status code */
191+
status: number;
192+
}
193+
export interface AjaxErrorConstructor {
194+
new(message: string, xhr: XMLHttpRequest, request: AjaxRequest): AjaxError;
195+
readonly prototype: AjaxError;
196+
}
197+
198+
function AjaxErrorCtor(this: AjaxError, message: string, xhr: XMLHttpRequest, request: AjaxRequest): AjaxError {
199+
const err = Error.call(this, message);
200+
this.message = err.message;
201+
this.xhr = xhr;
202+
this.request = request;
203+
this.status = xhr.status;
204+
return this;
205+
}
206+
AjaxErrorCtor.prototype = Object.create(Error.prototype);
207+
AjaxErrorCtor.prototype.constructor = AjaxErrorCtor;
208+
209+
export const AjaxError = AjaxErrorCtor as any as AjaxErrorConstructor;
210+
211+
/**
212+
* @see {@link ajax}
213+
*
214+
* @class AjaxTimeoutError
215+
*/
216+
export class AjaxTimeoutError extends AjaxError {
217+
constructor(xhr: XMLHttpRequest, request: AjaxRequest) {
218+
super('ajax timeout', xhr, request);
219+
}
220+
}
221+
176222
/**
177223
* We need this JSDoc comment for affecting ESDoc.
178224
* @ignore
@@ -420,41 +466,4 @@ export class AjaxResponse {
420466
break;
421467
}
422468
}
423-
}
424-
425-
/**
426-
* A normalized AJAX error.
427-
*
428-
* @see {@link ajax}
429-
*
430-
* @class AjaxError
431-
*/
432-
export class AjaxError extends Error {
433-
/** @type {XMLHttpRequest} The XHR instance associated with the error */
434-
xhr: XMLHttpRequest;
435-
436-
/** @type {AjaxRequest} The AjaxRequest associated with the error */
437-
request: AjaxRequest;
438-
439-
/** @type {number} The HTTP status code */
440-
status: number;
441-
442-
constructor(message: string, xhr: XMLHttpRequest, request: AjaxRequest) {
443-
super(message);
444-
this.message = message;
445-
this.xhr = xhr;
446-
this.request = request;
447-
this.status = xhr.status;
448-
}
449-
}
450-
451-
/**
452-
* @see {@link ajax}
453-
*
454-
* @class AjaxTimeoutError
455-
*/
456-
export class AjaxTimeoutError extends AjaxError {
457-
constructor(xhr: XMLHttpRequest, request: AjaxRequest) {
458-
super('ajax timeout', xhr, request);
459-
}
460-
}
469+
}

src/util/ArgumentOutOfRangeError.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@
88
*
99
* @class ArgumentOutOfRangeError
1010
*/
11-
export class ArgumentOutOfRangeError extends Error {
12-
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;
17-
}
11+
export interface ArgumentOutOfRangeError extends Error {}
12+
export interface ArgumentOutOfRangeErrorConstructor {
13+
new(): ArgumentOutOfRangeError;
14+
readonly prototype: ArgumentOutOfRangeError;
1815
}
16+
17+
function ArgumentOutOfRangeErrorCtor(this: ArgumentOutOfRangeError): ArgumentOutOfRangeError {
18+
Error.call(this, 'argument out of range');
19+
this.name = 'ArgumentOutOfRangeError';
20+
return this;
21+
}
22+
ArgumentOutOfRangeErrorCtor.prototype = Object.create(Error.prototype);
23+
ArgumentOutOfRangeErrorCtor.prototype.constructor = ArgumentOutOfRangeErrorCtor;
24+
25+
export const ArgumentOutOfRangeError = ArgumentOutOfRangeErrorCtor as any as ArgumentOutOfRangeErrorConstructor;

src/util/EmptyError.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@
88
*
99
* @class EmptyError
1010
*/
11-
export class EmptyError extends Error {
12-
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;
17-
}
11+
export interface EmptyError extends Error {}
12+
export interface EmptyErrorConstructor {
13+
new(): EmptyError;
14+
readonly prototype: EmptyError;
1815
}
16+
17+
function EmptyErrorCtor(this: EmptyError): EmptyError {
18+
Error.call(this, 'no elements in sequence');
19+
this.name = 'EmptyError';
20+
return this;
21+
}
22+
EmptyErrorCtor.prototype = Object.create(Error.prototype);
23+
EmptyErrorCtor.prototype.constructor = EmptyErrorCtor;
24+
25+
export const EmptyError = EmptyErrorCtor as any as EmptyErrorConstructor;

src/util/ObjectUnsubscribedError.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@
77
*
88
* @class ObjectUnsubscribedError
99
*/
10-
export class ObjectUnsubscribedError extends Error {
11-
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;
16-
}
10+
export interface ObjectUnsubscribedError extends Error {}
11+
export interface ObjectUnsubscribedErrorConstructor {
12+
new(): ObjectUnsubscribedError;
13+
readonly prototype: ObjectUnsubscribedError;
1714
}
15+
16+
function ObjectUnsubscribedErrorCtor(this: ObjectUnsubscribedError): ObjectUnsubscribedError {
17+
Error.call(this, 'object unsubscribed');
18+
this.name = 'ObjectUnsubscribedError';
19+
return this;
20+
}
21+
ObjectUnsubscribedErrorCtor.prototype = Object.create(Error.prototype);
22+
ObjectUnsubscribedErrorCtor.prototype.constructor = ObjectUnsubscribedErrorCtor;
23+
24+
export const ObjectUnsubscribedError = ObjectUnsubscribedErrorCtor as any as ObjectUnsubscribedErrorConstructor;

src/util/TimeoutError.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
/**
2-
* An error thrown when duetime elapses.
3-
*
4-
* @see {@link timeout}
5-
*
6-
* @class TimeoutError
7-
*/
8-
export class TimeoutError extends Error {
9-
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;
14-
}
15-
}
1+
/**
2+
* An error thrown when duetime elapses.
3+
*
4+
* @see {@link timeout}
5+
*/
6+
export interface TimeoutError extends Error {}
7+
export interface TimeoutErrorConstructor {
8+
new(): TimeoutError;
9+
readonly prototype: TimeoutError;
10+
}
11+
12+
function TimeoutErrorCtor(this: TimeoutError): TimeoutError {
13+
Error.call(this, 'Timeout has occurred');
14+
this.name = 'TimeoutError';
15+
return this;
16+
}
17+
TimeoutErrorCtor.prototype = Object.create(Error.prototype);
18+
TimeoutErrorCtor.prototype.constructor = TimeoutErrorCtor;
19+
20+
export const TimeoutError = TimeoutErrorCtor as any as TimeoutErrorConstructor;

src/util/UnsubscriptionError.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@
22
* An error thrown when one or more errors have occurred during the
33
* `unsubscribe` of a {@link Subscription}.
44
*/
5-
export class UnsubscriptionError extends Error {
6-
constructor(public errors: any[]) {
7-
super();
8-
const err: any = Error.call(this, errors ?
9-
`${errors.length} errors occurred during unsubscription:
5+
export interface UnsubscriptionError extends Error {
6+
errors: any[];
7+
}
8+
export interface UnsubscriptionErrorConstructor {
9+
new(errors: any[]): UnsubscriptionError;
10+
readonly prototype: UnsubscriptionError;
11+
}
12+
13+
function UnsubscriptionErrorCtor(this: UnsubscriptionError, errors: any[]): UnsubscriptionError {
14+
const err: any = Error.call(this, errors ?
15+
`${errors.length} errors occurred during unsubscription:
1016
${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;
14-
}
17+
this.name = err.name = 'UnsubscriptionError';
18+
this.stack = err.stack;
19+
this.message = err.message;
20+
this.errors = errors;
21+
return this;
1522
}
23+
UnsubscriptionErrorCtor.prototype = Object.create(Error.prototype);
24+
UnsubscriptionErrorCtor.prototype.constructor = UnsubscriptionErrorCtor;
25+
26+
export const UnsubscriptionError = UnsubscriptionErrorCtor as any as UnsubscriptionErrorConstructor;

0 commit comments

Comments
 (0)