Skip to content
Merged
Show file tree
Hide file tree
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
20 changes: 19 additions & 1 deletion dist/build.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion dist/merge.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion dist/preview.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions src/stainless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ export function createAutoRefreshFetch(
};
}

/**
* adds retries to a fetch function for 401 responses, as it seems that OIDC decode on the server can
* occasionally fail transiently
*/
export const addFetch401Retries = (
fetch: typeof globalThis.fetch,
{ numRetries }: { numRetries: number },
): typeof globalThis.fetch => {
return async (input, init) => {
let attempts = 0;
let response;
do {
response = await fetch(input, init);
if (response.status !== 401) {
break;
}
attempts++;
logger.warn("Received 401 response, retrying...");
} while (attempts <= numRetries);
return response;
};
};

export function getStainlessClient(
action: string | undefined,
opts: ClientOptions,
Expand Down
11 changes: 9 additions & 2 deletions src/wrapAction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import Stainless from "@stainless-api/sdk";
import { getInput } from "./compat/input";
import { getStainlessAuth } from "./compat";
import { createAutoRefreshFetch, getStainlessClient } from "./stainless";
import {
addFetch401Retries,
createAutoRefreshFetch,
getStainlessClient,
} from "./stainless";
import { logger } from "./logger";

const accumulatedBuildIds = new Set<string>();
Expand Down Expand Up @@ -33,7 +37,10 @@ export function wrapAction(
apiKey: auth.key,
logLevel: "warn",
logger,
fetch: createAutoRefreshFetch(auth, getStainlessAuth),
fetch: addFetch401Retries(
createAutoRefreshFetch(auth, getStainlessAuth),
{ numRetries: 2 },
),
});
await fn(stainless);
await maybeReportResult({
Expand Down
Loading