Skip to content

Commit

Permalink
retry body_as_text 3 times
Browse files Browse the repository at this point in the history
  • Loading branch information
WebFreak001 committed Sep 26, 2022
1 parent c140a37 commit d85cc71
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

18 changes: 16 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import * as httpm from 'typed-rest-client/HttpClient';
import { BearerCredentialHandler } from 'typed-rest-client/Handlers';
import { IHttpClientResponse } from 'typed-rest-client/Interfaces';

export async function body_as_text(url: string, token: string = ''): Promise<string> {
const bearer = token ? [ new BearerCredentialHandler(token) ] : undefined;
let client = new httpm.HttpClient("dlang-community/setup-dlang", bearer);
return (await (await client.get(url)).readBody()).trim();
const timeouts = [5000, 10000, 20000];
let retry = 0;
let res: IHttpClientResponse | undefined = undefined;
for (; retry < 3; retry++) {
const client = new httpm.HttpClient("dlang-community/setup-dlang", bearer);
res = await client.get(url);
// redirects are followed by the library, check for error codes here
const statusCode = res?.message?.statusCode ?? 500;
if (statusCode >= 400) {
await new Promise(resolve => setTimeout(resolve, timeouts[retry]));
continue;
}
return (await res.readBody()).trim();
}
throw new Error(`failed requesting ${url} - aborting after ${retry} tries\n${res?.message.statusCode} ${res?.message.statusMessage}:\n${res?.message.rawHeaders.join('\n')}\n\n${(await res?.readBody())?.trim()}`);
}

0 comments on commit d85cc71

Please sign in to comment.