Skip to main content
返回应用

错误响应

#HTTP 状态码

含义
200成功
201资源已创建
400请求体 / 查询校验失败
404资源不存在
429超出速率限制
500服务器错误

#响应 envelope

{
  "statusCode": 400,
  "error": "Bad Request",
  "message": [
    "outcomeId must be a string",
    "side must be one of: BUY, SELL"
  ]
}
字段说明
statusCode与 HTTP 状态相同的数字
error简短的人类可读标签("Bad Request""Not Found"
messagestring 或 string[]。校验失败时始终为数组。
注意 message 可能是数组

NestJS ValidationPipe 的校验失败始终以 message: string[] 返回。不要简单地把 error.message 当作单行显示,而应分别渲染数组的每个元素。

#客户端处理模式

type ApiError = {
  statusCode: number;
  error: string;
  message: string | string[];
};
 
async function call() {
  const res = await fetch(url, init);
  if (!res.ok) {
    const err = (await res.json()) as ApiError;
    const messages = Array.isArray(err.message) ? err.message : [err.message];
    throw new Error(messages.join('\n'));
  }
  return res.json();
}

#常见场景

情形响应
缺少 outcomeId400 + ["outcomeId should not be empty"]
错误的 enum400 + ["status must be one of: ACTIVE, ..."]
市场不存在404 + "Market not found"
Rate limit429 + "ThrottlerException: Too Many Requests"

#Rate limit

公开接口在应用层按 IP 进行限流 —— 具体数值参见 速率限制

收到 429 时请在短暂退避(数秒)后重试。若持续被拦截,请参考 Retry-After 头。

#下一步

本页面是否有帮助?