Skip to content

Feature: allow throwError to accept a factory function #5617

Closed
@benlesh

Description

The thought is this: Frequently, you don't want to create an Error object until the moment the error happens. However, currently, throwError only accepts the instance of the error it is going to throw as an argument.

I'm wondering if we could allow it to optionally take a factory function that will create the error at the moment of subscription.

This would be a breaking change, however, as right now, it's possible to emit a function as an error by using throwError:

Current behavior when you provide a function

throwError(new Error('bad'  + Date.now())).subscribe({
  error: console.error // logs the error above, but the call stack is from the moment the `throwError` was called, not from during the subscription.
})

throwError(() => new Error('bad' + Date.now())).subscribe({
  error: console.error // logs a function reference
})

Proposed change

throwError(new Error('bad' + Date.now()); // This is unchanged

// NEW:
throwError(() => new Error('bad' + Date.now())).subscribe({
  error: console.error // logs the error created above, with a call stack from that place
})

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

featurePRs and issues for features

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions