jpskill.com
📦 その他 コミュニティ

m06-error-handling

プログラムでエラーが起きた際に、Result型やOption型、panicなどの様々な方法を用いて、エラーの種類に応じた適切な対処法を判断し、エラー処理をスムーズに進めるSkill。

📜 元の英語説明(参考)

CRITICAL: Use for error handling. Triggers: Result, Option, Error, ?, unwrap, expect, panic, anyhow, thiserror, when to panic vs return Result, custom error, error propagation, 错误处理, Result 用法, 什么时候用 panic

🇯🇵 日本人クリエイター向け解説

一言でいうと

プログラムでエラーが起きた際に、Result型やOption型、panicなどの様々な方法を用いて、エラーの種類に応じた適切な対処法を判断し、エラー処理をスムーズに進めるSkill。

※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o m06-error-handling.zip https://jpskill.com/download/9265.zip && unzip -o m06-error-handling.zip && rm m06-error-handling.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/9265.zip -OutFile "$d\m06-error-handling.zip"; Expand-Archive "$d\m06-error-handling.zip" -DestinationPath $d -Force; ri "$d\m06-error-handling.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して m06-error-handling.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → m06-error-handling フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-18
取得日時
2026-05-18
同梱ファイル
1

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

エラーハンドリング

レイヤー1: 言語のメカニズム

中核となる問い

この失敗は予期されたものですか、それともバグですか?

エラー処理戦略を選択する前に:

  • これは通常の操作で失敗する可能性がありますか?
  • 誰がこの失敗を処理する必要がありますか?
  • 呼び出し元はどのようなコンテキストを必要としますか?

エラー → 設計に関する問い

パターン 単に言うのではなく 代わりに尋ねる
unwrap がパニック ? を使う」 ここで None/Err が実際に起こりうるか?
? で型が不一致 anyhow を使う」 エラー型は正しく設計されているか?
エラーコンテキストの喪失 .context() を追加する」 呼び出し元は何を知る必要があるか?
エラーバリアントが多すぎる Box<dyn Error> を使う」 エラーの粒度は適切か?

思考を促すプロンプト

エラーを処理する前に:

  1. これはどのような種類の失敗ですか?

    • 予期されるもの → Result<T, E>
    • 通常の不在 → Option<T>
    • バグ/不変条件 → panic!
    • 回復不能 → panic!
  2. 誰がこれを処理しますか?

    • 呼び出し元 → ? で伝播
    • 現在の関数 → match/if-let
    • ユーザー → フレンドリーなエラーメッセージ
    • プログラマー → メッセージ付きで panic
  3. どのようなコンテキストが必要ですか?

    • エラーの種類 → thiserror バリアント
    • 呼び出しチェーン → anyhow::Context
    • デバッグ情報 → anyhow または tracing

トレースアップ ↑

エラー戦略が不明確な場合:

"Result を返すか、Option を返すか?"
    ↑ 尋ねる: 不在/失敗は正常か、それとも例外か?
    ↑ 確認: m09-domain (ドメインは何と言っているか?)
    ↑ 確認: domain-* (エラー処理の要件)
状況 トレース先 質問
unwrap が多すぎる m09-domain データモデルは正しいか?
エラーコンテキストの設計 m13-domain-error どのような回復が必要か?
ライブラリ vs アプリのエラー m11-ecosystem 消費者は誰か?

トレースダウン ↓

設計から実装へ:

"予期される失敗、ライブラリコード"
    ↓ 使う: 型付きエラーには thiserror

"予期される失敗、アプリケーションコード"
    ↓ 使う: 人間工学的なエラーには anyhow

"不在は正常 (find, get, lookup)"
    ↓ 使う: Option<T>

"バグまたは不変条件違反"
    ↓ 使う: panic!, assert!, unreachable!

"コンテキストを伝播する必要がある"
    ↓ 使う: .context("何が起こっていたか")

クイックリファレンス

パターン いつ
Result<T, E> 回復可能なエラー fn read() -> Result<String, io::Error>
Option<T> 不在は正常 fn find() -> Option<&Item>
? エラーを伝播 let data = file.read()?;
unwrap() 開発/テストのみ config.get("key").unwrap()
expect() 不変条件が保持される env.get("HOME").expect("HOME set")
panic! 回復不能 panic!("critical failure")

ライブラリ vs アプリケーション

コンテキスト エラークレート 理由
ライブラリ thiserror 消費者向けの型付きエラー
アプリケーション anyhow 人間工学的なエラー処理
混合 両方 境界では thiserror、内部では anyhow

意思決定フローチャート

失敗は予期されるか?
├─ はい → 不在だけが「失敗」か?
│        ├─ はい → Option<T>
│        └─ いいえ → Result<T, E>
│                 ├─ ライブラリ → thiserror
│                 └─ アプリケーション → anyhow
└─ いいえ → バグか?
        ├─ はい → panic!, assert!
        └─ いいえ → 本当に回復不能か検討する

? を使う → コンテキストが必要か?
├─ はい → .context("メッセージ")
└─ いいえ → 単純な ?

よくあるエラー

エラー 原因 修正
unwrap() パニック 未処理の None/Err ? または match を使う
型の不一致 異なるエラー型 anyhow または From を使う
コンテキストの喪失 コンテキストなしの ? .context() を追加する
cannot use ? Result の戻り値がない Result<(), E> を返す

アンチパターン

アンチパターン なぜ悪いか より良い方法
どこでも .unwrap() 本番環境でパニック .expect("理由") または ?
エラーを黙って無視 バグが隠れる 処理または伝播
予期されるエラーに panic! 悪い UX、回復不能 Result
どこでも Box<dyn Error> 型情報が失われる thiserror

関連スキル

いつ 参照
ドメインエラー戦略 m13-domain-error
クレート境界 m11-ecosystem
型安全なエラー m05-type-driven
メンタルモデル m14-mental-model
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

Error Handling

Layer 1: Language Mechanics

Core Question

Is this failure expected or a bug?

Before choosing error handling strategy:

  • Can this fail in normal operation?
  • Who should handle this failure?
  • What context does the caller need?

Error → Design Question

Pattern Don't Just Say Ask Instead
unwrap panics "Use ?" Is None/Err actually possible here?
Type mismatch on ? "Use anyhow" Are error types designed correctly?
Lost error context "Add .context()" What does the caller need to know?
Too many error variants "Use Box<dyn Error>" Is error granularity right?

Thinking Prompt

Before handling an error:

  1. What kind of failure is this?

    • Expected → Result<T, E>
    • Absence normal → Option<T>
    • Bug/invariant → panic!
    • Unrecoverable → panic!
  2. Who handles this?

    • Caller → propagate with ?
    • Current function → match/if-let
    • User → friendly error message
    • Programmer → panic with message
  3. What context is needed?

    • Type of error → thiserror variants
    • Call chain → anyhow::Context
    • Debug info → anyhow or tracing

Trace Up ↑

When error strategy is unclear:

"Should I return Result or Option?"
    ↑ Ask: Is absence/failure normal or exceptional?
    ↑ Check: m09-domain (what does domain say?)
    ↑ Check: domain-* (error handling requirements)
Situation Trace To Question
Too many unwraps m09-domain Is the data model right?
Error context design m13-domain-error What recovery is needed?
Library vs app errors m11-ecosystem Who are the consumers?

Trace Down ↓

From design to implementation:

"Expected failure, library code"
    ↓ Use: thiserror for typed errors

"Expected failure, application code"
    ↓ Use: anyhow for ergonomic errors

"Absence is normal (find, get, lookup)"
    ↓ Use: Option<T>

"Bug or invariant violation"
    ↓ Use: panic!, assert!, unreachable!

"Need to propagate with context"
    ↓ Use: .context("what was happening")

Quick Reference

Pattern When Example
Result<T, E> Recoverable error fn read() -> Result<String, io::Error>
Option<T> Absence is normal fn find() -> Option<&Item>
? Propagate error let data = file.read()?;
unwrap() Dev/test only config.get("key").unwrap()
expect() Invariant holds env.get("HOME").expect("HOME set")
panic! Unrecoverable panic!("critical failure")

Library vs Application

Context Error Crate Why
Library thiserror Typed errors for consumers
Application anyhow Ergonomic error handling
Mixed Both thiserror at boundaries, anyhow internally

Decision Flowchart

Is failure expected?
├─ Yes → Is absence the only "failure"?
│        ├─ Yes → Option<T>
│        └─ No → Result<T, E>
│                 ├─ Library → thiserror
│                 └─ Application → anyhow
└─ No → Is it a bug?
        ├─ Yes → panic!, assert!
        └─ No → Consider if really unrecoverable

Use ? → Need context?
├─ Yes → .context("message")
└─ No → Plain ?

Common Errors

Error Cause Fix
unwrap() panic Unhandled None/Err Use ? or match
Type mismatch Different error types Use anyhow or From
Lost context ? without context Add .context()
cannot use ? Missing Result return Return Result<(), E>

Anti-Patterns

Anti-Pattern Why Bad Better
.unwrap() everywhere Panics in production .expect("reason") or ?
Ignore errors silently Bugs hidden Handle or propagate
panic! for expected errors Bad UX, no recovery Result
Box<dyn Error> everywhere Lost type info thiserror

Related Skills

When See
Domain error strategy m13-domain-error
Crate boundaries m11-ecosystem
Type-safe errors m05-type-driven
Mental models m14-mental-model