Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: allow throwError to accept a factory function #5617

Closed
benlesh opened this issue Jul 31, 2020 · 1 comment · Fixed by #5647
Closed

Feature: allow throwError to accept a factory function #5617

benlesh opened this issue Jul 31, 2020 · 1 comment · Fixed by #5647
Assignees
Labels
feature PRs and issues for features

Comments

@benlesh
Copy link
Member

benlesh commented Jul 31, 2020

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
})
@benlesh benlesh added AGENDA ITEM Flagged for discussion at core team meetings type: discussion labels Jul 31, 2020
@benlesh
Copy link
Member Author

benlesh commented Aug 12, 2020

General approval from core team.

@benlesh benlesh self-assigned this Aug 12, 2020
@benlesh benlesh added feature PRs and issues for features and removed AGENDA ITEM Flagged for discussion at core team meetings type: discussion labels Aug 12, 2020
benlesh added a commit to benlesh/rxjs that referenced this issue Aug 16, 2020
- Adds feature to allow the error instance the consumer is notified with to be created at the moment of notification.
- Updates tests to use "run mode".
- Improves documentation, including recommendations on when `throwError` is not necessary.

BREAKING CHANGE: In an extreme corner case for usage, `throwError` is no longer able to emit a function as an error directly. If you need to push a function as an error, you will have to use the factory function to return the function like so: `throwError(() => functionToEmit)`, in other words `throwError(() => () => console.log('called later'))`.

resolves ReactiveX#5617
benlesh added a commit that referenced this issue Aug 18, 2020
* feat(throwError): now accepts a factory to create the error

- Adds feature to allow the error instance the consumer is notified with to be created at the moment of notification.
- Updates tests to use "run mode".
- Improves documentation, including recommendations on when `throwError` is not necessary.

BREAKING CHANGE: In an extreme corner case for usage, `throwError` is no longer able to emit a function as an error directly. If you need to push a function as an error, you will have to use the factory function to return the function like so: `throwError(() => functionToEmit)`, in other words `throwError(() => () => console.log('called later'))`.

resolves #5617

* chore: clean up and address comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature PRs and issues for features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant