fix retry

This commit is contained in:
discountry
2025-09-23 16:00:51 +08:00
parent ffa5b9e6fa
commit f54e81c7df
9 changed files with 164 additions and 152 deletions
+15
View File
@@ -0,0 +1,15 @@
export function isUnknownOrderError(error: unknown): boolean {
const message = extractMessage(error);
if (!message) return false;
return message.includes("Unknown order") || message.includes("code\":-2011");
}
export function extractMessage(error: unknown): string {
if (typeof error === "string") return error;
if (error instanceof Error) return error.message;
try {
return JSON.stringify(error);
} catch {
return String(error);
}
}
-37
View File
@@ -1,37 +0,0 @@
import fs from "fs/promises";
import path from "path";
const DATA_DIR = path.resolve(process.cwd(), "data");
let dataDirReady = false;
async function ensureDataDir(): Promise<void> {
if (dataDirReady) return;
await fs.mkdir(DATA_DIR, { recursive: true }).catch(() => undefined);
dataDirReady = true;
}
export async function loadState<T>(fileName: string): Promise<T | null> {
try {
await ensureDataDir();
const filePath = path.join(DATA_DIR, fileName);
const data = await fs.readFile(filePath, "utf8");
return JSON.parse(data) as T;
} catch (error: any) {
if (error?.code === "ENOENT") return null;
console.error(`[state] load ${fileName} failed`, error);
return null;
}
}
export async function saveState(fileName: string, state: unknown): Promise<void> {
try {
await ensureDataDir();
const filePath = path.join(DATA_DIR, fileName);
const tempPath = `${filePath}.tmp`;
const payload = JSON.stringify(state, null, 2);
await fs.writeFile(tempPath, payload, "utf8");
await fs.rename(tempPath, filePath);
} catch (error) {
console.error(`[state] save ${fileName} failed`, error);
}
}