2025/5/13 error management読む
Two Types of Errors
https://effect.website/docs/error-management/two-error-types/
Effectには2種類のエラーがある
Expected Errors (effect)
Unexpected Errors (effect)
Fallback
https://effect.website/docs/error-management/fallback/
失敗する可能性のある処理に対して、代替の処理を定義してエラーハンドリングを簡潔に行うための仕組み
Effect.orElse
Effect.orElseFail
Effect.orElseSucceed
Effect.firstSuccessOf
Matching
https://effect.website/docs/error-management/matching/
Effect.match
Effect.matchEffect
Effect.matchCause
Effect.matchCauseEffect
Effect.ignore
Retrying
https://effect.website/docs/error-management/retrying/
Effect.retry
Effect.retryOrElse
Timing Out
Effect.timeout
Sandboxing
Effect.sandbox
Error Accumulation
この辺の複数実行する系のAPIは、Fail Fast、つまり最初のエラーが発生した時点で全体を中断する
Effect.zip
Effect.all
Effect.forEach
Error Accumulation(エラー蓄積)とは
すべての処理を最後まで実行したい
複数のエラーをまとめて確認したい
Effect.validate
Effect.validateAll
Effect.validateFirst
Effect.partition
Effect.validateWith
Error Channel Operations
Effect.mapError
Effect.mapBoth
Success channel を条件で「落とす」
なぜ filter 系が必要か
Effect では
成功したあとでも
「値として不正」なら失敗にしたい
というケースが多い。
Filtering
Effect.filter
Effect.filterEffectOrElse
Effect.filterEffectOrFail
Effect.filterMap
Effect.filterOrFail
code:ts
A ──predicate──✗──→ Fail(E)
predicate が false → 回復可能エラー
エラーは Error channel に乗る
用途
バリデーション
認可チェック
null / undefined 排除
Effect.filterOrDie / Effect.filterOrDieMessage
predicate が false → 即死(Defect)
recover 不可
用途
「起きたらバグ」な状態
絶対に起きないはずの前提違反
Effect.filterOrElse
predicate が false → 代替 Effect を実行
用途
フォールバック
再試行
デフォルト処理
Error を「観測」する(挙動は変えない)
Effect.tapError
Effect.tapErrorTag
TaggedError のみ対象
特定エラー型だけ処理
用途
エラー種別ごとのログ
NetworkError / ValidationError 分岐
Effect.tapErrorCause
Cause 全体を観測
fail + die 両方含む
用途
障害解析
Sentry 連携
Effect.tapDefect
Die(致命的エラー)だけ
Fail は無視
用途
本当に危険なバグ検知
パニックログ
Effect.tapBoth
成功 / 失敗をまとめて観測
用途
実行結果の一元ログ
トレース用途
5. Error を Success 側に「露出」させる
Effect.either
Effect.cause
Effect.merge
Effect.flip
Parallel and Sequential Errors
Yieldable Errors