api-error-handling
БесплатноБез исполняемых скриптовНе проверенApply when designing error responses, implementing error handlers, and ensuring consistent error format across APIs.
от diegosouzapw★ 68 звёзд на GitHubОб этом скилле
When to Use
Apply when designing error responses, implementing error handlers, and ensuring consistent error format across APIs.
Patterns
Pattern 1: Standard Error Response Format
// Source: https://www.rfc-editor.org/rfc/rfc9457 (Problem Details)
interface ApiError {
error: {
code: string; // Machine-readable code
message: string; // Human-readable message
details?: ErrorDetail[];// Field-level errors
requestId?: string; // For debugging
};
}
interface ErrorDetail {
field: string;
message: string;
code?: string;
}
// Example response
{
"error": {
"code": "VALIDATION_ERROR",
"message": "Request validation failed",
"details": [
{ "field": "email", "message": "Invalid email format", "code": "INVALID_FORMAT" },
{ "field": "age", "message": "Must be positive", "code": "INVALID_RANGE" }
],
"requestId": "req_abc123"
}
}
Pattern 2: Error Class Hierarchy
// Source: Best practice pattern
class AppError extends Error {
constructor(
public code: string,
message: string,
public statusCode: number,
public details?: ErrorDetail[]
) {
super(message);
this.name = 'AppError';
}
}
class ValidationError extends AppError {
constructor(details: ErrorDetail[]) {
super('VALIDATION_ERROR', 'Validation failed', 400, details);
}
}
class NotFoundError extends AppError {
constructor(resource: string) {
super('NOT_FOUND', `${resource} not found`, 404);
}
}
class UnauthorizedError extends AppError {
constructor() {
super('UNAUTHORIZED', 'Authentication required', 401);
}
}
Pattern 3: Global Error Handler (Express/Next.js)
// Source: Best practice pattern
function errorHandler(
err: Error,
req: Request,
res: Response,
next: NextFunction
) {
// Log for debugging
console.error('Error:', {
message: err.message,
stack: err.stack,
requestId: req.headers['x-request-id'],
});
if (err instanceof AppError) {
return res.status(err.statusCode).json({
error: {
code: err.code,
message: err.message,
details: err.details,
requestId: req.headers['x-request-id'],
},
});
}
// Unknown error - don't leak details
return res.status(500).json({
error: {
code: 'INTERNAL_ERROR',
message: 'An unexpected error occurred',
requestId: req.headers['x-request-id'],
},
});
}
Pattern 4: Frontend Error Handling
// Source: Best practice pattern
async function apiCall<T>(url: string, options?: RequestInit): Promise<T> {
const response = await fetch(url, options);
if (!response.ok) {
const error = await response.json();
throw new ApiError(error.error.code, error.error.message, error.error.details);
}
return response.json();
}
// Usage with error handling
try {
const user = await apiCall<User>('/api/users/123');
} catch (error) {
if (error instanceof ApiError) {
if (error.code === 'NOT_FOUND') {
showNotification('User not found');
} else if (error.code === 'VALIDATION_ERROR') {
setFormErrors(error.details);
}
}
}
Pattern 5: Error Code Constants
// Source: Best practice pattern
export const ErrorCodes = {
VALIDATION_ERROR: 'VALIDATION_ERROR',
NOT_FOUND: 'NOT_FOUND',
UNAUTHORIZED: 'UNAUTHORIZED',
FORBIDDEN: 'FORBIDDEN',
CONFLICT: 'CONFLICT',
RATE_LIMITED: 'RATE_LIMITED',
INTERNAL_ERROR: 'INTERNAL_ERROR',
} as const;
type ErrorCode = typeof ErrorCodes[keyof typeof ErrorCodes];
Anti-Patterns
- Exposing stack traces - Never in production
- Generic "Error occurred" - Provide actionable messages
- 200 for errors - Use appropriate HTTP status codes
- Inconsistent format - Same structure for all errors
Verification Checklist
- All errors have code + message
- Status codes match error type
- Validation errors include field details
- Stack traces hidden in production
- Request ID for debugging correlation
Установить api-error-handling в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
FAQ
Что делает скилл api-error-handling?
Apply when designing error responses, implementing error handlers, and ensuring consistent error format across APIs.
Как установить скилл api-error-handling?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл api-error-handling запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
mcp-builder
Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when…
от Anthropicclaude-api
Reference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming, tool use, MCP, agents, caching, token counting, model migration. TRIGGER…
от Anthropicalgorithmic-art
Creating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code…
от Anthropicmcp-integration
This skill should be used when the user asks to "add MCP server", "integrate MCP", "configure MCP in plugin", "use .mcp.json", "set up Model Context…
от Anthropic