Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/observables. A spec-faithful yet ergonomic TC39-inspired Observable library with detailed ty
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286/** * Error operators decide what should happen after a stage fails. * * In pass-through mode, a thrown error can keep moving downstream as an * `ObservableError` value. * * ```text * ordinary value -> map() -> filter() -> consumer * thrown error -> ObservableError -> catchErrors()/ignoreErrors()/throwErrors() * ``` * * These operators decide what should happen next: drop the failure, replace * it, inspect it, count it, or throw it. * * @module */import type { ExcludeError, Operator } from "../_types.ts";import { isObservableError, ObservableError } from "../../error.ts";import { createOperator, createStatefulOperator } from "../operators.ts";
/** * Removes wrapped failures and keeps only successful values. * * @example Drop failed values * ```ts * import { ObservableError, from, ignoreErrors, pipe } from "./helpers/mod.ts"; * * const sourceStream = from([ * 1, * ObservableError.from(new Error("fail"), "example"), * 2, * 3, * ]); * * const cleanStream = pipe(sourceStream, ignoreErrors()); * ``` */export function ignoreErrors<T>(): Operator< T | ObservableError, ExcludeError<T>> { return createOperator<T | ObservableError, T>({ name: "ignoreErrors", errorMode: "ignore", transform(chunk, controller) { controller.enqueue(chunk as ExcludeError<T>); }, });}
/** * Replaces each wrapped failure with a fallback value. * * @example Fall back to a default value * ```ts * import { ObservableError, catchErrors, from, pipe } from "./helpers/mod.ts"; * * const sourceStream = from([ * 1, * ObservableError.from(new Error("fail"), "example"), * ]); * * const safeStream = pipe(sourceStream, catchErrors("default")); * ``` */export function catchErrors<T, R>( fallback: R,): Operator<T | ObservableError, ExcludeError<T> | R> { return createOperator<T | ObservableError, ExcludeError<T> | R>({ name: "catchErrors", errorMode: "manual", transform(chunk, controller) { if (isObservableError(chunk)) { controller.enqueue(fallback); } else { controller.enqueue(chunk as ExcludeError<T>); } }, });}
/** * Turns each wrapped failure into a new value. * * @example Map failures to status objects * ```ts * import { ObservableError, from, mapErrors, pipe } from "./helpers/mod.ts"; * * const sourceStream = from([ * 1, * ObservableError.from(new Error("Not Found"), "lookup"), * ObservableError.from(new Error("Server Error"), "lookup"), * ]); * * const handledStream = pipe( * sourceStream, * mapErrors((err) => { * if (err.message === "Not Found") return { status: 404 }; * return { status: 500 }; * }), * ); * ``` */export function mapErrors<T, E>( errorMapper: (error: ObservableError) => E,): Operator<T | ObservableError, ExcludeError<T> | E> { return createOperator<T | ObservableError, ExcludeError<T> | E>({ name: "mapErrors", errorMode: "manual", transform(chunk, controller) { if (isObservableError(chunk)) { try { const mappedError = errorMapper(chunk); controller.enqueue(mappedError); } catch (mapperError) { controller.error( new ObservableError( [mapperError, chunk], `Your error mapper function threw an error: ${mapperError}`, { operator: "operator:mapErrors:mapper", value: mapperError, cause: chunk, tip: "The function passed to mapErrors threw an error. Check its implementation.", }, ), ); } } else { controller.enqueue(chunk as ExcludeError<T>); } }, });}
/** * Keeps only wrapped failures and drops successful values. * * @example Split failures into their own stream * ```ts * import { ObservableError, from, onlyErrors, pipe } from "./helpers/mod.ts"; * * const sourceStream = from([ * 1, * ObservableError.from(new Error("fail"), "example"), * 2, * ]); * * const errorStream = pipe(sourceStream, onlyErrors()); * ``` */export function onlyErrors<T>(): Operator< T | ObservableError, ObservableError> { return createOperator<T | ObservableError, ObservableError>({ name: "onlyErrors", errorMode: "manual", transform(chunk, controller) { if (isObservableError(chunk)) { controller.enqueue(chunk); } }, });}
/** * Counts how many values succeeded and how many failed. * * @example Build a final summary * ```ts * import { ObservableError, from, pipe, summarizeErrors } from "./helpers/mod.ts"; * * const sourceStream = from([ * 1, * ObservableError.from(new Error("fail"), "example"), * 2, * 3, * ]); * * const summary = await pipe(sourceStream, summarizeErrors()).toPromise(); * ``` */export function summarizeErrors<T>(): Operator<T | ObservableError, { successCount: number; errorCount: number; totalProcessed: number; successRate: number;}> { return createStatefulOperator<T | ObservableError, { successCount: number; errorCount: number; totalProcessed: number; successRate: number; }, { successCount: number; errorCount: number }>({ name: "summarizeErrors", errorMode: "manual", createState: () => ({ successCount: 0, errorCount: 0 }), transform(chunk, state) { if (isObservableError(chunk)) { state.errorCount++; } else { state.successCount++; } }, flush(state, controller) { const total = state.successCount + state.errorCount; controller.enqueue({ successCount: state.successCount, errorCount: state.errorCount, totalProcessed: total, successRate: total > 0 ? state.successCount / total : 0, }); }, });}
/** * Runs side effects for failures without changing the stream. * * @example Log each failure and keep going * ```ts * import { ObservableError, from, pipe, tapError } from "./helpers/mod.ts"; * * const sourceStream = from([ * 1, * ObservableError.from(new Error("fail"), "example"), * ]); * * const tappedStream = pipe( * sourceStream, * tapError((err) => console.error("Found an error:", err)), * ); * ``` */export function tapError<T>( sideEffect: (error: ObservableError) => void,): Operator<T | ObservableError, T | ObservableError> { return createOperator<T | ObservableError, T | ObservableError>({ name: "tapError", errorMode: "manual", transform(chunk, controller) { try { if (isObservableError(chunk)) { sideEffect(chunk); } } catch (_) { /* no empty */ } finally { controller.enqueue(chunk); } }, });}
/** * Throws as soon as a wrapped failure appears. * * @example Stop on the first failure * ```ts * import { ObservableError, from, pipe, throwErrors } from "./helpers/mod.ts"; * * const sourceStream = from([ * 1, * ObservableError.from(new Error("fail"), "example"), * 2, * ]); * * const throwingStream = pipe(sourceStream, throwErrors()); * * for await (const item of throwingStream) { * console.log(item); * } * ``` */export function throwErrors<T>(): Operator<T | ObservableError, T> { return createOperator({ name: "throwErrors", errorMode: "throw", transform(chunk, controller) { controller.enqueue(chunk as T); }, });}