Skip to content

Commit

Permalink
fix: calling function one more time after complete error retries
Browse files Browse the repository at this point in the history
  • Loading branch information
Complexlity committed Aug 25, 2024
1 parent 047020f commit b605de4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
19 changes: 11 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,25 @@ class uniFarcasterSdk implements Omit<Service, "name"> {
...args: any[]
): Promise<DataOrError<T>> {
let attempts = 0;

let result: DataOrError<T> = {
data: null, error: {message: "Something went wrong. Please try again"}
};
// console.log(this.retries)
while (attempts <= this.retries) {
const result = await fn(...args);
result = await fn(...args);

if (!result.error) {
return result;
}
if (attempts > 0) {
this.logger({ name: "retrying..." }).warning(
`attempt ${attempts} of ${this.retries}`,
);
}
attempts++;
if (attempts <= this.retries) {
this.logger({ name: "retrying..." }).warning(
`attmept ${attempts} of ${this.retries}`,
);
}
}
return await fn(...args);
return result

}

private async withCache<T extends CacheKeys>(
Expand Down
1 change: 1 addition & 0 deletions src/retries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ describe("uniFarcasterSdk with retries", () => {
const result = await sdk.getUserByFid(mockUser.fid);

expect(result).toEqual(errorResponse);
expect(mockService.getUserByFid).toHaveBeenCalledTimes(3);
});

test("should not retry if first attempt is successful", async () => {
Expand Down
5 changes: 3 additions & 2 deletions src/services/neynar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ export class neynarService implements Service {
private handleError(e: unknown) {
const GENERIC_ERROR_MESSAGE = "Something went wrong. Please try again";
if (e instanceof AxiosError) {
const errorMessage = typeof e.response?.data.message === "string" ? e.response?.data.message : typeof e.response?.data.error.message === "string" ? e.response?.data?.error.message :GENERIC_ERROR_MESSAGE ;
return {
data: null,
error: {
message: e.response?.data?.error.message || GENERIC_ERROR_MESSAGE,
message: errorMessage as string,
},
};
} else if (!!e && typeof e === "object" && "message" in e && e.message) {
} else if (!!e && typeof e === "object" && "message" in e && e.message && typeof e.message === "string") {
return {
data: null,
error: { message: e.message },
Expand Down

0 comments on commit b605de4

Please sign in to comment.