error-handling
OfficialTypeScript、Python、Goにわたる堅牢なエラー処理のパターン。型付きエラー、エラー境界、リトライ、サーキットブレーカー、ユーザー向けエラーメッセージをカバーします。
What this skill does
When applied, it prepends a system prompt before your request is sent — no extra calls and no change to how you are billed beyond the added tokens.
---
name: error-handling
description: TypeScript、Python、Goにわたる堅牢なエラー処理のパターン。型付きエラー、エラー境界、リトライ、サーキットブレーカー、ユーザー向けエラーメッセージをカバーします。
origin: ECC
---
# エラー処理パターン
本番アプリケーション向けの一貫した堅牢なエラー処理パターン。
## アクティベートするタイミング
- 新しいモジュールやサービスのエラー型や例外階層を設計する場合
- 信頼性の低い外部依存関係に対してリトライロジックやサーキットブレーカーを追加する場合
- APIエンドポイントでエラー処理の欠落をレビューする場合
- ユーザー向けエラーメッセージとフィードバックを実装する場合
- カスケード障害やサイレントなエラー飲み込みをデバッグする場合
## コア原則
1. **早く大きく失敗する** — エラーが発生した境界で表面化させる。埋め込まない
2. **文字列メッセージより型付きエラー** — エラーは構造を持つファーストクラスの値
3. **ユーザーメッセージ ≠ 開発者メッセージ** — ユーザーには親しみやすいテキストを表示し、詳細なコンテキストはサーバー側でログに記録する
4. **エラーをサイレントに飲み込まない** — すべての`catch`ブロックは処理、再スロー、またはログのいずれかを行う必要がある
5. **エラーはAPIコントラクトの一部** — クライアントが受け取る可能性があるすべてのエラーコードをドキュメント化する
## TypeScript / JavaScript
### 型付きエラークラス
```typescript
// ドメインのエラー階層を定義する
export class AppError extends Error {
constructor(
message: string,
public readonly code: string,
public readonly statusCode: number = 500,
public readonly details?: unknown,
) {
super(message)
this.name = this.constructor.name
// トランスパイルされたES5 JavaScriptでプロトタイプチェーンを正しく維持する。
// 組み込みのErrorクラスを拡張する際に`instanceof`チェック
// (例: `error instanceof NotFoundError`)が正しく動作するために必要。
Object.setPrototypeOf(this, new.target.prototype)
}
}
export class NotFoundError extends AppError {
constructor(resource: string, id: string) {
super(`${resource} not found: ${id}`, 'NOT_FOUND', 404)
}
}
export class ValidationError extends AppError {
constructor(message: string, details: { field: string; message: string }[]) {
super(message, 'VALIDATION_ERROR', 422, details)
}
}
export class UnauthorizedError extends AppError {
constructor(reason = 'Authentication required') {
super(reason, 'UNAUTHORIZED', 401)
}
}
export class RateLimitError extends AppError {
constructor(public readonly retryAfterMs: number) {
super('Rate limit exceeded', 'RATE_LIMITED', 429)
}
}
```
### Resultパターン(スロー不使用スタイル)
失敗が想定され一般的な操作(パース、外部呼び出し)向け:
```typescript
type Result<T, E = AppErrorUse this skill
Add a "skill" field with the skill’s ID to your chat completion request. It is applied server-side before your prompt is sent — no extra calls.
{
"model": "gpt-4o-mini",
"skill": "imp-f1987695-d31e-409b-bf98-84c13fb6abf5",
"messages": [{ "role": "user", "content": "…" }]
}Install the skill, enable it in your dashboard and (optionally) limit it to specific models. It then applies automatically to every matching request — with no "skill" field to send each time.
Set it up in your dashboardMore skills
Set up and use 1Password CLI for sign-in, desktop integration, and reading or injecting secrets.
Create, view, edit, delete, search, move, or export Apple Notes via the memo CLI on macOS.
List, add, edit, complete, or delete Apple Reminders and reminder lists via remindctl.
Create, search, and manage Bear notes via grizzly CLI.
Monitor blogs and RSS/Atom feeds for updates using the blogwatcher CLI.
BluOS CLI (blu) for discovery, playback, grouping, and volume.
Capture frames or clips from RTSP/ONVIF cameras.
Search, install, update, sync, or publish agent skills with the ClawHub CLI and registry.