Effect CommunityEC
Effect Community3y ago
11 replies
Frederic

Handling Failure for "Option.none" in an Effect

If I want an effect to fail on an "Option.none" is this the way to go or is there a smarter approach ?:
const getProduct = (prodName: string) => {
    return Effect.tryPromise({
        try: () => getEntry('products', 'index'),
        catch: (err) => new AstroCollectionError('collection products not found', err as Error)
    }).pipe(
        Effect.map(coll => coll.data),
        Effect.map( ReadonlyArray.findFirst(e => e.name === prodName )),
        Effect.flatMap( Option.match({
            onNone: () => Effect.fail(new EntryNotFoundError(`${prodName} in Products`)),
            onSome: (value) => Effect.succeed(value)
        }))
    )
}
Was this page helpful?