[]previous and next page

catchError

Runs fn inside a new child scope whose errors go to handler: errors thrown synchronously by fn, and later errors of the effects, memos and cleanups it created.

Runs fn inside a new child scope whose errors go to handler: errors thrown synchronously by fn, and later errors of the effects, memos and cleanups it created. The scope is disposed with the current owner. Use it to contain the failures of one part of a component tree.

Import#

ts
import { catchError } from "@shadow-js/core";

Signature#

ts
function catchError<T>(fn: () => T, handler: ErrorHandler): T | undefined

Parameters#

Name Description
fn The function to run; it runs untracked, with the new scope as owner.
handler Receives each error, untracked. Throw from it to pass the error on to the next handler up.

Returns#

Whatever fn returns, or undefined when fn threw and handler took the error.

Throws#

  • Rethrows the error handler throws when no handler above takes it (outside a flush).

Examples#

ts
import { catchError, useEffect } from "@shadow-js/core";

function Widget() {
  return catchError(
    () => {
      useEffect(() => {
        throw new Error("network down");
      });
      return "ready";
    },
    (error) => console.error("widget failed:", error),
  );
}