Skip to content

feat: 例外クラスに構造化コンテキストとエラーコードを追加#125

Merged
KinjiKawaguchi merged 5 commits intodevelopfrom
worktree-improve-exceptions
Mar 25, 2026
Merged

feat: 例外クラスに構造化コンテキストとエラーコードを追加#125
KinjiKawaguchi merged 5 commits intodevelopfrom
worktree-improve-exceptions

Conversation

@KinjiKawaguchi
Copy link
Copy Markdown
Member

@KinjiKawaguchi KinjiKawaguchi commented Mar 24, 2026

Why

現状の例外はメッセージ文字列にIDや値が埋め込まれており、プログラムからの判別が困難だった。
呼び出し側が catch 後にエラー内容に応じた処理を行うには文字列パースが必要で、APIレスポンス構築やログ構造化に不便だった。

What

DomainException / ApplicationException サブクラスに以下のプロパティを追加:

プロパティ 用途
code プログラムからの判別用エラーコード(例: MEMBER_NOT_FOUND
category 大分類(VALIDATION, NOT_FOUND, CONFLICT, INVARIANT_VIOLATION, PRECONDITION_FAILED
context 関連IDや値を型付きオブジェクトとして取得可能
hint 利用者への解決ガイダンス
cause ES2022 Error.cause によるエラーチェーン対応

使用例

catch (e) {
  if (e instanceof MemberNotFoundException) {
    console.log(e.code);               // "MEMBER_NOT_FOUND"
    console.log(e.category);           // "NOT_FOUND"
    console.log(e.context.memberId);   // "abc-123"
    console.log(e.hint);               // "メンバーIDが正しいか、または削除されていないか確認してください"
  }
}

既存互換性

  • message プロパティは従来通り人間向けメッセージを返す
  • instanceof による分岐は従来通り動作
  • 新プロパティはオプショナルに使えるため、既存の catch コードは破壊されない

備考

  • RegisterMemberToExhibit / RemoveMemberFromExhibitEventNotFoundExceptionexhibitId が渡されている既存の意味的不整合があるが、スコープ外として今回は未修正

Test plan

  • npm run typecheck パス
  • npm test 全127テストパス
  • npm run build 成功

🤖 Generated with Claude Code


Open with Devin

KinjiKawaguchi and others added 3 commits March 24, 2026 20:21
全例外クラスを改善し、プログラムから扱いやすい構造化された情報を提供する。
- code: プログラムからの判別用エラーコード
- category: VALIDATION / NOT_FOUND / CONFLICT / INVARIANT_VIOLATION 等の大分類
- context: 関連IDや値をプロパティとして取得可能に
- hint: 利用者への解決ガイダンス
- cause: ES2022 Error.cause によるエラーチェーン対応

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
readonly 初期化子はリテラル型として推論されるため、明示的な as const は冗長。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@KinjiKawaguchi KinjiKawaguchi marked this pull request as ready for review March 25, 2026 00:46
Copilot AI review requested due to automatic review settings March 25, 2026 00:46
devin-ai-integration[bot]

This comment was marked as resolved.

This comment was marked as resolved.

EventNotFoundException に exhibitId が渡されていた意味的不整合を解消。
構造化コンテキストの信頼性を確保するため、exhibitId起点でイベントが
見つからないケース専用の例外クラスを追加。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
devin-ai-integration[bot]

This comment was marked as resolved.

findByExhibitId でイベントが見つからない場合の例外を統一。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 new potential issue.

Open in Devin Review

Comment on lines +62 to 72
export class ExhibitNotFoundFromExhibitIdException extends ApplicationException {
readonly code = "EXHIBIT_NOT_FOUND_BY_EXHIBIT_ID";
readonly category = "NOT_FOUND";
readonly context: { exhibitId: string };
readonly hint = "展示IDが正しいか、またはイベントに登録済みか確認してください";

constructor(exhibitId: string, options?: { cause?: unknown }) {
super(`指定された展示IDで展示が見つかりません: ${exhibitId}`, options);
this.context = { exhibitId };
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Info: ExhibitNotFoundFromExhibitIdException is now dead code

The PR adds EventNotFoundByExhibitIdException and migrates all three use cases (GetMembersByExhibit, RegisterMemberToExhibit, RemoveMemberFromExhibit) to use it. However, the old ExhibitNotFoundFromExhibitIdException is still defined at src/application/exceptions/ApplicationExceptions.ts:62 and exported, but has zero remaining imports or usages in the codebase. It should be removed to avoid confusion with the similarly-named domain-level ExhibitNotFoundException.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

@KinjiKawaguchi KinjiKawaguchi merged commit 942d00b into develop Mar 25, 2026
4 checks passed
@KinjiKawaguchi KinjiKawaguchi deleted the worktree-improve-exceptions branch March 25, 2026 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants