Effect CommunityEC
Effect Community3y ago
23 replies
matias

Idiomatic way to interrupt an Effect when an error occurs inside Effect.gen

What would be the idiomatic way of interrupt an Effect when an error happens inside an Effect.gen?

Example code
function program(){
    Effect.gen(function*($) {
        yield* $(function1())
        yield* $(function2()) 
        yield* $(lastStep())
        // All of this function can fail with different Errors in the Error channel 
        Effect.catchAll(error => {
             return pipe(
                 Effect.log('Effect have errors'),
                 () => sendErrorMessage()
                 () => Effect.interrupt()
                 () => Effect.log('Execution stopped')
             )
        })
        return true
    })
}


Will that Effect.catchAll be the way to fallback if some of the previous function errored?
The idea is to perform some cleanup and messaging functions and interrupt the execution

What if I want to interrupt the execution early? let's say only move into lastStep() call if the previous functions were succesfull ?

Consider function1() and function2() using Effect.tryCatchPromise
Was this page helpful?