error-handling
Handle errors across the three tiers — HTTP/Drizzle layer rejection, service factory validation failure, and component-level try/catch with toast notifications
- License 许可
- Apache-2.0
- 最近更新
- 2026-11-04
- Stars
- ★ 41
- Forks
- 0
01 前言 Front Matter
- name
- error-handling
- description
- Handle errors across the three tiers — HTTP/Drizzle layer rejection, service factory validation failure, and component-level try/catch with toast notifications
03 SKILL.md 正文
Error Handling
This project follows a three-tier error handling pattern, with an additional tier for @repo/api-client usage:
- Implementation layer — rejects on errors (non-ok HTTP response or DB failure)
- Service layer — throws on Zod validation failure
- Component layer — catches errors and displays via toast
No custom error classes. No centralized error reporting. Errors propagate up from Implementation → Service → Component.
Tier 0: api-client + Server Function Response Handling
When using @repo/api-client via server functions (web app), responses are checked after the SDK call:
// server-fns/guides.ts
import { createServerFn } from "@tanstack/react-start";
import { api } from "@repo/api-client";
export const createGuide = createServerFn({ method: "POST" })
.inputValidator((input: { title: string; description?: string }) => input)
.handler(async ({ data }) => {
const response = await api.guides.createGuide({
title: data.title,
description: data.description ?? null,
});
if (response.status !== 201 || !response.data?.guide) {
console.error("Failed to create guide:", response.data);
return null;
}
return response.data.guide;
});
Rules:
- Check
response.statusagainst the expected success code - Check
response.datafor the expected shape - Log the error and return a safe value (
null) or throw - The auto-generated SDK handles transport-level errors (network, 5xx)
Tier 1: Implementation Layer Rejection
Extension (HTTP) — For non-api-client domains
All HTTP implementations use the same error pattern:
if (!response.ok) {
throw new Error(`Failed: ${response.status} ${response.statusText}`);
}
Rules:
- Always include the HTTP status in the error message
- Throw synchronously with
throw new Error(...) - Parse the JSON error body from the API response if available
Web (Drizzle) — For non-api-client domains
Drizzle operations throw on DB failure naturally. They are wrapped by the composition layer:
// Errors from Drizzle propagate up unmodified
const guide = await drizzleCreateGuide(db, input);
Tier 2: Service Factory Validation Failure
Extension (HTTP implementation validation) — For non-api-client domains
Service HTTP implementations use getValidationResult from @repo/data-commons to validate response bodies:
import { getValidationResult } from "@repo/data-commons";
// or import the domain-specific validator from models:
import { validateHttpCreateGuide } from "@/models";
const data = await response.json();
const validationResult = validateHttpCreateGuide(data);
if (!validationResult.success) {
throw new Error(validationResult.error);
}
Rules:
- Import
getValidationResultfrom@repo/data-commonsor use domain-specific validators frommodels/ - Always check
validationResult.success === falsefor type narrowing - Throw
new Error(validationResult.error)— the error string comes fromValidationResult.error
Tier 3: Component-Level Catch and Toast
Every async operation in components/pages follows the same pattern:
try {
await someOperation();
} catch (error: any) {
showToastError("Error", error.message ?? "An error occurred");
}
The toast service exposes three methods:
showToastSuccess(title, message) // green
showToastInfo(title, message) // blue
showToastError(title, message) // red
Rules:
- Always use
catch (error: any)— caught errors areunknownby default - Always provide a fallback message:
error.message ?? "An error occurred" - Use the error message from the service layer — it contains the Zod validation error or the implementation error message
Try/Catch with Finally
When cleanup is needed after an operation (regardless of success/failure), use finally:
try {
await operation();
} catch (error: any) {
showToastError("Error", error.message);
} finally {
setLoading(false);
}
Rules Summary
✅ DO
- Check status + data shape when using
@repo/api-clientin server functions - Throw
new Error(...)on HTTP/Drizzle errors - Use
getValidationResultfrom@repo/data-commonsfor Zod validation in HTTP implementations - Wrap every async component operation in
try { ... } catch (error: any) { showToastError(...) } - Use
finallywhen cleanup is needed regardless of outcome
❌ DON'T
- Don't define custom error classes — use
new Error(message)consistently - Don't catch errors silently — always show feedback via toast
- Don't log errors to console in production paths
- Don't use
catch (error)without the: anytype annotation
04 同仓库更多 Skills
typst
Typst document creation and package development. Use when: (1) Working with .typ files, (2) User mentions typst, typst.toml, or typst-cli, (3) Creating or using Typst packages, (4) Developing document templates, (5) Converting Markdown/LaTeX to Typst
shadcn
Manages shadcn components and projects — adding, searching, fixing, debugging, styling, and composing UI. Provides project context, component docs, and usage examples. Applies when working with shadcn/ui, component registries, presets, --preset codes, or any project with a components.json file. Also triggers for "shadcn init", "create an app with --preset", or "switch to --preset".
handoff
Compact the current conversation into a handoff document for another agent to pick up.
grilling
Grill the user relentlessly about a plan, decision, or idea. Use when the user wants to stress-test their thinking, or uses any 'grill' trigger phrases.
grill-me
A relentless interview to sharpen a plan or design.
dont-waffle
Use this skill when answering questions, explaining concepts, writing technical responses, reviewing code, or helping with development tasks. Communicate in clear, simple, standard English. Keep answers concise and focused, remove unnecessary filler and repetition, explain technical ideas in plain language, and structure information so it is easy to read and understand. When showing code, present it clearly with enough context for the developer to understand what it does and where it belongs.