From b68c019fafbef22fe2bd501e6de6161308ad22b8 Mon Sep 17 00:00:00 2001 From: dnlbui Date: Wed, 25 Sep 2024 23:34:50 -0500 Subject: [PATCH 1/3] feat: Add error handling for stake_info query when valid but no account found --- api/handlers/node.ts | 18 ++++++++++++++++-- hooks/fetcher.ts | 2 ++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/api/handlers/node.ts b/api/handlers/node.ts index 40b9c4c5..f149071a 100644 --- a/api/handlers/node.ts +++ b/api/handlers/node.ts @@ -128,8 +128,22 @@ export default function configureNodeHandlers(apiRouter: Router) { badRequestResponse(res, 'No address provided'); return; } - console.log('executing operator-cli status...'); - const output = execFileSync('operator-cli', ['stake_info', address], { encoding: 'utf8' }) + + + console.log('Executing operator-cli stake_info...'); + const output = execFileSync('operator-cli', ['stake_info', address], { encoding: 'utf8' }); + + // Check for specific error message + // Case where query was valid but no stake info was found + if (output.includes('Error: No stake information found')) { + console.log('Entered error handling'); + // Check if the response headers have already been sent + if (!res.headersSent) { + console.log('Sending 500 response: Account not found'); + res.status(500).json({ errorMessage: 'Account not found. Please ensure the account has been created and funded.', errorDetails: '' }); + } + return; + } const yamlData = yaml.load(output); res.json(yamlData); }) diff --git a/hooks/fetcher.ts b/hooks/fetcher.ts index 8587405a..56a4ed48 100644 --- a/hooks/fetcher.ts +++ b/hooks/fetcher.ts @@ -35,6 +35,8 @@ export const fetcher = async (input: RequestInfo | URL, const data = await res.json(); if (res.status === 403) { authService.logout(apiBase); + } else if (input.toString().includes('account') && res.status === 500 && data.errorMessage === `Account not found. Please ensure the account has been created and funded.`) { + throw new Error('No stake information found. Please fund the account and try again.'); } else if (res.status === 500) { showErrorMessage('Sorry, something went wrong. Please report this issue to our support team so we can investigate and resolve the problem.'); return; From 91c2f20580bcceb453f83897daea7ad8a1d7873c Mon Sep 17 00:00:00 2001 From: dnlbui Date: Thu, 26 Sep 2024 11:25:07 -0500 Subject: [PATCH 2/3] update error status and update error string --- api/handlers/node.ts | 8 +++----- hooks/fetcher.ts | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/api/handlers/node.ts b/api/handlers/node.ts index f149071a..eaa0986e 100644 --- a/api/handlers/node.ts +++ b/api/handlers/node.ts @@ -131,16 +131,14 @@ export default function configureNodeHandlers(apiRouter: Router) { console.log('Executing operator-cli stake_info...'); - const output = execFileSync('operator-cli', ['stake_info', address], { encoding: 'utf8' }); + const output = execFileSync('operator-cli', ['stake_info', address], { encoding: 'utf8' }) // Check for specific error message // Case where query was valid but no stake info was found - if (output.includes('Error: No stake information found')) { - console.log('Entered error handling'); + if (output === 'No stake information found. Please fund the account and try again.') { // Check if the response headers have already been sent if (!res.headersSent) { - console.log('Sending 500 response: Account not found'); - res.status(500).json({ errorMessage: 'Account not found. Please ensure the account has been created and funded.', errorDetails: '' }); + res.status(404).json({ errorMessage: 'Account not found. Please ensure the account has been funded.', errorDetails: '' }); } return; } diff --git a/hooks/fetcher.ts b/hooks/fetcher.ts index 56a4ed48..e38b464a 100644 --- a/hooks/fetcher.ts +++ b/hooks/fetcher.ts @@ -35,7 +35,7 @@ export const fetcher = async (input: RequestInfo | URL, const data = await res.json(); if (res.status === 403) { authService.logout(apiBase); - } else if (input.toString().includes('account') && res.status === 500 && data.errorMessage === `Account not found. Please ensure the account has been created and funded.`) { + } else if (input.toString().includes('account') && res.status === 404 && data.errorMessage === `Account not found. Please ensure the account has been funded.`) { throw new Error('No stake information found. Please fund the account and try again.'); } else if (res.status === 500) { showErrorMessage('Sorry, something went wrong. Please report this issue to our support team so we can investigate and resolve the problem.'); From c9f8fcde3196d237988bd2874a62148555f2d2d1 Mon Sep 17 00:00:00 2001 From: dnlbui Date: Thu, 26 Sep 2024 18:52:05 -0500 Subject: [PATCH 3/3] remove changes since refactored CLI to not throw error for this task --- api/handlers/node.ts | 12 ------------ hooks/fetcher.ts | 2 -- 2 files changed, 14 deletions(-) diff --git a/api/handlers/node.ts b/api/handlers/node.ts index eaa0986e..c7fada0c 100644 --- a/api/handlers/node.ts +++ b/api/handlers/node.ts @@ -128,20 +128,8 @@ export default function configureNodeHandlers(apiRouter: Router) { badRequestResponse(res, 'No address provided'); return; } - - console.log('Executing operator-cli stake_info...'); const output = execFileSync('operator-cli', ['stake_info', address], { encoding: 'utf8' }) - - // Check for specific error message - // Case where query was valid but no stake info was found - if (output === 'No stake information found. Please fund the account and try again.') { - // Check if the response headers have already been sent - if (!res.headersSent) { - res.status(404).json({ errorMessage: 'Account not found. Please ensure the account has been funded.', errorDetails: '' }); - } - return; - } const yamlData = yaml.load(output); res.json(yamlData); }) diff --git a/hooks/fetcher.ts b/hooks/fetcher.ts index e38b464a..8587405a 100644 --- a/hooks/fetcher.ts +++ b/hooks/fetcher.ts @@ -35,8 +35,6 @@ export const fetcher = async (input: RequestInfo | URL, const data = await res.json(); if (res.status === 403) { authService.logout(apiBase); - } else if (input.toString().includes('account') && res.status === 404 && data.errorMessage === `Account not found. Please ensure the account has been funded.`) { - throw new Error('No stake information found. Please fund the account and try again.'); } else if (res.status === 500) { showErrorMessage('Sorry, something went wrong. Please report this issue to our support team so we can investigate and resolve the problem.'); return;