Skip to content

Commit

Permalink
Put throw outside of try/catch
Browse files Browse the repository at this point in the history
  • Loading branch information
ineiti committed Mar 12, 2024
1 parent f8d78b8 commit e80a2d9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 34 deletions.
4 changes: 3 additions & 1 deletion web/frontend/src/components/utils/usePostCall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const usePostCall = (setError) => {
try {
const result = await response.json();
if (result.Token) {
pollTransaction(checkTransaction, result.Token, 1000, 30).then(
// 600s is the time to shuffle 10'000 votes. This should be enough for
// everybody.
pollTransaction(checkTransaction, result.Token, 1000, 600).then(
() => {
setIsPosting((prev) => !prev);
},
Expand Down
11 changes: 0 additions & 11 deletions web/frontend/src/pages/form/components/FomrStatusLoading.tsx

This file was deleted.

45 changes: 23 additions & 22 deletions web/frontend/src/pages/form/components/utils/TransactionPoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,40 @@ const pollTransaction = (
let attempts = 0;

const request = {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
method: "GET",

Check failure on line 10 in web/frontend/src/pages/form/components/utils/TransactionPoll.ts

View workflow job for this annotation

GitHub Actions / Web frontend Lint

Replace `"GET"` with `'GET'`
headers: { "Content-Type": "application/json" }

Check failure on line 11 in web/frontend/src/pages/form/components/utils/TransactionPoll.ts

View workflow job for this annotation

GitHub Actions / Web frontend Lint

Replace `"Content-Type":·"application/json"·}` with `'Content-Type':·'application/json'·},`
};

const executePoll = async (resolve, reject): Promise<any> => {
let response, result;
try {
attempts += 1;
const response = await fetch(endpoint(data), request);
const result = await response.json();

if (!response.ok) {
throw new Error(JSON.stringify(result));
}
response = await fetch(endpoint(data), request);
result = await response.json();
} catch (e) {
return reject(e);
}

data = result.Token;
if (!response.ok) {
throw new Error(JSON.stringify(result));
}

if (result.Status === 1) {
return resolve(result);
}
data = result.Token;

if (result.Status === 2) {
throw new Error('Transaction Rejected');
}
if (result.Status === 1) {
return resolve(result);
}

// Add a timeout
if (attempts === maxAttempts) {
throw new Error('Timeout');
}
if (result.Status === 2) {
throw new Error("Transaction Rejected");

Check failure on line 35 in web/frontend/src/pages/form/components/utils/TransactionPoll.ts

View workflow job for this annotation

GitHub Actions / Web frontend Lint

Replace `"Transaction·Rejected"` with `'Transaction·Rejected'`
}

setTimeout(executePoll, interval, resolve, reject);
} catch (e) {
return reject(e);
// Add a timeout
if (attempts === maxAttempts) {
throw new Error("Timeout");

Check failure on line 40 in web/frontend/src/pages/form/components/utils/TransactionPoll.ts

View workflow job for this annotation

GitHub Actions / Web frontend Lint

Replace `"Timeout"` with `'Timeout'`
}

setTimeout(executePoll, interval, resolve, reject);
};

return new Promise(executePoll);
Expand Down

0 comments on commit e80a2d9

Please sign in to comment.