Skip to content

Commit

Permalink
fix: Fix error handling on tracking operations [DEV-3527] (#461)
Browse files Browse the repository at this point in the history
* fix: Fix error handling on tracking operations

* Silently log tracking errors

* Remove duplicate return statements

---------

Co-authored-by: Andrew Nikitin <[email protected]>
  • Loading branch information
DaevMithran and Andrew Nikitin authored Jan 11, 2024
1 parent 814c2f2 commit 53d7dfd
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 70 deletions.
53 changes: 20 additions & 33 deletions src/controllers/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
45 changes: 24 additions & 21 deletions src/controllers/credential-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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}`);
}
}

Expand Down Expand Up @@ -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}`);
}
}

Expand Down
30 changes: 18 additions & 12 deletions src/controllers/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}`);
}
}

Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions src/controllers/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 53d7dfd

Please sign in to comment.