You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
692 B
30 lines
692 B
export type GatewayResult<T> = {
|
|
status: number;
|
|
body: T;
|
|
};
|
|
|
|
const INTERNAL_ERROR_BODY = {
|
|
code: "INTERNAL_ERROR",
|
|
message: "服务暂时不可用,请稍后重试",
|
|
data: null,
|
|
} as const;
|
|
|
|
/**
|
|
* 网关层 5xx 单次重试:首次 5xx 再执行一次,仍 5xx 则返回 503 INTERNAL_ERROR。
|
|
*/
|
|
export async function executeWithGatewayRetry<T>(
|
|
handler: () => Promise<GatewayResult<T>>,
|
|
): Promise<GatewayResult<T | typeof INTERNAL_ERROR_BODY>> {
|
|
const first = await handler();
|
|
if (first.status < 500) {
|
|
return first;
|
|
}
|
|
|
|
const second = await handler();
|
|
if (second.status < 500) {
|
|
return second;
|
|
}
|
|
|
|
return { status: 503, body: INTERNAL_ERROR_BODY };
|
|
}
|