Files
Genarrative/packages/shared/src/http.ts
T
kdletters 687bad0adf
Project CI / AI game creator shell Rust shard 1/4 (push) Successful in 5m1s
Project CI / AI game creator shell Rust shard 2/4 (push) Successful in 4m27s
Project CI / AI game creator shell Rust shard 3/4 (push) Successful in 4m44s
Project CI / AI game creator shell Rust smoke (push) Successful in 1m34s
Project CI / AI game creator shell Rust shard 4/4 (push) Successful in 3m56s
Project CI / AI game creator shell Rust crates (push) Successful in 2m4s
Project CI / Frontend tests (push) Successful in 4m29s
Project CI / Repository checks (push) Successful in 3m42s
Project CI / Backend tests (push) Successful in 7m0s
Project CI / AI game creator shell web tests (push) Successful in 2m44s
Project CI / Native shell tests (push) Successful in 7m25s
修复登录失败原因提示 (#374)
登录失败时,部分网关或兼容接口返回字符串形式的 error,前端解析失败后只显示通用“登录失败”,用户无法判断具体原因。

本次修改:
- 统一错误解析器支持字符串 error,并保留标准嵌套错误的优先级。
- 增加登录接口返回“手机号或密码错误”的回归测试。
- 同步认证排障记录。

验证:
- API 客户端测试 32 项通过
- 认证服务与登录弹窗测试 55 项通过
- npm run typecheck 通过
- npm run lint:eslint 通过
- npm run check:encoding 通过

Reviewed-on: #374
Co-authored-by: kdletters <kdletters@qq.com>
Co-committed-by: kdletters <kdletters@qq.com>
2026-09-15 19:27:14 +08:00

269 lines
6.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export const API_VERSION = '2026-06-16';
export const API_RESPONSE_ENVELOPE_HEADER = 'x-genarrative-response-envelope';
export const API_RESPONSE_ENVELOPE_VERSION = 'v1';
export type ApiErrorCode =
| 'BAD_REQUEST'
| 'INVALID_REQUEST'
| 'VALIDATION_ERROR'
| 'UNAUTHORIZED'
| 'FORBIDDEN'
| 'NOT_FOUND'
| 'CONFLICT'
| 'UPSTREAM_ERROR'
| 'INTERNAL_SERVER_ERROR'
| 'bad_request'
| 'validation_error'
| 'unauthorized'
| 'forbidden'
| 'not_found'
| 'conflict'
| 'upstream_error'
| 'internal_error'
| (string & Record<never, never>);
export type ApiErrorPayload = {
code: ApiErrorCode;
message: string;
details?: Record<string, unknown> | null;
};
export type ApiMeta = {
apiVersion: string;
requestId?: string;
routeVersion?: string;
operation?: string | null;
latencyMs?: number;
timestamp?: string;
};
export type ApiSuccessResponse<T> = {
ok: true;
data: T;
error: null;
meta: ApiMeta;
};
export type ApiErrorResponse = {
ok: false;
data: null;
error: ApiErrorPayload;
meta: ApiMeta;
};
export type ApiResponse<T> = ApiSuccessResponse<T> | ApiErrorResponse;
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null;
}
function buildApiMeta(meta: Partial<ApiMeta> = {}): ApiMeta {
return {
apiVersion: meta.apiVersion ?? API_VERSION,
requestId:
typeof meta.requestId === 'string' && meta.requestId.trim()
? meta.requestId.trim()
: undefined,
routeVersion:
typeof meta.routeVersion === 'string' && meta.routeVersion.trim()
? meta.routeVersion.trim()
: undefined,
operation:
typeof meta.operation === 'string' && meta.operation.trim()
? meta.operation.trim()
: meta.operation === null
? null
: undefined,
latencyMs:
typeof meta.latencyMs === 'number' && Number.isFinite(meta.latencyMs)
? meta.latencyMs
: undefined,
timestamp:
typeof meta.timestamp === 'string' && meta.timestamp.trim()
? meta.timestamp.trim()
: undefined,
};
}
export function createApiSuccess<T>(
data: T,
meta: Partial<ApiMeta> = {},
): ApiSuccessResponse<T> {
return {
ok: true,
data,
error: null,
meta: buildApiMeta(meta),
};
}
export function createApiError(
error: ApiErrorPayload,
meta: Partial<ApiMeta> = {},
): ApiErrorResponse {
return {
ok: false,
data: null,
error: {
code: error.code,
message: error.message,
details: error.details ?? null,
},
meta: buildApiMeta(meta),
};
}
export function isApiResponse<T>(value: unknown): value is ApiResponse<T> {
if (!isRecord(value) || typeof value.ok !== 'boolean' || !('meta' in value)) {
return false;
}
if (!isRecord(value.meta) || typeof value.meta.apiVersion !== 'string') {
return false;
}
if (value.ok) {
return 'data' in value && value.error === null;
}
return (
value.data === null &&
isRecord(value.error) &&
typeof value.error.code === 'string' &&
typeof value.error.message === 'string'
);
}
export function unwrapApiResponse<T>(value: ApiResponse<T> | T): T {
if (!isApiResponse<T>(value)) {
return value as T;
}
if (value.ok) {
return value.data;
}
throw new Error(getApiErrorDisplayMessage(value.error) || '请求失败');
}
function readTrimmedMessage(value: unknown) {
return typeof value === 'string' && value.trim() ? value.trim() : '';
}
function readApiErrorDetailMessage(details: unknown) {
if (!isRecord(details)) {
return '';
}
// 后端通用 message 常用于错误分类;reason 更适合直接展示给用户,
// 例如 VectorEngine 网络分类会把底层 reqwest message 留给日志。
return (
readTrimmedMessage(details.reason) || readTrimmedMessage(details.message)
);
}
export function getApiErrorDisplayMessage(error: ApiErrorPayload) {
const detailMessage = readApiErrorDetailMessage(error.details);
return detailMessage || readTrimmedMessage(error.message);
}
export function parseApiErrorMessage(rawText: string, fallbackMessage: string) {
if (!rawText.trim()) {
return fallbackMessage;
}
try {
const parsed = JSON.parse(rawText) as
| ApiErrorResponse
| {
error?:
| string
| {
message?: string;
code?: string;
details?: Record<string, unknown> | null;
};
message?: string;
code?: string;
};
const detailMessage =
typeof parsed.error === 'object' && parsed.error !== null
? readApiErrorDetailMessage(parsed.error.details)
: '';
if (detailMessage) {
return detailMessage;
}
if (typeof parsed.error === 'string' && parsed.error.trim()) {
return parsed.error.trim();
}
if (
typeof parsed.error === 'object' &&
parsed.error !== null &&
typeof parsed.error.message === 'string' &&
parsed.error.message.trim()
) {
return parsed.error.message.trim();
}
const topLevelMessage =
'message' in parsed && typeof parsed.message === 'string'
? parsed.message.trim()
: '';
if (topLevelMessage) {
return topLevelMessage;
}
const errorCode =
typeof parsed.error === 'object' &&
parsed.error !== null &&
typeof parsed.error.code === 'string' &&
parsed.error.code.trim()
? parsed.error.code.trim()
: 'code' in parsed &&
typeof parsed.code === 'string' &&
parsed.code.trim()
? parsed.code.trim()
: '';
if (errorCode) {
return `${fallbackMessage}${errorCode}`;
}
} catch {
// Ignore malformed json responses.
}
return rawText.trim() || fallbackMessage;
}
export function appendApiErrorRequestId(
message: string,
requestId: string | null | undefined,
) {
const trimmedMessage = message.trim() || '请求失败';
const trimmedRequestId =
typeof requestId === 'string' && requestId.trim() ? requestId.trim() : '';
if (!trimmedRequestId || trimmedMessage.includes(trimmedRequestId)) {
return trimmedMessage;
}
return `${trimmedMessage}requestId: ${trimmedRequestId}`;
}
export function parseApiErrorMessageWithRequestId(
rawText: string,
fallbackMessage: string,
requestId: string | null | undefined,
) {
return appendApiErrorRequestId(
parseApiErrorMessage(rawText, fallbackMessage),
requestId,
);
}