/claude-code-superpowers:error-handlingfrom claude-code-superpowers
Teaches Claude Code's error philosophy: typed errors for expected failures, framework catches and formats them for the LLM. Use when writing tools, utilities, or services that can fail.
Ships in claude-code-superpowers. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Claude Code has two error categories. Session errors are unrecoverable failures that crash the process or abort the current session (network configuration issues, authentication failures, corrupted state). Tool errors are expected failures that should be shown to the LLM so it can reason about them, retry, or explain the situation to the user.
The fundamental rule: tool.call() throws typed errors for expected failures. The framework catches them, calls formatError(error) from src/utils/toolErrors.ts, and delivers the formatted string to the LLM as a tool_result block with is_error: true. Only truly unrecoverable infrastructure failures should propagate past the framework as unhandled exceptions.
The LLM is in a conversation loop. If a tool throws a raw, unformatted exception, the framework must decide what to show the model — a stack trace is not useful. By throwing typed errors (ShellError, AbortError, Error), tools hand the framework enough structure to produce a clean, readable message. For example, a file-not-found error thrown from FileReadTool lets the LLM suggest the correct path or ask the user to check.
This also means the user sees a coherent explanation rather than an error box. The LLM translates the technical error into natural language in its next response.
Custom error types (ShellError, AbortError) are used at system boundaries — they carry structured context (exit code, stderr, interrupted flag) that formatError uses to build the right message.
call(). Catch specific error types first, then rethrow or let them propagate.throw new Error(message) for expected, non-shell failures. Include enough context for the LLM to act: what was attempted, what failed, what the user could check.isENOENT() from utils/errors.js to detect it and throw new Error("File not found: /path. Does it exist?").new ShellError(stdout, stderr, code, interrupted) — formatError assembles exit code and stderr automatically.new AbortError() — formatError maps it to a clean interruption message.// Source: src/tools/BashTool/BashTool.tsx
if (result.preSpawnError) {
throw new Error(result.preSpawnError) // pre-spawn failures → plain Error
}
if (interpretationResult.isError && !isInterrupt) {
throw new ShellError('', outputWithSbFailures, result.code, result.interrupted)
// ShellError carries structured context; formatError builds the LLM message
}
// Source: src/utils/toolErrors.ts — framework calls this after catching
export function formatError(error: unknown): string {
if (error instanceof AbortError) {
return error.message || INTERRUPT_MESSAGE_FOR_TOOL_USE
}
if (!(error instanceof Error)) return String(error)
const parts = getErrorParts(error)
return parts.filter(Boolean).join('\n').trim() || 'Command failed with no output'
// truncated to 10 000 chars if longer
}
export function getErrorParts(error: Error): string[] {
if (error instanceof ShellError) {
return [`Exit code ${error.code}`, error.interrupted ? '...' : '', error.stderr, error.stdout]
}
return [error.message]
}
// Source: src/tools/FileReadTool/FileReadTool.ts
import { isENOENT } from '../../utils/errors.js' // NOT utils/file.js
} catch (error) {
if (isENOENT(error)) {
throw new Error(`File not found: ${file_path}. Does it exist?`)
}
throw error // re-throw unknown errors — framework handles them
}
The abort-error branch is non-obvious: when the user presses Ctrl+C, an AbortError propagates through all in-flight async calls. Tools must let it propagate (or throw a new AbortError) so formatError returns a clean "interrupted" message.
Before — tool that returns a wrong shape instead of throwing:
async call(args, context, canUseTool) {
try {
const response = await fetch(args.url)
if (!response.ok) {
return { type: 'error', error: `HTTP ${response.status}` } // Wrong shape: ToolResult has no error variant
}
return { type: 'success', data: await response.text() } // Wrong shape: no 'type' field on ToolResult
} catch (err) {
return { type: 'error', error: String(err) } // Wrong: swallows AbortError, wrong shape
}
}
After — tool that throws typed errors:
async call(args, context, canUseTool) {
try {
const response = await fetch(args.url, {
signal: context.abortController.signal, // WHY: lets AbortError propagate on Ctrl+C
})
if (!response.ok) {
// WHY: throw Error so formatError surfaces a readable message to the LLM
throw new Error(`HTTP ${response.status} ${response.statusText} fetching ${args.url}`)
}
return { data: await response.text() } // WHY: ToolResult<T> is just { data: T }
} catch (err) {
if (err instanceof AbortError) throw err // WHY: let framework handle cancellation cleanly
// WHY: network errors (DNS failure, timeout) re-thrown as plain Error with context
throw new Error(`Network error fetching ${args.url}: ${err instanceof Error ? err.message : String(err)}`)
}
}
call() returns { type: 'error', error: ... } — there is no error variant on ToolResult<T>; throw insteadisENOENT imported from utils/file.js — the correct source is utils/errors.jsAbortError is caught and swallowed — Ctrl+C produces an error dialog instead of a clean cancellationtool-definition — the ToolResult type and where errors appear in the return valuepermission-system — handling permission denial as a specific error caseasync-concurrency — AbortError handling when tools are cancelledWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
Generated from the plugin's indexed repository (techymt/claude-code-superpowers) — no native marketplace selected
npx claudepluginhub p/techymt-claude-code-superpowers16 skills in this plugin
/async-concurrencyTeaches Claude Code's concurrency model: isConcurrencySafe() for parallel tool execution, AbortController propagation for cancellation, Promise.all() for parallel initialization, and the StreamingToolExecutor for concurrent progress streams. Use this when writing tools that could benefit from parallel execution, need to respect cancellation, or must stream partial results. Read-only tools that don't declare concurrency safety are needlessly serialized.
/build-tool-factoryTeaches how to create a new Claude Code tool using the buildTool() factory from src/Tool.ts.
Use this whenever adding a new capability to Claude Code — buildTool is the only correct entry
point. It fills in safe defaults for isConcurrencySafe, isReadOnly, isDestructive,
checkPermissions, and userFacingName so you only implement what your tool actually needs.
/creating-skillsOther plugins' skills for the same job