Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLI] Increase default timeout for CLI #345

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 45 additions & 39 deletions client/packages/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,65 +541,71 @@ async function fetchJson({
let authToken = null;
if (withAuth) {
authToken = await readConfigAuthToken();

if (!authToken) {
console.error("Unauthenticated. Please log in with `instant-cli login`");
console.error("Unauthenticated. Please log in with `instant-cli login`");
return { ok: false, data: undefined };
}
}
const timeoutMs = 1000 * 60 * 5; // 5 minutes

const res = await fetch(`${instantBackendOrigin}${path}`, {
method: method ?? "GET",
headers: {
...(withAuth ? { Authorization: `Bearer ${authToken}` } : {}),
"Content-Type": "application/json",
},
body: body ? JSON.stringify(body) : undefined,
});

if (verbose) {
console.log(debugName, "response:", res.status, res.statusText);
}
try {
const res = await fetch(`${instantBackendOrigin}${path}`, {
method: method ?? "GET",
headers: {
...(withAuth ? { Authorization: `Bearer ${authToken}` } : {}),
"Content-Type": "application/json",
},
body: body ? JSON.stringify(body) : undefined,
signal: AbortSignal.timeout(timeoutMs),
});

if (!res.ok) {
if (withErrorLogging) {
console.error(errorMessage);
if (verbose) {
console.log(debugName, "response:", res.status, res.statusText);
}
let errData;

let data;
try {
errData = await res.json();
data = await res.json();
} catch {
data = null;
}

if (!res.ok) {
if (withErrorLogging) {
if (errData?.message) {
console.error(errData.message);
console.error(errorMessage);
if (data?.message) {
console.error(data.message);
}

if (Array.isArray(errData?.hint?.errors)) {
for (const error of errData.hint.errors) {
if (Array.isArray(data?.hint?.errors)) {
for (const error of data.hint.errors) {
console.error(
`${error.in ? error.in.join("->") + ": " : ""}${error.message}`,
`${error.in ? error.in.join("->") + ": " : ""}${error.message}`
);
}
}
if (!data) {
console.error("Failed to parse error response");
}
}
} catch (error) {
if (withErrorLogging) {
console.error("Failed to parse error response");
}
return { ok: false, data };
}

return { ok: false, data: errData };
}
if (verbose) {
console.log(debugName, "data:", data);
}

const data = await res.json();
return { ok: true, data };

if (verbose) {
console.log(debugName, "data:", data);
} catch (err) {
if (withErrorLogging) {
if (err.name === "AbortError") {
console.error(`Timeout: It took more than ${timeoutMs / 60000} minutes to get the result!`);
} else {
console.error(`Error: type: ${err.name}, message: ${err.message}`);
}
}
return { ok: false, data: null };
}

return {
ok: true,
data,
};
}

async function promptOk(message) {
Expand Down