このページでは、同じ5つの境界で、失敗の依頼がどこまで進むかを確認します。業務上の失敗の境界で返す失敗と、副作用の境界で Err に戻る競合は、Err として応答まで渡されます。SQLite の障害は Err にならず、例外の境界で 500 の応答になります。
失敗の依頼が5つの境界を進む範囲
値は説明用の例です。項目は一部だけを記載しています。
場面1「業務上の失敗」
- 予約がすでに
InExaminationだったとします。この失敗は読み取りの時点で判明し、利用者には「現在の予約状態ではこの操作を実行できません。画面を更新して状態を確認してください。」と通知します。入力と検証は前のページの成功と同じく通過し、ensureCheckedInでkindが異なるため、InvalidAppointmentStateのErrを返します。以降のステップは実行されません。 - route の
matchはinvalidStateを呼びます。応答は、error=invalid-stateが付いた詳細ページへの 303 です。
場面2「競合」
- 別の依頼が先に診察を開始していたとします。読み取りの時点では
CheckedInだったため、この失敗は保存の時点で判明し、利用者には「予約を更新できませんでした。最新の状態を確認してください。」と通知します。statusがCheckedInの行が無いため、更新は0行で、changes !== 1になります。storeはAppointmentConflictをthrowし、transaction は取り消されます。 throwされた値はfromPromiseの変換関数に渡されます。AppointmentConflictSchemaに一致するため、ErrのAppointmentConflictに戻します。- route の
matchがAppointmentConflictを受け取り、error=appointment-conflictが付いた詳細ページへの 303 を返します。例外の境界は通りません。
場面3「SQLite 障害」
- 前のページの成功と同じくイベントまで生成し、
storeが transaction の中で予約の行を更新します。SQLite の書き込みが失敗してrun()が例外を投げ、transaction は取り消されます。 - 例外は
fromPromiseの変換関数に渡されます。競合ではないため、変換せずにthrow causeで投げ直します。 - 投げ直された例外は
Errにならず、Hono のapp.onErrorに渡されます。app.onErrorはHTTPException(Hono で定義された HTTP 応答を表す例外)かどうかを判定し、該当しないため、詳細を含まない 500 のResponseを生成します。 - route の
Errの分岐は使われません。SQLite の障害はResultに含まれません。例外の境界が作った 500 がそのまま応答になります。
- 1 入力の境界
- 2 業務上の失敗の境界
- 3 出力イベントの境界
- 4 副作用の境界
- 5 例外の境界
- 応答
HTTP の入力を検証し、ユースケースの入力へ変換します。
examples/final/src/adaptor/primary/web/routes/appointmentRoutes.ts:164, 170, 497 context.req.parseBody(),
…
const parsed = schema.safeParse(body);
…
.run({ actorUserId: actor.value.user.userId, appointmentId: appointmentId.value, veterinarianId }) UseCaseInput {
actorUserId: "u-1",
appointmentId: "a-1",
veterinarianId: "v-1",
} 予約なしや状態不正などの業務上の失敗を、型付きの Err として返します。
examples/final/src/useCase/startExaminationUseCase.ts:76, 87-91 const run =
…
.andThen(ensureUserFound(input.actorUserId))
.andThen(ensureCanStartExamination(input.veterinarianId))
.andThen(() => appointmentResolver.resolveById(input.appointmentId))
.andThen(ensureAppointmentFound(input.appointmentId))
.andThen(ensureCheckedIn) examples/final/src/useCase/errors.ts:41, 44-47 export const ensureCheckedIn = (
…
appointment.kind === "CheckedIn"
? ok(appointment)
: err({
kind: "InvalidAppointmentState",
… CheckedIn { kind: "CheckedIn", appointmentId: "a-1", … } InvalidAppointmentState {
kind: "InvalidAppointmentState",
appointmentId: "a-1",
expectedKind: "CheckedIn",
actualKind: "InExamination",
} 業務の検証に成功した経路だけが、ExaminationStarted の生成へ進みます。
examples/final/src/domain/appointment/appointment.ts:141, 146, 149-151 const startExamination =
…
): ExaminationStarted => {
kind: "InExamination",
veterinarianId,
examinationStartedAt: context.occurredAt, ExaminationStarted {
kind: "ExaminationStarted",
eventId: "e-1",
aggregateId: "a-1",
aggregateState: { kind: "InExamination", … },
eventName: "appointment.examination-started",
occurredAt: "2026-08-30T06:30:00.000Z",
} 予約の状態と監査記録を、1つの transaction で保存します。
examples/final/src/adaptor/secondary/sqlite/store/appointmentEventStore.ts:145, 148, 173-174, 178 store: (...events: readonly AppointmentProjectionEvent[]) =>
db.transaction((tx) => {
…
case "ExaminationStarted":
return tx.update(appointmentsTable)
…
eq(appointmentsTable.status, "CheckedIn"), examples/final/src/adaptor/secondary/sqlite/store/appointmentEventStore.ts:204, 209-210, 216 if (changes !== 1) {
…
}
tx.insert(domainEventsTable)
…
.run(); examples/final/src/adaptor/secondary/sqlite/store/appointmentEventStore.ts:178, 204-209 eq(appointmentsTable.status, "CheckedIn"),
…
if (changes !== 1) {
throw {
kind: "AppointmentConflict",
appointmentId: event.aggregateId,
} as const;
} examples/final/src/adaptor/secondary/sqlite/store/appointmentEventStore.ts:139, 146, 220-222 const AppointmentConflictSchema = z.object({
…
ResultAsync.fromPromise<void, AppointmentStoreError>(
…
(cause): AppointmentStoreError => {
const conflict = AppointmentConflictSchema.safeParse(cause);
if (conflict.success) return conflict.data; examples/final/src/adaptor/secondary/sqlite/store/appointmentEventStore.ts:145, 148, 173-174, 180 store: (...events: readonly AppointmentProjectionEvent[]) =>
db.transaction((tx) => {
…
case "ExaminationStarted":
return tx.update(appointmentsTable)
…
.run().changes; examples/final/src/adaptor/secondary/sqlite/store/appointmentEventStore.ts:146, 220-223 ResultAsync.fromPromise<void, AppointmentStoreError>(
…
(cause): AppointmentStoreError => {
const conflict = AppointmentConflictSchema.safeParse(cause);
if (conflict.success) return conflict.data;
throw cause; unknown throw cause; AppointmentConflict { kind: "AppointmentConflict", appointmentId: "a-1" } 業務の Result に入らない障害を、共通の境界が受け取って 500 を返します。
examples/final/src/app.ts:532-536 app.onError((error) =>
error instanceof HTTPException
? error.getResponse()
: new Response("Internal Server Error", { status: 500 }),
); Response new Response("Internal Server Error", { status: 500 }) route が結果を HTTP の応答に変換します。
examples/final/src/adaptor/primary/web/routes/appointmentRoutes.ts:496-501, 506-509 return dependencies.startExamination
.run({ actorUserId: actor.value.user.userId, appointmentId: appointmentId.value, veterinarianId })
.match(
() => context.redirect(detailUrl(appointmentId.value), 303),
(error) => {
switch (error.kind) {
…
case "InvalidAppointmentState":
return invalidState(context, appointmentId.value);
case "AppointmentConflict":
return context.redirect(`${detailUrl(appointmentId.value)}?error=appointment-conflict`, 303); Response context.redirect("/appointments/a-1", 303) Response context.redirect("/appointments/a-1?error=invalid-state", 303) Response context.redirect("/appointments/a-1?error=appointment-conflict", 303) Response 例外の境界が作成 new Response("Internal Server Error", { status: 500 })