Communitygithub.com

YepAPI/skills

错误安全——生产环境中返回安全的错误响应、关联ID,不泄露堆栈跟踪信息。

skills 是什么?

该技能专注于在生产环境中安全地处理错误,防止泄露敏感信息。它规定了严格的规则,要求所有错误都返回带有唯一标识符的通用消息,而完整的错误细节仅记录在服务器端,并附带相同的关联ID以供调试。技能支持不同的环境级别(开发和生产),开发环境显示完整堆栈,生产环境则只返回安全消息与错误ID。通过全局错误处理器捕获所有未处理的错误,确保原始错误永远不会到达客户端,并对第三方服务的错误消息进行消毒。此外,它要求使用正确的HTTP状态码(400、401、403、404、500)来指示错误类型。示例代码包括Express错误中间件和Next.js错误边界组件,展示了如何实现这些规则。该技能非常适合任何Web后端或API应用程序,能提升安全性和可调试性。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/error-security

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

错误安全

规则

  • 在生产环境中,永远不要向客户端泄漏堆栈跟踪、数据库错误或文件路径
  • 返回带有唯一错误ID的通用错误消息:{ error: "出了点问题", errorId: "abc123" }
  • 将完整的错误详细信息记录在服务器端,并使用相同的关联ID以便调试
  • 使用不同的错误详细级别:开发环境(完整堆栈跟踪) vs 生产环境(安全消息 + ID)
  • 使用全局错误处理器捕获所有未处理的错误——永远不要让原始错误到达客户端
  • 在将第三方服务的错误消息转发给客户端之前,对其进行消毒
  • 返回正确的HTTP状态码:400(错误输入)、401(未认证)、403(禁止访问)、404(未找到)、500(服务器错误)
// Express 错误处理中间件
import { randomUUID } from "node:crypto";

app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
  const errorId = randomUUID();

  // 在服务器端记录完整细节
  console.error({ errorId, message: err.message, stack: err.stack, url: req.url });

  // 向客户端返回安全响应
  const statusCode = (err as any).statusCode || 500;
  res.status(statusCode).json({
    error: statusCode >= 500 ? "内部服务器错误" : err.message,
    errorId,
    ...(process.env.NODE_ENV === "development" && { stack: err.stack }),
  });
});
// Next.js 错误边界——生产环境安全错误
export default function GlobalError({ error, reset }) {
  return (
    <div>
      <h2>出了点问题</h2>
      <p>错误ID:{error.digest}</p>
      <button onClick={reset}>重试</button>
    </div>
  );
}

避免

  • 直接返回 err.message——数据库错误会泄漏表名、列名和查询结构
  • 在生产响应中包含堆栈跟踪——它们会暴露文件路径、依赖项和内部架构
  • 对所有错误使用通用的500——使用正确的状态码,以便客户端能够适当地处理错误
  • 记录错误时不带关联ID——这会使得生产调试几乎不可能
  • 静默地吞掉错误——始终记录,即使响应是通用的

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Theme switching with CSS variables, system preference detection, next-themes, and persistence.

YepAPI/skills

Threaded comments, @mentions, reactions, moderation queue, optimistic UI, and cursor pagination.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

YepAPI/skills

Upload handling, resize/compress, CDN delivery, blur placeholders, and next/image patterns.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

相关技能