import { Effect } from "effect"; import { type SmithersErrorCode, SmithersError, toSmithersError, } from "../utils/errors"; export type ErrorWrapOptions = { code?: SmithersErrorCode; details?: Record; }; export function toError( cause: unknown, label?: string, options: ErrorWrapOptions = {}, ): SmithersError { return toSmithersError(cause, label, options); } export function fromPromise( label: string, evaluate: () => PromiseLike, options: ErrorWrapOptions = {}, ): Effect.Effect { return Effect.tryPromise({ try: () => evaluate(), catch: (cause) => toError(cause, label, options), }); } export function fromSync( label: string, evaluate: () => A, options: ErrorWrapOptions = {}, ): Effect.Effect { return Effect.try({ try: () => evaluate(), catch: (cause) => toError(cause, label, options), }); } /** * Run a synchronous side-effect, silently swallowing any thrown error. * Useful for best-effort cleanup (process.kill, sqlite.close, etc.). */ export function ignoreSyncError(label: string, fn: () => void): Effect.Effect { return Effect.sync(() => { try { fn(); } catch { // intentionally swallowed – best-effort cleanup } }); }