Skip to content

Commit 61b0535

Browse files
divyav-aotdivyav-aot
andauthored
MAX_RETRIES added for the roles API call incase the access token expires (#581)
* VERSION updated to v7.0.0-rsbc-v0.3.8 (#75) Co-authored-by: divyav-aot <divyav-aot@github> * JWT token refresh issue fixed (#76) * formioToken refresh integrated with KeycloakService. * made updateJwtToken public static to access in offlineSubmissions. * VERSION updated to v7.0.0-rsbc-v0.3.9 * proper comments added * reverted some changes with constants.ts --------- Co-authored-by: divyav-aot <divyav-aot@github> * MAX_RETRIES added for the roles API call incase the access token expires (#77) * formioToken refresh integrated with KeycloakService. * made updateJwtToken public static to access in offlineSubmissions. * VERSION updated to v7.0.0-rsbc-v0.3.9 * proper comments added * reverted some changes with constants.ts * MAX_RETRIES = 3 added for the roles API call incase the token expires --------- Co-authored-by: divyav-aot <divyav-aot@github> --------- Co-authored-by: divyav-aot <divyav-aot@github>
1 parent f170b56 commit 61b0535

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

forms-flow-service/src/request/jwtTokenService.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,34 @@ import RequestService from "./requestService";
22
import { API } from "../constants/constants";
33

44
export const getUpdatedJwtToken = async (): Promise<any> => {
5-
try {
6-
const response = await RequestService.httpGETRequest(
7-
API.FORMIO_ROLES,
8-
null,
9-
null
10-
);
11-
if (response) {
12-
return response;
13-
} else {
14-
console.log(`No user roles found!`);
15-
return null;
16-
}
17-
} catch (error: any) {
18-
if (error?.response?.data) {
19-
console.log(error.response.data?.message);
20-
} else {
21-
console.log(`Failed to fetch user roles!`);
5+
const MAX_RETRIES = 3;
6+
7+
for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
8+
try {
9+
const response = await RequestService.httpGETRequest(
10+
API.FORMIO_ROLES,
11+
null,
12+
null
13+
);
14+
if (response) {
15+
return response;
16+
} else {
17+
console.warn(`Attempt ${attempt}: No user roles found!`);
18+
}
19+
} catch (error: any) {
20+
const errorMessage = error?.response?.data?.message ?? "Unknown error";
21+
console.warn(`Attempt ${attempt} failed: ${errorMessage}`);
22+
23+
// If it's the last attempt, throw the error
24+
if (attempt === MAX_RETRIES) {
25+
console.error("Max retries reached. Giving up.");
26+
throw error;
27+
}
28+
29+
// a small delay before retrying
30+
await new Promise((res) => setTimeout(res, 500));
2231
}
23-
throw error;
2432
}
33+
return null; // Just in case all retries fail without throwing
2534
};
35+

0 commit comments

Comments
 (0)