-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding retryable error handler
- Loading branch information
1 parent
88d79db
commit 1ff5a5d
Showing
4 changed files
with
74 additions
and
79 deletions.
There are no files selected for viewing
12 changes: 0 additions & 12 deletions
12
backend/src/build-system/__tests__/test.model-provider.spec.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Logger } from "@nestjs/common"; | ||
|
||
export class RetryHandler { | ||
private readonly logger = new Logger(RetryHandler.name); | ||
private static instance: RetryHandler; | ||
private retryCounts: Map<string, number> = new Map(); | ||
public static getInstance(){ | ||
if(this.instance)return this.instance; | ||
return new RetryHandler(); | ||
} | ||
async retryMethod(error: Error, upperMethod: (...args: any[]) => void, args: any[]): Promise<void>{ | ||
const errorName = error.name; | ||
let retryCount = this.retryCounts.get(errorName) || 0; | ||
let isRetrying = this.retryCounts.has(errorName); | ||
let res ; | ||
switch (errorName) { | ||
case 'GeneratedTagError': | ||
case 'TimeoutError': | ||
this.logger.warn(`Retryable error occurred: ${error.message}. Retrying...`); | ||
if(!isRetrying)this.retryCounts.set(errorName, 0); | ||
else this.retryCounts.set(errorName, this.retryCounts.get(errorName)+1); | ||
while (retryCount < 3) { | ||
try { | ||
this.logger.log(`retryCount: ${retryCount}`) | ||
res = await upperMethod(...args); | ||
|
||
} catch (e) { | ||
if (e.name === errorName) { | ||
retryCount++; | ||
this.retryCounts.set(errorName, retryCount); | ||
this.logger.warn(`Retryable error occurred: ${e.name}: ${e.message}. Retrying attempt ${retryCount}...`); | ||
} else { | ||
this.logger.error(`Non-retryable error occurred: ${e.name}: ${e.message}. Terminating process.`); | ||
throw e; | ||
} | ||
} | ||
} | ||
this.retryCounts.delete(errorName); | ||
if(res)return res; | ||
this.logger.error('Max retry attempts reached. Terminating process.'); | ||
throw new Error('Max retry attempts reached'); | ||
default: | ||
this.logger.error(`Non-retryable error occurred: ${error.name}: ${error.message}. Terminating process.`); | ||
throw error; | ||
} | ||
} | ||
} |