reject は、S04 の例外が非同期で発生した形です。データベースの障害や保存データの破損には、利用者が選べる次の操作がないため、Err に変えず、reject のまま外側へ渡します。fromPromise は reject を Err に変える関数です。これですべてを AppointmentConflict に変えると、障害が競合の通知として扱われ、発見できません。
値がワークフローを流れる
保存障害: 例外の経路
- 予約の確認とイベントの作成まで進み、保存で失敗します。作った
eventをstoreに渡して呼びます。 Promise.resolve().thenは、同期で投げられた例外も reject にします。fromSafePromiseは、reject をErrに変えず例外のまま残します。名前のSafeは、例外の処理を意味しません。- 内側の Promise が reject した
ResultAsyncは、OkにもErrにもならず、awaitした場所で例外になります。そのためandThroughはResultを受け取れず、最後のmapも実行されません。 - 例外のまま外へ出て、外側で 500 を返します。詳細は含めません。保存先の障害を
Errの型に追加しないため、route が分岐する失敗は業務の失敗のままです。
examples/
resolveAppointment(deps)(input.appointmentId) 実行されない
Ok AppointmentState | undefined { kind: "CheckedIn", … }
.andThen(ensureFound(input.appointmentId)) 実行されない
Ok Appointment { kind: "CheckedIn", … }
.andThen(ensureCheckedIn) 実行されない
Ok CheckedIn { kind: "CheckedIn", … }
.map(startExaminationEvent(deps, input.veterinarianId)) 実行されない
Ok ExaminationStarted { kind: "ExaminationStarted", … }
.andThrough(deps.examinationStartedStore.store) 実行されない
Ok ExaminationStarted { kind: "ExaminationStarted", … }
Err AppointmentConflict { kind: "AppointmentConflict", … }
reject AppointmentPersistenceError append-audit の
examinationStartedStore.ts:186-188
ResultAsync. fromSafePromise(
Promise.resolve(). then(() => storeAtomically(event)),
). andThen((result) => result), .map((event) => event.aggregateState); 実行されない
Ok InExamination { kind: "InExamination", … }
examples/
error.kind === "AppointmentConflict"
? "conflict"
: toStartExaminationNoticeCode(error); 実行されない
応答 Response 303 /?notice=conflict
獣医師の画面
操作のお知らせ
予約が
examples/
app. onError((_error, context) => context. text("Internal Server Error", 500)); 実行されない
応答 Response 500 Internal Server Error
右の札は、その行から次の行へ渡される値です。札「Ok」は成功の Result です。札「応答」は呼び出し元へ返す HTTP の応答です。
札「Err」は型に残る業務の失敗で、枠と線は破線です。札「reject」は型に残らず外へ出る例外で、枠と線は実線です。
reject は、S04 で扱った、例外を最後に受け取る場所(app.onError)が、詳細を含まない 500 応答にします。