{"title":"错误与错误处理","slug":"errors","category":"api","summary":"HTTP 状态码、错误响应格式以及从常见错误中恢复的方法。","audience":["human","llm"],"tags":["api","errors","troubleshooting","http"],"difficulty":"beginner","updated":"2026-06-27","word_count":239,"read_minutes":1,"llm_context":"Error format: { statusCode, error, message, docs? }\nCommon errors: 401 (auth), 404 (wrong path), 429 (rate limit), 500 (server)\n401 → check Mind Key/JWT, see /docs/getting-started/authentication\n404 → wrong path, GET /endpoints for valid list, do NOT guess paths\n429 → rate limited (?key= is 60/min), use Bearer header instead\n500 → server error, retry with backoff, check /health\ndocs field in error response links to relevant documentation.\n","lang":"zh","translated":true,"requested_lang":"zh","content_markdown":"\n# 错误与错误处理\n\nSynapse 使用标准 HTTP 状态码，并提供一致的错误响应格式。本页说明如何解读错误并从中恢复。\n\n## 错误响应格式\n\n所有错误都返回如下结构的 JSON：\n\n```json\n{\n  \"statusCode\": 401,\n  \"error\": \"Unauthorized\",\n  \"message\": \"Mind Key fehlt oder ungültig.\",\n  \"docs\": \"https://synapse.schaefer.zone/docs/getting-started/authentication\"\n}\n```\n\n| 字段 | 说明 |\n|-------|-------------|\n| `statusCode` | HTTP 状态码 |\n| `error` | HTTP 状态名称 |\n| `message` | 人类可读的错误描述 |\n| `docs` | 指向相关文档的 URL（如适用） |\n\n## HTTP 状态码\n\n### 200 OK\n\n成功。请求被正确处理。\n\n### 201 Created\n\n成功。创建了新资源（例如 `POST /memory`）。\n\n### 204 No Content\n\n成功。无响应体返回（例如 `DELETE /memory/:id`）。\n\n### 400 Bad Request\n\n请求格式不正确。常见原因：\n\n- 缺少必填的 JSON 字段\n- JSON 语法无效\n- 枚举值无效（例如分类不正确）\n\n```json\n{\n  \"statusCode\": 400,\n  \"error\": \"Bad Request\",\n  \"message\": \"category must be one of: identity, preference, fact, project, skill, mistake, context, note, credentials\"\n}\n```\n\n**修复：** 对照 API 文档检查请求体。确保所有必填字段均存在且值合法。\n\n### 401 Unauthorized\n\n认证失败。常见原因：\n\n- 缺少 `Authorization` 头\n- Mind Key 或 JWT 无效\n- 在需要 JWT 的地方用了 Mind Key（或反之）\n\n```json\n{\n  \"statusCode\": 401,\n  \"error\": \"Unauthorized\",\n  \"message\": \"Mind Key fehlt oder ungültig.\",\n  \"docs\": \"https://synapse.schaefer.zone/docs/getting-started/authentication\"\n}\n```\n\n**修复：** 校验你的令牌。参见 [Authentication](/docs/getting-started/authentication)。\n\n### 403 Forbidden\n\n你已认证但无权执行此操作。常见原因：\n\n- 尝试删除他人的 Mind\n- 尝试用 Mind Key 验证记忆（需要 JWT）\n- Mind 已被禁用\n\n**修复：** 检查你为此端点使用的令牌类型是否正确。\n\n### 404 Not Found\n\n请求的路径或资源不存在。\n\n> [!CRITICAL]\n> **不要猜测端点路径。** 只有 `GET /endpoints` 中列出的路径才存在。如果收到 404，说明你用了错误的路径。\n\n```json\n{\n  \"statusCode\": 404,\n  \"error\": \"Not Found\",\n  \"message\": \"Route GET /memory/list not found\",\n  \"docs\": \"https://synapse.schaefer.zone/docs/api/errors\"\n}\n```\n\n**修复：** 调用 `GET /endpoints` 查看有效端点列表。将你的 URL 与列表逐字符对比。\n\n### 409 Conflict\n\n请求与现有状态冲突。常见原因：\n\n- 尝试用已存在的邮箱注册\n- Webhook URL 重复\n\n**修复：** 使用不同的值，或使用 `PUT` 更新已有资源。\n\n### 429 Too Many Requests\n\n你触发了限速。仅适用于 `?key=` 查询参数认证（60 次/分钟）。\n\n```json\n{\n  \"statusCode\": 429,\n  \"error\": \"Too Many Requests\",\n  \"message\": \"Rate limit exceeded. Use Authorization header for unlimited access.\"\n}\n```\n\n响应头：\n\n```\nX-RateLimit-Limit: 60\nX-RateLimit-Remaining: 0\nRetry-After: 42\n```\n\n**修复：** 改用 `Authorization: Bearer` 头（无限制），或等待 `Retry-After` 秒。\n\n### 500 Internal Server Error\n\n服务器错误。这种情况不应发生 — 如果发生，则是 Bug。\n\n**修复：**\n\n1. 使用指数退避重试（1 秒、2 秒、4 秒、8 秒）\n2. 检查 `GET /health` 查看服务器是否在线\n3. 如果持续出现，请上报该错误\n\n### 503 Service Unavailable\n\n服务器临时不可用（例如部署、数据库迁移期间）。\n\n**修复：** 等待并重试。检查 `GET /health`。\n\n## 恢复模式\n\n### 指数退避重试\n\n```python\nimport time\nimport requests\n\ndef call_with_retry(url, max_retries=3):\n    for attempt in range(max_retries):\n        try:\n            r = requests.get(url, headers={\"Authorization\": f\"Bearer {KEY}\"})\n            if r.status_code == 429:\n                wait = int(r.headers.get(\"Retry-After\", 60))\n                time.sleep(wait)\n                continue\n            if r.status_code >= 500:\n                time.sleep(2 ** attempt)\n                continue\n            return r\n        except requests.RequestException:\n            time.sleep(2 ** attempt)\n    raise Exception(f\"Failed after {max_retries} retries\")\n```\n\n### 认证错误处理\n\n```python\ndef safe_call(url, key):\n    r = requests.get(url, headers={\"Authorization\": f\"Bearer {key}\"})\n    if r.status_code == 401:\n        # Mind Key 无效 — 提醒用户\n        raise AuthError(\"Mind Key invalid or expired\")\n    return r\n```\n\n## 常见错误场景\n\n### \"Mind Key fehlt oder ungültig\"\n\n- 你忘记加 `Authorization` 头\n- 你用了 `?key=` 但 key 错误\n- 你在需要 Mind Key 的地方用了 JWT\n\n### \"Route not found\"\n\n- 你猜了一个不存在的路径\n- 你用错了动词（例如 `GET /memory` 与 `POST /memory` 混淆）\n- 检查 `GET /endpoints` 获取有效路径\n\n### \"Rate limit exceeded\"\n\n- 你使用了 `?key=` 并超过 60 次/分钟\n- 改用 `Authorization: Bearer` 头\n\n## 下一步\n\n- [Authentication](/docs/getting-started/authentication)\n- [API 概览](/docs/api/overview)\n- [限速](/docs/api/rate-limits)\n","content_html":"<h1>错误与错误处理</h1>\n<p>Synapse 使用标准 HTTP 状态码，并提供一致的错误响应格式。本页说明如何解读错误并从中恢复。</p>\n<h2>错误响应格式</h2>\n<p>所有错误都返回如下结构的 JSON：</p>\n<pre><code class=\"hljs language-json\"><span class=\"hljs-punctuation\">{</span>\n  <span class=\"hljs-attr\">&quot;statusCode&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-number\">401</span><span class=\"hljs-punctuation\">,</span>\n  <span class=\"hljs-attr\">&quot;error&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">&quot;Unauthorized&quot;</span><span class=\"hljs-punctuation\">,</span>\n  <span class=\"hljs-attr\">&quot;message&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">&quot;Mind Key fehlt oder ungültig.&quot;</span><span class=\"hljs-punctuation\">,</span>\n  <span class=\"hljs-attr\">&quot;docs&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">&quot;https://synapse.schaefer.zone/docs/getting-started/authentication&quot;</span>\n<span class=\"hljs-punctuation\">}</span></code></pre><table>\n<thead>\n<tr>\n<th>字段</th>\n<th>说明</th>\n</tr>\n</thead>\n<tbody><tr>\n<td><code>statusCode</code></td>\n<td>HTTP 状态码</td>\n</tr>\n<tr>\n<td><code>error</code></td>\n<td>HTTP 状态名称</td>\n</tr>\n<tr>\n<td><code>message</code></td>\n<td>人类可读的错误描述</td>\n</tr>\n<tr>\n<td><code>docs</code></td>\n<td>指向相关文档的 URL（如适用）</td>\n</tr>\n</tbody></table>\n<h2>HTTP 状态码</h2>\n<h3>200 OK</h3>\n<p>成功。请求被正确处理。</p>\n<h3>201 Created</h3>\n<p>成功。创建了新资源（例如 <code>POST /memory</code>）。</p>\n<h3>204 No Content</h3>\n<p>成功。无响应体返回（例如 <code>DELETE /memory/:id</code>）。</p>\n<h3>400 Bad Request</h3>\n<p>请求格式不正确。常见原因：</p>\n<ul>\n<li>缺少必填的 JSON 字段</li>\n<li>JSON 语法无效</li>\n<li>枚举值无效（例如分类不正确）</li>\n</ul>\n<pre><code class=\"hljs language-json\"><span class=\"hljs-punctuation\">{</span>\n  <span class=\"hljs-attr\">&quot;statusCode&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-number\">400</span><span class=\"hljs-punctuation\">,</span>\n  <span class=\"hljs-attr\">&quot;error&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">&quot;Bad Request&quot;</span><span class=\"hljs-punctuation\">,</span>\n  <span class=\"hljs-attr\">&quot;message&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">&quot;category must be one of: identity, preference, fact, project, skill, mistake, context, note, credentials&quot;</span>\n<span class=\"hljs-punctuation\">}</span></code></pre><p><strong>修复：</strong> 对照 API 文档检查请求体。确保所有必填字段均存在且值合法。</p>\n<h3>401 Unauthorized</h3>\n<p>认证失败。常见原因：</p>\n<ul>\n<li>缺少 <code>Authorization</code> 头</li>\n<li>Mind Key 或 JWT 无效</li>\n<li>在需要 JWT 的地方用了 Mind Key（或反之）</li>\n</ul>\n<pre><code class=\"hljs language-json\"><span class=\"hljs-punctuation\">{</span>\n  <span class=\"hljs-attr\">&quot;statusCode&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-number\">401</span><span class=\"hljs-punctuation\">,</span>\n  <span class=\"hljs-attr\">&quot;error&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">&quot;Unauthorized&quot;</span><span class=\"hljs-punctuation\">,</span>\n  <span class=\"hljs-attr\">&quot;message&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">&quot;Mind Key fehlt oder ungültig.&quot;</span><span class=\"hljs-punctuation\">,</span>\n  <span class=\"hljs-attr\">&quot;docs&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">&quot;https://synapse.schaefer.zone/docs/getting-started/authentication&quot;</span>\n<span class=\"hljs-punctuation\">}</span></code></pre><p><strong>修复：</strong> 校验你的令牌。参见 <a href=\"/docs/getting-started/authentication\">Authentication</a>。</p>\n<h3>403 Forbidden</h3>\n<p>你已认证但无权执行此操作。常见原因：</p>\n<ul>\n<li>尝试删除他人的 Mind</li>\n<li>尝试用 Mind Key 验证记忆（需要 JWT）</li>\n<li>Mind 已被禁用</li>\n</ul>\n<p><strong>修复：</strong> 检查你为此端点使用的令牌类型是否正确。</p>\n<h3>404 Not Found</h3>\n<p>请求的路径或资源不存在。</p>\n<div class=\"callout callout-critical\">**不要猜测端点路径。** 只有 `GET /endpoints` 中列出的路径才存在。如果收到 404，说明你用了错误的路径。</div><pre><code class=\"hljs language-json\"><span class=\"hljs-punctuation\">{</span>\n  <span class=\"hljs-attr\">&quot;statusCode&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-number\">404</span><span class=\"hljs-punctuation\">,</span>\n  <span class=\"hljs-attr\">&quot;error&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">&quot;Not Found&quot;</span><span class=\"hljs-punctuation\">,</span>\n  <span class=\"hljs-attr\">&quot;message&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">&quot;Route GET /memory/list not found&quot;</span><span class=\"hljs-punctuation\">,</span>\n  <span class=\"hljs-attr\">&quot;docs&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">&quot;https://synapse.schaefer.zone/docs/api/errors&quot;</span>\n<span class=\"hljs-punctuation\">}</span></code></pre><p><strong>修复：</strong> 调用 <code>GET /endpoints</code> 查看有效端点列表。将你的 URL 与列表逐字符对比。</p>\n<h3>409 Conflict</h3>\n<p>请求与现有状态冲突。常见原因：</p>\n<ul>\n<li>尝试用已存在的邮箱注册</li>\n<li>Webhook URL 重复</li>\n</ul>\n<p><strong>修复：</strong> 使用不同的值，或使用 <code>PUT</code> 更新已有资源。</p>\n<h3>429 Too Many Requests</h3>\n<p>你触发了限速。仅适用于 <code>?key=</code> 查询参数认证（60 次/分钟）。</p>\n<pre><code class=\"hljs language-json\"><span class=\"hljs-punctuation\">{</span>\n  <span class=\"hljs-attr\">&quot;statusCode&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-number\">429</span><span class=\"hljs-punctuation\">,</span>\n  <span class=\"hljs-attr\">&quot;error&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">&quot;Too Many Requests&quot;</span><span class=\"hljs-punctuation\">,</span>\n  <span class=\"hljs-attr\">&quot;message&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">&quot;Rate limit exceeded. Use Authorization header for unlimited access.&quot;</span>\n<span class=\"hljs-punctuation\">}</span></code></pre><p>响应头：</p>\n<pre><code class=\"hljs language-plaintext\">X-RateLimit-Limit: 60\nX-RateLimit-Remaining: 0\nRetry-After: 42</code></pre><p><strong>修复：</strong> 改用 <code>Authorization: Bearer</code> 头（无限制），或等待 <code>Retry-After</code> 秒。</p>\n<h3>500 Internal Server Error</h3>\n<p>服务器错误。这种情况不应发生 — 如果发生，则是 Bug。</p>\n<p><strong>修复：</strong></p>\n<ol>\n<li>使用指数退避重试（1 秒、2 秒、4 秒、8 秒）</li>\n<li>检查 <code>GET /health</code> 查看服务器是否在线</li>\n<li>如果持续出现，请上报该错误</li>\n</ol>\n<h3>503 Service Unavailable</h3>\n<p>服务器临时不可用（例如部署、数据库迁移期间）。</p>\n<p><strong>修复：</strong> 等待并重试。检查 <code>GET /health</code>。</p>\n<h2>恢复模式</h2>\n<h3>指数退避重试</h3>\n<pre><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import</span> time\n<span class=\"hljs-keyword\">import</span> requests\n\n<span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">call_with_retry</span>(<span class=\"hljs-params\">url, max_retries=<span class=\"hljs-number\">3</span></span>):\n    <span class=\"hljs-keyword\">for</span> attempt <span class=\"hljs-keyword\">in</span> <span class=\"hljs-built_in\">range</span>(max_retries):\n        <span class=\"hljs-keyword\">try</span>:\n            r = requests.get(url, headers={<span class=\"hljs-string\">&quot;Authorization&quot;</span>: <span class=\"hljs-string\">f&quot;Bearer <span class=\"hljs-subst\">{KEY}</span>&quot;</span>})\n            <span class=\"hljs-keyword\">if</span> r.status_code == <span class=\"hljs-number\">429</span>:\n                wait = <span class=\"hljs-built_in\">int</span>(r.headers.get(<span class=\"hljs-string\">&quot;Retry-After&quot;</span>, <span class=\"hljs-number\">60</span>))\n                time.sleep(wait)\n                <span class=\"hljs-keyword\">continue</span>\n            <span class=\"hljs-keyword\">if</span> r.status_code &gt;= <span class=\"hljs-number\">500</span>:\n                time.sleep(<span class=\"hljs-number\">2</span> ** attempt)\n                <span class=\"hljs-keyword\">continue</span>\n            <span class=\"hljs-keyword\">return</span> r\n        <span class=\"hljs-keyword\">except</span> requests.RequestException:\n            time.sleep(<span class=\"hljs-number\">2</span> ** attempt)\n    <span class=\"hljs-keyword\">raise</span> Exception(<span class=\"hljs-string\">f&quot;Failed after <span class=\"hljs-subst\">{max_retries}</span> retries&quot;</span>)</code></pre><h3>认证错误处理</h3>\n<pre><code class=\"hljs language-python\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">safe_call</span>(<span class=\"hljs-params\">url, key</span>):\n    r = requests.get(url, headers={<span class=\"hljs-string\">&quot;Authorization&quot;</span>: <span class=\"hljs-string\">f&quot;Bearer <span class=\"hljs-subst\">{key}</span>&quot;</span>})\n    <span class=\"hljs-keyword\">if</span> r.status_code == <span class=\"hljs-number\">401</span>:\n        <span class=\"hljs-comment\"># Mind Key 无效 — 提醒用户</span>\n        <span class=\"hljs-keyword\">raise</span> AuthError(<span class=\"hljs-string\">&quot;Mind Key invalid or expired&quot;</span>)\n    <span class=\"hljs-keyword\">return</span> r</code></pre><h2>常见错误场景</h2>\n<h3>&quot;Mind Key fehlt oder ungültig&quot;</h3>\n<ul>\n<li>你忘记加 <code>Authorization</code> 头</li>\n<li>你用了 <code>?key=</code> 但 key 错误</li>\n<li>你在需要 Mind Key 的地方用了 JWT</li>\n</ul>\n<h3>&quot;Route not found&quot;</h3>\n<ul>\n<li>你猜了一个不存在的路径</li>\n<li>你用错了动词（例如 <code>GET /memory</code> 与 <code>POST /memory</code> 混淆）</li>\n<li>检查 <code>GET /endpoints</code> 获取有效路径</li>\n</ul>\n<h3>&quot;Rate limit exceeded&quot;</h3>\n<ul>\n<li>你使用了 <code>?key=</code> 并超过 60 次/分钟</li>\n<li>改用 <code>Authorization: Bearer</code> 头</li>\n</ul>\n<h2>下一步</h2>\n<ul>\n<li><a href=\"/docs/getting-started/authentication\">Authentication</a></li>\n<li><a href=\"/docs/api/overview\">API 概览</a></li>\n<li><a href=\"/docs/api/rate-limits\">限速</a></li>\n</ul>\n","urls":{"html":"/docs/api/errors","text":"/docs/api/errors?format=text","json":"/docs/api/errors?format=json","llm":"/docs/api/errors?format=llm"},"translations_available":["en","zh","hi","es","fr","ar","pt","ru","ja","de","it","ko","nl","pl","tr","sv","vi","th","id","uk"]}