Skip to content

TypeScript 2.1+ and Errors #2612

@benlesh

Description

@benlesh

Issue

Currently, we're extending Error to create the ability to do instanceof checks again things like ObjectUnsubscribedError. This is no longer something that TypeScript supports for a variety of principled reasons.

Proposed Change

We move to just throwing simple new Error(), but decorating that error with a custom property, and providing a method to check that property. Ideally, that property will be set with a Symbol to avoid collisions.

const timeoutErrorMsg = 'RxJS Timeout Error';
const timeoutErrorSymbol = Symbol(timeoutErrorMsg);

export function createTimeoutError(): Error {
  const error = new Error(timeoutErrorMsg');
  error[timeoutErrorSymbol] = true;
  return error;
}

export function isTimeoutError(err: Error): boolean {
  return err && err[timeoutErrorSymbol];
}

And then to check an error:

source$
  .timeout(3000)
  .catch(err => {
    if (isTimeoutError(err)) {
      // if it's a timeout error from Rx, handle it in some way
      return Observable.of({ handled: 'timeout error' });
    }
    throw err; // otherwise rethrow
  })

Other Things

We should probably make these utilties available on Rx.Util. for example: Rx.Util.isTimeoutError

This affects the following

  • EmptyError
  • TimeoutError
  • ArgumentOutOfRangeError
  • UnsubscriptionError
  • ObjectUnsubscribedError

This would be a BREAKING CHANGE for sure.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions