错误响应
#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") |
message | string 或 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();
}#常见场景
| 情形 | 响应 |
|---|---|
缺少 outcomeId | 400 + ["outcomeId should not be empty"] |
| 错误的 enum | 400 + ["status must be one of: ACTIVE, ..."] |
| 市场不存在 | 404 + "Market not found" |
| Rate limit | 429 + "ThrottlerException: Too Many Requests" |
#Rate limit
公开接口在应用层按 IP 进行限流 —— 具体数值参见 速率限制。
收到 429 时请在短暂退避(数秒)后重试。若持续被拦截,请参考 Retry-After 头。
#下一步
- 分页 —— 正常响应的 list envelope
- REST → Markets —— 实际端点
本页面是否有帮助?