Add retry
This commit is contained in:
65
src/utils.ts
65
src/utils.ts
@@ -94,31 +94,37 @@ const getSecretFromConnectItem = async (
|
|||||||
|
|
||||||
// If a file was found, get the content of the file
|
// If a file was found, get the content of the file
|
||||||
if (fileId) {
|
if (fileId) {
|
||||||
try {
|
const maxAttempts = 3;
|
||||||
core.info(`Getting file content: vault=${parsed.vault} item=${parsed.item} fileId=${fileId}`);
|
const retryDelayMs = 2000;
|
||||||
const content = await client.getFileContent(
|
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
||||||
parsed.vault,
|
try {
|
||||||
parsed.item,
|
const content = await client.getFileContent(
|
||||||
fileId,
|
parsed.vault,
|
||||||
);
|
parsed.item,
|
||||||
return content;
|
fileId,
|
||||||
} catch (err) {
|
);
|
||||||
if (err && typeof err === "object") {
|
return content;
|
||||||
core.error(`getFileContent error keys: ${Object.keys(err).join(", ")}`);
|
} catch (err) {
|
||||||
core.error(`getFileContent err.message: ${(err as any)?.message}`);
|
const is503 =
|
||||||
core.error(`getFileContent err.code: ${(err as any)?.code}`);
|
err &&
|
||||||
if ((err as any)?.response) {
|
typeof err === "object" &&
|
||||||
core.error(`getFileContent err.response.status: ${(err as any).response?.status}`);
|
(err as Record<string, unknown>).statusCode === 503;
|
||||||
core.error(`getFileContent err.response.data: ${JSON.stringify((err as any).response?.data)}`);
|
if (is503 && attempt < maxAttempts) {
|
||||||
}
|
core.info(
|
||||||
if ((err as any)?.cause) {
|
`getFileContent returned 503 (attempt ${attempt}/${maxAttempts}), retrying in ${retryDelayMs / 1000}s...`,
|
||||||
core.error(`getFileContent err.cause: ${(err as any).cause?.message ?? String((err as any).cause)}`);
|
);
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, retryDelayMs));
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
core.error(`getFileContent failed: ${getErrorMessage(err)}`);
|
||||||
|
throw err;
|
||||||
}
|
}
|
||||||
core.error(`getFileContent failed: ${getErrorMessage(err)}`);
|
|
||||||
throw err;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
core.error(`getFileContent failed: ${getErrorMessage(err)}`);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (parsed.section) {
|
if (parsed.section) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@@ -443,21 +449,8 @@ const loadSecretsViaConnect = async (
|
|||||||
|
|
||||||
function getErrorMessage(err: unknown): string {
|
function getErrorMessage(err: unknown): string {
|
||||||
if (err instanceof Error) return err.message;
|
if (err instanceof Error) return err.message;
|
||||||
if (!err || typeof err !== "object") return String(err);
|
if (err && typeof (err as { message?: unknown }).message === "string")
|
||||||
const e = err as Record<string, unknown>;
|
return (err as { message: string }).message;
|
||||||
// Node HTTP response object (thrown by SDK on HTTP errors)
|
|
||||||
if (typeof e.statusCode === "number") {
|
|
||||||
const statusMsg = e.statusMessage != null ? String(e.statusMessage) : "";
|
|
||||||
return `HTTP ${e.statusCode}${statusMsg ? `: ${statusMsg}` : ""}`.trim();
|
|
||||||
}
|
|
||||||
if (typeof e.message === "string") return e.message;
|
|
||||||
if (e.response && typeof e.response === "object") {
|
|
||||||
const res = e.response as Record<string, unknown>;
|
|
||||||
if (typeof res.status === "number")
|
|
||||||
return `HTTP ${res.status}${res.statusText ? `: ${res.statusText}` : ""}`.trim();
|
|
||||||
}
|
|
||||||
if (typeof e.code === "string") return e.code;
|
|
||||||
if (e.cause instanceof Error) return e.cause.message;
|
|
||||||
return String(err);
|
return String(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user