[]previous and next page

catchError, onError and handleError

catchError, onError and handleError in the Error handling guide.

For agentsUpdated

These three share one routing mechanism. Every owner (a component, effect, memo, root or scope) may carry error handlers. When something throws inside an owner's computation, the error goes to the handlers of the nearest owner that has any, walking up through parents.

  • catchError(fn, handler) runs fn in a new child scope that has handler. If fn throws synchronously, handler is called and catchError returns undefined. Errors thrown later by effects, memos and cleanups created inside also go to handler.
  • onError(handler) adds a handler to the current owner. It receives errors from that owner's descendants that no closer handler took. With no owner it warns NO_OWNER_ERROR. Handlers added to an effect or memo last for one run: they are cleared when it re-runs.
  • Several handlers on one owner are tried newest first. A handler that returns normally handled the error. A handler that throws passes its own error on: to the next handler, then up to the parents.
  • handleError(error, owner) from @shadow-js/core/extend starts the same routing by hand, from the owner you pass. Use it after an await, in a timer or in an event handler to send a failure into the boundary tree.