From eec927eebed58aa34c52e87f110b16a32a247266 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Mon, 3 Oct 2016 20:45:34 -0700 Subject: [PATCH] refactor(TimeoutError): timeout throws typed TimeoutError closes #1997 --- spec/operators/timeout-spec.ts | 2 +- src/Rx.ts | 1 + src/operator/timeout.ts | 11 ++++++----- src/util/TimeoutError.ts | 15 +++++++++++++++ 4 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 src/util/TimeoutError.ts diff --git a/spec/operators/timeout-spec.ts b/spec/operators/timeout-spec.ts index 82b81d81df..95e66bde9b 100644 --- a/spec/operators/timeout-spec.ts +++ b/spec/operators/timeout-spec.ts @@ -6,7 +6,7 @@ const Observable = Rx.Observable; /** @test {timeout} */ describe('Observable.prototype.timeout', () => { - const defaultTimeoutError = new Error('timeout'); + const defaultTimeoutError = new Rx.TimeoutError(); asDiagram('timeout(50)')('should timeout after a specified timeout period', () => { const e1 = cold('-------a--b--|'); diff --git a/src/Rx.ts b/src/Rx.ts index 39d64953fb..cc6322359f 100644 --- a/src/Rx.ts +++ b/src/Rx.ts @@ -155,6 +155,7 @@ export {Notification} from './Notification'; export {EmptyError} from './util/EmptyError'; export {ArgumentOutOfRangeError} from './util/ArgumentOutOfRangeError'; export {ObjectUnsubscribedError} from './util/ObjectUnsubscribedError'; +export {TimeoutError} from './util/TimeoutError'; export {UnsubscriptionError} from './util/UnsubscriptionError'; export {TimeInterval} from './operator/timeInterval'; export {Timestamp} from './operator/timestamp'; diff --git a/src/operator/timeout.ts b/src/operator/timeout.ts index 94c64597dc..d7d52aa5ed 100644 --- a/src/operator/timeout.ts +++ b/src/operator/timeout.ts @@ -5,6 +5,7 @@ import { Subscriber } from '../Subscriber'; import { Scheduler } from '../Scheduler'; import { Observable } from '../Observable'; import { TeardownLogic } from '../Subscription'; +import { TimeoutError } from '../util/TimeoutError'; /** * @param due @@ -80,7 +81,7 @@ class TimeoutSubscriber extends Subscriber { this._previousIndex = currentIndex; } - protected _next(value: T) { + protected _next(value: T): void { this.destination.next(value); if (!this.absoluteTimeout) { @@ -88,17 +89,17 @@ class TimeoutSubscriber extends Subscriber { } } - protected _error(err: any) { + protected _error(err: any): void { this.destination.error(err); this._hasCompleted = true; } - protected _complete() { + protected _complete(): void { this.destination.complete(); this._hasCompleted = true; } - notifyTimeout() { - this.error(this.errorToSend || new Error('timeout')); + notifyTimeout(): void { + this.error(this.errorToSend || new TimeoutError()); } } diff --git a/src/util/TimeoutError.ts b/src/util/TimeoutError.ts new file mode 100644 index 0000000000..c68adffac4 --- /dev/null +++ b/src/util/TimeoutError.ts @@ -0,0 +1,15 @@ +/** + * An error thrown when duetime elapses. + * + * @see {@link timeout} + * + * @class TimeoutError + */ +export class TimeoutError extends Error { + constructor() { + const err: any = super('Timeout has occurred'); + ( this).name = err.name = 'TimeoutError'; + ( this).stack = err.stack; + ( this).message = err.message; + } +}