refactor: Improve error logging/reporting for cli (#4691)

* use response error classes instead of `ResponseError` everywhere

* improve error logging in dev mode or when telemetry is disabled
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2022-11-22 14:00:36 +01:00
committed by GitHub
parent 5364e7fc92
commit 0b754a4f85
29 changed files with 253 additions and 393 deletions

View File

@@ -54,7 +54,7 @@ EECredentialsController.get(
const includeDecryptedData = req.query.includeData === 'true';
if (Number.isNaN(Number(credentialId))) {
throw new ResponseHelper.ResponseError(`Credential ID must be a number.`, undefined, 400);
throw new ResponseHelper.BadRequestError(`Credential ID must be a number.`);
}
let credential = (await EECredentials.get(
@@ -63,17 +63,15 @@ EECredentialsController.get(
)) as CredentialsEntity & CredentialWithSharings;
if (!credential) {
throw new ResponseHelper.ResponseError(
throw new ResponseHelper.NotFoundError(
'Could not load the credential. If you think this is an error, ask the owner to share it with you again',
undefined,
404,
);
}
const userSharing = credential.shared?.find((shared) => shared.user.id === req.user.id);
if (!userSharing && req.user.globalRole.name !== 'owner') {
throw new ResponseHelper.ResponseError(`Forbidden.`, undefined, 403);
throw new ResponseHelper.UnauthorizedError(`Forbidden.`);
}
credential = EECredentials.addOwnerAndSharings(credential);
@@ -117,7 +115,7 @@ EECredentialsController.post(
if (!ownsCredential) {
const sharing = await EECredentials.getSharing(req.user, credentials.id);
if (!sharing) {
throw new ResponseHelper.ResponseError(`Forbidden`, undefined, 403);
throw new ResponseHelper.UnauthorizedError(`Forbidden`);
}
const decryptedData = await EECredentials.decrypt(encryptionKey, sharing.credentials);
@@ -144,13 +142,13 @@ EECredentialsController.put(
!Array.isArray(shareWithIds) ||
!shareWithIds.every((userId) => typeof userId === 'string')
) {
throw new ResponseHelper.ResponseError('Bad request', undefined, 400);
throw new ResponseHelper.BadRequestError('Bad request');
}
const { ownsCredential, credential } = await EECredentials.isOwned(req.user, credentialId);
if (!ownsCredential || !credential) {
throw new ResponseHelper.ResponseError('Forbidden', undefined, 403);
throw new ResponseHelper.UnauthorizedError('Forbidden');
}
let amountRemoved: number | null = null;