Skip to content

Error Codes

Error codes are a single shared namespace (internal/pkg/errors.go): the gateway, handlers, and services use the same ranges, and modules may not invent their own. The response body is always:

{ "code": 2001, "msg": "参数无效", "data": null }

code is the business code, msg is the default human-readable text, and data is null on failure. Check code === 0 for success rather than the HTTP status — the two intentionally differ in a few cases.

CodeNameHTTPDefault messageTypical trigger
1001TOKEN_NOT_EXIST401令牌不存在No Authorization or X-API-Key
1002TOKEN_WRONG401令牌错误Malformed sid, unknown API key
1003TOKEN_EXPIRED401令牌已过期Session expired or its row was cleaned up
1010INVALID_PASSWORD401密码错误Wrong password
1011USER_NOT_FOUND401用户不存在Unknown username; usually indistinguishable from 1010
1012USERNAME_EXISTS401用户名已存在Registration or rename conflict
1013FORBIDDEN403禁止访问Insufficient permission or space-level denial
1014TOKEN_REVOKED401令牌已被撤销Old sid after logout, password, or role change
1015USER_BANNED403用户已被封禁role=ban or a write during a ban
1016EMAIL_EXISTS401邮箱已存在Duplicate registration email
CodeNameHTTPDefault messageTypical trigger
2001INVALID_PARAMS400参数无效Missing field, wrong type, out of range, invalid sort key
CodeNameHTTPDefault messageTypical trigger
3001NOT_FOUND404资源不存在Missing or invisible post, user, or space
3002ALREADY_EXISTS409资源已存在Unique constraint (slug, invite code)
CodeNameHTTPDefault messageTypical trigger
4001RATE_LIMITED429请求过于频繁,请稍后再试Endpoint budget or the global per-IP ceiling
4002BODY_TOO_LARGE413请求体过大PathBodyLimit or the upload limit
CodeNameHTTPDefault messageTypical trigger
5001INTERNAL_ERROR500服务器内部错误Unclassified internal error, panic fallback
5004REQUEST_TIMEOUT504请求超时Soft-timeout middleware (SSE excluded)
5005MAINTENANCE503站点维护中,请稍后再试Writes while site_maintenance=true

Banned users get 404, not 403. The gateway’s ban check treats a banned account as a missing resource so it cannot be used to confirm that an account exists. USER_BANNED maps to 403 in general, but the ban path returns 404 via pkg.FailStatus — the status itself is the contract.

OAuth / OIDC protocol endpoints skip the envelope. /.well-known/*, token, and userinfo must keep their RFC shapes ({"access_token": "...", "token_type": "Bearer"}, {"error": "invalid_grant"}), or standard clients break. This is the only sanctioned bypass.

const res = await fetch('/api/v1/posts/list', { method: 'POST', body: JSON.stringify(body) });
const payload = await res.json();
if (payload.code === 0) return payload.data;
if (payload.code === 1003 || payload.code === 1014) {
// Session is gone: clear local credentials and redirect to login.
}
throw new ApiError(payload.code, payload.msg);

Do not branch on the HTTP status: 1003 and 2001 may both arrive as 4xx but need different handling.