状态码
Understand HTTP status codes returned by Edge Functions to properly debug issues and handle responses.
在调用 Edge Function 时,响应可能会返回各种 HTTP 状态码。最常见的状态码列在下面。
🌐 When invoking an Edge Function, the response may return a variety of HTTP status codes. The most common status codes are listed below.
错误响应也可能包含一个 sb-error-code 头,用于标识具体的错误情况。完整的错误代码及其含义请参见 Error Codes。
🌐 Error responses may also include an sb-error-code header that identifies the specific error condition. See Error Codes for a complete list of error codes and their meanings.
成功回应 #
🌐 Success Responses
2XX 成功 #
🌐 2XX Success
你的 Edge 函数执行成功并返回了有效响应。这包括你函数明确返回的任何 200-299 范围内的状态码。
🌐 Your Edge Function executed successfully and returned a valid response. This includes any status code in the 200-299 range that your function explicitly returns.
3XX 重定向 #
🌐 3XX Redirect
你的边缘函数使用了 Response.redirect() API 将客户端重定向到不同的 URL。在实现认证流程或 URL 转发时,这是正常的响应。
🌐 Your Edge Function used the Response.redirect() API to redirect the client to a different URL. This is a normal response when implementing authentication flows or URL forwarding.
客户端错误 #
🌐 Client Errors
这些错误表明请求本身有问题,通常需要改变函数的调用方式。
🌐 These errors indicate issues with the request itself, which typically require changing how the function is called.
401 未经授权 #
🌐 401 Unauthorized
**原因:**Edge 函数启用了 JWT 验证,但请求使用了无效或缺失的 JWT 令牌。
解决方案:
- 确保你在
Authorization头中传递了一个有效的 JWT 令牌 - 检查一下你的令牌是否已过期
- 对于网页钩子或公共端点,考虑关闭 JWT 验证
404 未找到 #
🌐 404 Not Found
原因: 请求的 Edge 功能不存在或 URL 路径不正确。
解决方案:
- 确认一下你请求 URL 中的函数名和项目引用
- 检查一下函数是否已成功部署
不允许使用405方法 #
🌐 405 Method Not Allowed
原因: 你正在使用不支持的 HTTP 方法。Edge Functions 仅支持:GET、POST、PUT、PATCH、DELETE 和 OPTIONS。
解决方法: 更新你的请求以使用受支持的 HTTP 方法。
服务器错误 #
🌐 Server Errors
这些错误表示函数执行或底层平台存在问题。
🌐 These errors indicate issues with the function execution or underlying platform.
500 内部服务器错误 #
🌐 500 Internal Server Error
原因: 你的 Edge 函数抛出了未捕获的异常(WORKER_ERROR)。
常见原因:
- 你的函数代码中有未处理的 JavaScript 错误
- 异步操作缺少错误处理
- 无效的 JSON 解析
解决方法: 查看你的 Edge Function 日志以确定具体错误,并在代码中添加适当的错误处理。
1// ✅ Good error handling2try {3 const result = await someAsyncOperation()4 return new Response(JSON.stringify(result))5} catch (error) {6 console.error('Function error:', error)7 return new Response('Internal error', { status: 500 })8}你可以在 Edge Function 日志 中看到输出。
🌐 You can see the output in the Edge Function Logs.
503 服务不可用 #
🌐 503 Service Unavailable
原因: 你的 Edge 功能启动失败(BOOT_ERROR)。
常见原因:
- 语法错误导致函数无法加载
- 导入错误或缺少依赖
- 无效的函数配置
解决方案: 检查你的 Edge Function 日志,并确认你的函数代码可以在本地通过 supabase functions serve 执行。
504 网关超时 #
🌐 504 Gateway Timeout
原因: 你的 Edge Function 没有在 请求超时限制 内响应。
常见原因:
- 长时间运行的数据库查询
- 外部 API 调用慢
- 无限循环或阻塞操作
解决方案:
- 优化慢操作
- 给外部请求加上超时处理
- 考虑把大操作拆成小块
546 资源限制(自定义错误代码) #
🌐 546 Resource Limit (Custom Error Code)
原因: 你的 Edge Function 执行因为超出资源限制而被停止(之前是 WORKER_LIMIT,现在是 WORKER_RESOURCE_LIMIT)。Edge Function 日志应该会显示具体超出了哪个 资源限制。
常见原因:
- 内存使用超出可用限制
- CPU 时间超出执行配额
- 同时操作太多了
解决方法: 查看你的 Edge Function 日志,看看哪个资源限制被超出了,然后相应地优化你的函数。