[core] Add .onError() handler to service#1606
Conversation
|
| Name | Type |
|---|---|
| xstate | Patch |
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
Andarist
left a comment
There was a problem hiding this comment.
It would be great to define (in the docs) what's the exact purpose of onError. What it should do and what it can't do. Is it useful only for reporting? Can it influence anyhow the machine's lifetime? This seems vastly important for users wanting to use this.
| return this; | ||
| } | ||
|
|
||
| private handleError(errorData: unknown): void { |
There was a problem hiding this comment.
there are only 2 places in which this is currently called:
- when an error gets caught when we send an error event to itself after catching promise rejection
- when an action throws during
exec
There are still a lot of uncovered situations:
- errors thrown during guards evaluation
- errors thrown during service instantiation
- errors received from machines (should we hook into their
onError?) and from observables
There was a problem hiding this comment.
Right, we should handle errors there as well.
|
Would be great to define the role of this handler as mentioned here: #1606 (review) . We might change the semantics later but would be great to establish what this PR is currently aiming for. |
The purpose of
It's useful for reporting and reacting to errors.
No. |
Is it supposed to handle only uncaught/unhandled errors/events or all? I think the best one would be the former - handled stuff would lead to noise. I haven't rechecked right now which is most likely based on the provided implementation - just trying to establish key goals of this PR without looking at the implementation for now. Is it supposed to only receive errors that originated in the top-level machine or its invoked actors? Or should it (in the future perhaps) be responsible for receiving all uncaught errors from the whole "system" that it represents?
How one would react to an error from it? I mean - what action could one take from there? Just kill the machine? Or maybe something else? |
To make it simple,
Actors don't send errors to the parent by default; that's what
That's up to the developer. Stop/restart the machine, log the error, etc. |
|
I would temporarily park this PR - until I post the error-related RFC, based on our Discord conversations. It has the potential of influencing the overall design of error handling but also this particular API. |
|
@davidkpiano I noticed that errors thrown within The company I'm currently working for was considering using
Am I thinking about this in the wrong way? |
Yes, the error RFC has been recently been made internally, and we will surface it soon.
Yes you can; best to wrap in a try/catch 👍
Currently, whatever is easiest for you all. Ideally, these interactions will be in an invoked service, but the end result is the same: the machine changing state is due to an event, whether those are events sent by a service or externally. Only a minor refactor is needed in the future if you're currently sending them externally and want to move to a service. |
|
This PR targets |
This PR adds the
.onError(...)handler to services:This was necessary to test the behavior of tweaking
reportUnhandledExceptionOnInvocationto check thatoriginalErroris actually anError, sincereject(anything)might not be anErrorobject, soerr.stackwould itself throw an error.Still deciding if we want to add
.onError((err, state) => { ... })or other metadata 🤔