/kenji:backend-error-handlingfrom kenji skills
Implements layered error handling for full-stack apps: one action-result shape, error boundaries, and toasts. Use when adding error handling to new features or standardizing error responses.
Ships in kenji skills. 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.
Read when SKILL.md points to them.
Degree of freedom: MIXED. Which layer and message [HIGH freedom];
existing-type/boundary probes [LOW freedom — run exactly].
| Skill | Owns |
|---|---|
| backend-error-handling (this) | Implement error boundaries, toasts, and one API error shape |
plan-error-handling | Plan-only audit of silent failures and observability gaps |
debug-sentry-monitor | Operate Sentry: triage and fix issues, cut noise, monitor after deploy |
ActionResult / ApiErrorObserve:
createUserthrows PrismaP2002; UI shows a blank catch;ActionResultalready lives intypes/errors.ts. Interpret: known conflict is unmapped; no field/toast path. Classify: reuseActionResult+CONFLICT; toast the message; do not add a second error type. Verify: duplicate email returns{ success: false, error: { code: 'CONFLICT' } }; form alert shown.
ActionResult / ApiError / error.tsx before adding a typeINTERNAL_ERROR is generic; PII is not in the client messageplan-error-handling; live Sentry triage → debug-sentry-monitorLayered error handling for full-stack apps: one action-result shape, boundaries, and toasts.
Before adding ANY error handling, verify:
rg "type.*Error|interface.*Error" --type ts
rg "ActionResult|ApiError" --type ts
ls -la app/error.tsx app/global-error.tsx
rg "ErrorBoundary" --type tsx
rg "formatError|handleError|reportError" --type ts
ls -la src/lib/errors* src/lib/error* 2>/dev/null # @/lib/errors
rg "success: false|error:" src/features/*/server/ --type ts | head -10
Why: Inconsistent error handling confuses users and complicates debugging. Always follow established patterns.
ActionResult or a typed API error, never as a raw throw to the UI.Layer diagram: references/server-patterns.md §Error handling layers.
AppError { code, message, details? }; code is machine-readable, message is user-safe.ActionResult<T> is a discriminated union on success; never return { data, error } both-optional.VALIDATION_ERROR, NOT_FOUND, UNAUTHORIZED, FORBIDDEN, CONFLICT, RATE_LIMITED, INTERNAL_ERROR.// types/errors.ts
interface AppError { code: string; message: string; details?: unknown }
type ActionResult<T> =
| { success: true; data: T }
| { success: false; error: AppError }
Full ErrorCode constant: references/server-patterns.md §Standard error types.
safeParse the FormData; return VALIDATION_ERROR with flatten().fieldErrors as details.UNAUTHORIZED with a "please sign in" message.revalidatePath, return { success: true, data }.P2002 → CONFLICT with a specific message).INTERNAL_ERROR message, never the stack.} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === 'P2002') {
return { success: false, error: { code: 'CONFLICT', message: 'A user with this email already exists' } }
}
console.error('createUser error:', error)
return { success: false, error: { code: 'INTERNAL_ERROR', message: 'Something went wrong. Please try again.' } }
}
Full createUser action: references/server-patterns.md §Server Action error handling.
useActionState(createUser, null) for state; useFormStatus in a child SubmitButton for pending.state.error.details; render inline with aria-invalid and aria-describedby.role="alert" banner above the fields.useOptimistic for optimistic UI updates.Full UserForm component: references/ui-patterns.md §Form error display.
app/error.tsx per page segment: 'use client', receives { error, reset }, logs in useEffect, shows a "Try again" button.app/global-error.tsx for the root: must render its own <html> and <body>.ActionResult or TanStack Query.// app/error.tsx
'use client'
export default function Error({ error, reset }: { error: Error & { digest?: string }; reset: () => void }) {
useEffect(() => { console.error('Page error:', error) }, [error])
return (
<div className="flex flex-col items-center justify-center min-h-[400px]">
<h2 className="text-xl font-semibold mb-4">Something went wrong</h2>
<button onClick={reset} className="px-4 py-2 bg-primary text-primary-foreground rounded-lg">Try again</button>
</div>
)
}
Both boundaries in full: references/ui-patterns.md §React error boundaries.
{ error: { code, message, details? } } body shape for every non-2xx response.400 for VALIDATION_ERROR and INVALID_JSON (SyntaxError from request.json()), 201 on create, 500 generic.Full POST /api/products handler: references/server-patterns.md §API route error handling.
queryFn/mutationFn, throw new Error(body.error?.message ?? fallback) when !res.ok so the UI gets the server message.retry: no retry on 401/403; up to 3 attempts otherwise.onSuccess invalidates the query key and toast.success; onError → toast.error(error.message).useProducts and useCreateProduct hooks: references/ui-patterns.md §TanStack Query error handling.
ErrorState({ title, message, onRetry? }): icon, heading, message, optional retry button.error + refetch) instead of ad-hoc red text per list.const { data, error, isLoading, refetch } = useProducts()
if (error) return <ErrorState title="Failed to load products" message={error.message} onRetry={() => refetch()} />
Full ErrorState component: references/ui-patterns.md §Error state UI components.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
error-handling, error-boundaries, error-toasts, api-errors, fullstack
npx claudepluginhub kensaurus/skills --plugin kenji162 skills in this plugin
/audit-accessibilityWCAG 2.2 AA audit via playwright-cli: axe-core, keyboard, contrast, target size, focus, ARIA. Use when "audit accessibility", "check a11y", "WCAG audit", "check keyboard nav", or "test screen reader".
/audit-agent-speedMeasure and fix a slow coding-agent setup: status line, hooks, instruction bloat, effort, worktree pile-up, antivirus and indexer scans. Use when "Claude is slow" or "CPU is pinned". App speed → audit-performance.
/audit-analyticsRead-only audit of product-analytics events (PostHog, Amplitude, Mixpanel, GA4): taxonomy, funnels, consent-gated firing, dead or duplicate events. Use when "audit our analytics", "are we tracking the right events", or "funnel instrumentation".
Other plugins' skills for the same job