import { Cause, Data, Effect, Exit, Match, Option } from 'effect'
class AlphaError extends Data.TaggedError('AlphaError')<{ data: number }> {}
class BetaError extends Data.TaggedError('BetaError')<{ data: string }> {}
const exit = await Effect.runPromiseExit(
Effect.gen(function* () {
const x = Math.random()
if (x <= 0.25) {
yield* new AlphaError({ data: 1 })
} else if (x > 0.75) {
yield* new BetaError({ data: 'x' })
} else {
return true
}
}),
)
Exit.match(exit, {
onFailure: (cause) =>
Option.match(Cause.failureOption(cause), {
onNone: () => console.log('n/a'),
onSome: (cause) =>
Match.value(cause).pipe(
// >> REUSABLE MATCHER FOR AlphaError GOES HERE <<
Match.tags({
BetaError: (e) => {
console.log('BetaError:', e.data)
},
}),
Match.exhaustive,
),
}),
onSuccess: (value) => console.log('n/a'),
})
import { Cause, Data, Effect, Exit, Match, Option } from 'effect'
class AlphaError extends Data.TaggedError('AlphaError')<{ data: number }> {}
class BetaError extends Data.TaggedError('BetaError')<{ data: string }> {}
const exit = await Effect.runPromiseExit(
Effect.gen(function* () {
const x = Math.random()
if (x <= 0.25) {
yield* new AlphaError({ data: 1 })
} else if (x > 0.75) {
yield* new BetaError({ data: 'x' })
} else {
return true
}
}),
)
Exit.match(exit, {
onFailure: (cause) =>
Option.match(Cause.failureOption(cause), {
onNone: () => console.log('n/a'),
onSome: (cause) =>
Match.value(cause).pipe(
// >> REUSABLE MATCHER FOR AlphaError GOES HERE <<
Match.tags({
BetaError: (e) => {
console.log('BetaError:', e.data)
},
}),
Match.exhaustive,
),
}),
onSuccess: (value) => console.log('n/a'),
})