From 53d7dfdcc23d90dba85a681f39ebc724b7fbdcc2 Mon Sep 17 00:00:00 2001 From: DaevMithran <61043607+DaevMithran@users.noreply.github.com> Date: Thu, 11 Jan 2024 18:28:36 +0530 Subject: [PATCH] fix: Fix error handling on tracking operations [DEV-3527] (#461) * fix: Fix error handling on tracking operations * Silently log tracking errors * Remove duplicate return statements --------- Co-authored-by: Andrew Nikitin --- src/controllers/account.ts | 53 +++++++++++----------------- src/controllers/credential-status.ts | 45 ++++++++++++----------- src/controllers/credentials.ts | 30 +++++++++------- src/controllers/resource.ts | 10 +++--- 4 files changed, 68 insertions(+), 70 deletions(-) diff --git a/src/controllers/account.ts b/src/controllers/account.ts index 1b0e8b95..0d21f9e7 100644 --- a/src/controllers/account.ts +++ b/src/controllers/account.ts @@ -95,41 +95,28 @@ export class AccountController { } const identityStrategySetup = new IdentityServiceStrategySetup(response.locals.customer.customerId); - let apiKey = await identityStrategySetup.agent.getAPIKey(response.locals.customer, response.locals.user); - // If there is no API key for the customer - create it - if (!apiKey) { - apiKey = await identityStrategySetup.agent.setAPIKey( - request.session.idToken, - response.locals.customer, - response.locals.user - ); - } else if (apiKey.isExpired()) { - // If API key is expired - update it - apiKey = await identityStrategySetup.agent.updateAPIKey(apiKey, request.session.idToken); - } - return response.status(StatusCodes.OK).json({ - idToken: apiKey?.apiKey, - }); - } - - public async setupDefaultRole(request: Request, response: Response) { - if (request.body) { - const { body } = request; - if (!body.user.isSuspended) { - const logToHelper = new LogToHelper(); - const _r = await logToHelper.setup(); - if (_r.status !== StatusCodes.OK) { - return response.status(StatusCodes.BAD_GATEWAY).json({ - error: _r.error, - }); - } - const resp = await logToHelper.setDefaultRoleForUser(body.user.id as string); - return response.status(resp.status).json({ - error: resp.error, - }); + try { + // Get the API key for the customer + let apiKey = await identityStrategySetup.agent.getAPIKey(response.locals.customer, response.locals.user); + // If there is no API key for the customer - create it + if (!apiKey) { + apiKey = await identityStrategySetup.agent.setAPIKey( + request.session.idToken, + response.locals.customer, + response.locals.user + ); + } else if (apiKey.isExpired()) { + // If API key is expired - update it + apiKey = await identityStrategySetup.agent.updateAPIKey(apiKey, request.session.idToken); } + return response.status(StatusCodes.OK).json({ + idToken: apiKey?.apiKey, + }); + } catch (error) { + return response.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ + error: `Internal error: ${(error as Error)?.message || error}`, + }); } - return response.status(StatusCodes.BAD_REQUEST).json({}); } public async bootstrap(request: Request, response: Response) { diff --git a/src/controllers/credential-status.ts b/src/controllers/credential-status.ts index c22c067b..9e3c410f 100644 --- a/src/controllers/credential-status.ts +++ b/src/controllers/credential-status.ts @@ -562,13 +562,14 @@ export class CredentialStatusController { }, } as ITrackOperation; - const trackResult = await identityServiceStrategySetup.agent.trackOperation(trackResourceInfo); + const trackResult = await identityServiceStrategySetup.agent + .trackOperation(trackResourceInfo) + .catch((error) => { + return { error }; + }); if (trackResult.error) { - return response - .status(StatusCodes.INTERNAL_SERVER_ERROR) - .json(trackResult as CreateEncryptedStatusListUnsuccessfulResponseBody); + console.error(`Tracking Error: ${trackResult.error}`); } - // return result return response.status(StatusCodes.OK).json({ ...result, encrypted: undefined }); } catch (error) { @@ -695,14 +696,14 @@ export class CredentialStatusController { feePaymentNetwork: CheqdNetwork.Testnet, }, } as ITrackOperation; - const trackResult = await identityServiceStrategySetup.agent.trackOperation(trackResourceInfo); - + const trackResult = await identityServiceStrategySetup.agent + .trackOperation(trackResourceInfo) + .catch((error) => { + return { error }; + }); if (trackResult.error) { - return response - .status(StatusCodes.INTERNAL_SERVER_ERROR) - .json(trackResult as CreateEncryptedStatusListUnsuccessfulResponseBody); + console.error(`Tracking Error: ${trackResult.error}`); } - // return result return response.status(StatusCodes.OK).json({ ...result, encrypted: undefined }); } catch (error) { @@ -873,12 +874,13 @@ export class CredentialStatusController { symmetricKey: '', }, } as ITrackOperation; - const trackResult = await identityServiceStrategySetup.agent.trackOperation(trackResourceInfo); + const trackResult = await identityServiceStrategySetup.agent + .trackOperation(trackResourceInfo) + .catch((error) => { + return { error }; + }); if (trackResult.error) { - return response.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ - updated: false, - error: trackResult.error, - } as UpdateUnencryptedStatusListUnsuccessfulResponseBody); + console.error(`Tracking Error: ${trackResult.error}`); } } @@ -1072,12 +1074,13 @@ export class CredentialStatusController { feePaymentNetwork: CheqdNetwork.Testnet, }, } as ITrackOperation; - const trackResult = await identityServiceStrategySetup.agent.trackOperation(trackResourceInfo); + const trackResult = await identityServiceStrategySetup.agent + .trackOperation(trackResourceInfo) + .catch((error) => { + return { error }; + }); if (trackResult.error) { - return response.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ - updated: false, - error: trackResult.error, - } as UpdateUnencryptedStatusListUnsuccessfulResponseBody); + console.error(`Tracking Error: ${trackResult.error}`); } } diff --git a/src/controllers/credentials.ts b/src/controllers/credentials.ts index 83a0221e..852879f9 100644 --- a/src/controllers/credentials.ts +++ b/src/controllers/credentials.ts @@ -331,11 +331,13 @@ export class CredentialController { } as ITrackOperation; // Track operation - const trackResult = await identityServiceStrategySetup.agent.trackOperation(trackInfo); - if (trackResult.error) { - return response.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ - error: trackResult.error, + const trackResult = await identityServiceStrategySetup.agent + .trackOperation(trackInfo) + .catch((error) => { + return { error }; }); + if (trackResult.error) { + console.error(`Tracking Error: ${trackResult.error}`); } } // Return Ok response @@ -435,11 +437,13 @@ export class CredentialController { } as ITrackOperation; // Track operation - const trackResult = await identityServiceStrategySetup.agent.trackOperation(trackInfo); - if (trackResult.error) { - return response.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ - error: trackResult.error, + const trackResult = await identityServiceStrategySetup.agent + .trackOperation(trackInfo) + .catch((error) => { + return { error }; }); + if (trackResult.error) { + console.error(`Tracking Error: ${trackResult.error}`); } } @@ -537,11 +541,13 @@ export class CredentialController { } as ITrackOperation; // Track operation - const trackResult = await identityServiceStrategySetup.agent.trackOperation(trackInfo); - if (trackResult.error) { - return response.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ - error: trackResult.error, + const trackResult = await identityServiceStrategySetup.agent + .trackOperation(trackInfo) + .catch((error) => { + return { error }; }); + if (trackResult.error) { + console.error(`Tracking Error: ${trackResult.error}`); } } // Return Ok response diff --git a/src/controllers/resource.ts b/src/controllers/resource.ts index efeb22e1..4f06862a 100644 --- a/src/controllers/resource.ts +++ b/src/controllers/resource.ts @@ -190,11 +190,13 @@ export class ResourceController { }, } as ITrackOperation; - const trackResult = await identityServiceStrategySetup.agent.trackOperation(trackResourceInfo); - if (trackResult.error) { - return response.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ - error: `${trackResult.error}`, + const trackResult = await identityServiceStrategySetup.agent + .trackOperation(trackResourceInfo) + .catch((error) => { + return { error }; }); + if (trackResult.error) { + console.error(`Tracking Error: ${trackResult.error}`); } return response.status(StatusCodes.CREATED).json({