import { Resume } from '@typed/env' import { Just } from '@typed/maybe' import { Effect, Effects, Return } from '../Effect' import { runWith, RunWith } from '../run/runWith' import { ErrorOf, FailEnv, Failure, FailureTypes } from './Failure' export function* fail( errorType: A, error: Err, ): Effects, any> { return yield (c) => c[errorType](error) } export function catchFailure, K extends FailureTypes>( effect: A, errorType: K, onError: (error: ErrorOf) => Return, ) { const failEnv = { [errorType]: (e: ErrorOf) => Resume.of(Failure.of(e, Just.of(onError(e)))), } as FailEnv> return runWith(effect, failEnv) as RunWith }