Skip to content

Handling errors ​

Every operation can fail in a few specific, named ways. Each error has a _tag, so you can handle the ones you care about and let the rest through.

ts
import { Effect } from "effect";
import { fit, prophetFittingBackendLayer } from "effect-prophet";

// A typo: the timestamp is not a full UTC ISO string.
const history = [
  { timestamp: "2025-01-01", value: 10 },
  { timestamp: "2025-01-02T00:00:00.000Z", value: 12 },
];

const message = await Effect.runPromise(
  fit(history).pipe(
    Effect.as("fitted"),
    // Every failure has a `_tag`, so you can handle each kind separately.
    Effect.catchTag("InputValidationError", (error) =>
      Effect.succeed(`Bad input (${error.input}): ${error.message}`),
    ),
    Effect.catchTag("FittingError", (error) =>
      Effect.succeed(`Could not fit (${error.reason}): ${error.message}`),
    ),
    Effect.provide(prophetFittingBackendLayer),
  ),
);

console.log(message);
txt
Bad input (observations): Expected a valid canonical UTC timestamp (for example, 2024-01-01T00:00:00.000Z)
  at [0]["timestamp"]

The errors you may see ​

ErrorMeaningTypical fix
InputValidationErrorThe data or options are malformed: a bad timestamp, a missing field, an unknown optionThe message and issues point to the exact field
FittingErrorThe model couldn't be fitted to this data. reason says why, e.g. insufficient-observations or memory-limit.Check for too few rows or all-identical timestamps; see limits
PredictionErrorA forecast couldn't be computed for a rowCheck that future rows have every field the model needs
CountryHolidayErrorA country calendar couldn't be used. reason says why, e.g. known-coverage-restriction.See calendar errors
ModelSerializationErrorSaved model data couldn't be readMake sure it was saved by the same version
EvaluationError and friendsCross-validation, search or holdout checks failedreason and the wrapped cause explain which step

Without Effect ​

If you just await Effect.runPromise(...) and an error happens, the promise rejects with that error, so a normal try/catch works:

ts
try {
  const model = await Effect.runPromise(
    fit(history).pipe(Effect.provide(prophetFittingBackendLayer)),
  );
} catch (error) {
  console.error(error);
}

Inspired by Prophet from Meta. Not affiliated with Meta.