Skip to content

Commit

Permalink
fix: add error logging and retry to threadCreate resume
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristers Dugels committed Aug 6, 2024
1 parent 7898ffe commit c5df400
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
20 changes: 17 additions & 3 deletions src/features/resume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ export const resumeResources = async (bot: Client) => {
const channel = interaction.channel;
const firstMessage = await retry(
() => channel.fetchStarterMessage(),
5,
10,
() => logger.log("RESUME", "Failed to fetch interaction first message"),
{
retries: 5,
delayMs: 10,
},
);
if (!firstMessage) {
await interaction.reply({
Expand Down Expand Up @@ -207,7 +210,18 @@ export const resumeResources = async (bot: Client) => {
if (thread.parentId !== CHANNELS.resumeReview) {
return;
}
const firstMessage = await thread.fetchStarterMessage();

const firstMessage = await retry(
() => thread.fetchStarterMessage(),
() => {
logger.log("RESUME", "Failed to fetch thread first message");
},
{
retries: 5,
delayMs: 10,
},
);

if (!firstMessage) {
return;
}
Expand Down
11 changes: 7 additions & 4 deletions src/features/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,24 @@ as the first line of the `attempt` function produced:
*
* @template T
* @param {() => Promise<T>} fn - The function returning a promise to be retried.
* @param {number} [retries=3] - The number of retry attempts.
* @param {number} [delayMs=1000] - The initial delay in milliseconds before retrying.
* @param {(error: unknown) => void} onError - The function to call when an error occurs.
* @param {{ retries?: number; delayMs?: number }} [options] - The retry options.
* @returns {Promise<T>} A promise that resolves with the result of the function or rejects after all retries have been exhausted.
*/
export function retry<T>(
fn: () => Promise<T>,
retries = 3,
delayMs = 1000,
onError: (error: unknown) => void,
options = { retries: 3, delayMs: 1000 },
): Promise<T> {
const { retries, delayMs } = options;

return new Promise((resolve, reject) => {
const attempt = (retryCount: number) => {
fn()
.then(resolve)
.catch((error) => {
if (retryCount <= 0) {
onError(error);
reject(error);
} else {
setTimeout(
Expand Down

0 comments on commit c5df400

Please sign in to comment.