Skip to content

Commit

Permalink
Remove catch error-handling in favor of error-handling middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
dangtony98 committed Jun 25, 2023
1 parent 9bdecaf commit ca3f7ba
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 65 deletions.
64 changes: 7 additions & 57 deletions backend/src/controllers/v2/secretController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@ export const createSecret = async (req: Request, res: Response) => {
keyEncoding: ENCODING_SCHEME_UTF8
};

const secret = await Secret.create(sanitizedSecret).catch((err) => {
if (err instanceof ValidationError) {
throw RouteValidationError({ message: err.message, stack: err.stack });
}
throw err;
});
const secret = await new Secret(sanitizedSecret).save();

if (postHogClient) {
postHogClient.capture({
Expand Down Expand Up @@ -117,19 +112,7 @@ export const createSecrets = async (req: Request, res: Response) => {
sanitizedSecretesToCreate.push(safeUpdateFields);
});

const secrets = await Secret.insertMany(sanitizedSecretesToCreate).catch((bulkCreateError) => {
if (bulkCreateError instanceof ValidationError) {
throw RouteValidationError({
message: bulkCreateError.message,
stack: bulkCreateError.stack
});
}

throw InternalServerError({
message: "Unable to process your batch create request. Please try again",
stack: bulkCreateError ? bulkCreateError.stack : undefined
});
});
const secrets = await Secret.insertMany(sanitizedSecretesToCreate);

if (postHogClient) {
postHogClient.capture({
Expand Down Expand Up @@ -160,12 +143,7 @@ export const deleteSecrets = async (req: Request, res: Response) => {
const { workspaceId, environmentName } = req.params;
const secretIdsToDelete: string[] = req.body.secretIds;

const secretIdsUserCanDelete = await Secret.find({ workspace: workspaceId, environment: environmentName }, { _id: 1 })
.catch((secretIdsUserCanDeleteError) => {
throw InternalServerError({
message: `Unable to fetch secrets you own: [error=${secretIdsUserCanDeleteError.message}]`
});
});
const secretIdsUserCanDelete = await Secret.find({ workspace: workspaceId, environment: environmentName }, { _id: 1 });

const secretsUserCanDeleteSet: Set<string> = new Set(
secretIdsUserCanDelete.map((objectId) => objectId._id.toString())
Expand All @@ -187,17 +165,7 @@ export const deleteSecrets = async (req: Request, res: Response) => {
}
});

await Secret.bulkWrite(deleteOperationsToPerform)
.catch((bulkDeleteError) => {
if (bulkDeleteError instanceof ValidationError) {
throw RouteValidationError({
message: "Unable to apply modifications, please try again",
stack: bulkDeleteError.stack
})
}

throw InternalServerError();
})
await Secret.bulkWrite(deleteOperationsToPerform);

if (postHogClient) {
postHogClient.capture({
Expand Down Expand Up @@ -254,10 +222,7 @@ export const updateSecrets = async (req: Request, res: Response) => {
const postHogClient = await TelemetryService.getPostHogClient();
const { workspaceId, environmentName } = req.params;
const secretsModificationsRequested: ModifySecretRequestBody[] = req.body.secrets;
const secretIdsUserCanModify = await Secret.find({ workspace: workspaceId, environment: environmentName }, { _id: 1 })
.catch(() => {
throw InternalServerError({ message: "Unable to fetch secrets you own" })
})
const secretIdsUserCanModify = await Secret.find({ workspace: workspaceId, environment: environmentName }, { _id: 1 });

const secretsUserCanModifySet: Set<string> = new Set(
secretIdsUserCanModify.map((objectId) => objectId._id.toString())
Expand Down Expand Up @@ -295,17 +260,7 @@ export const updateSecrets = async (req: Request, res: Response) => {
}
});

await Secret.bulkWrite(updateOperationsToPerform)
.catch((bulkModificationInfoError) => {
if (bulkModificationInfoError instanceof ValidationError) {
throw RouteValidationError({
message: "Unable to apply modifications, please try again",
stack: bulkModificationInfoError.stack
});
}

throw InternalServerError();
});
await Secret.bulkWrite(updateOperationsToPerform);

if (postHogClient) {
postHogClient.capture({
Expand Down Expand Up @@ -335,12 +290,7 @@ export const updateSecret = async (req: Request, res: Response) => {
const { workspaceId, environmentName } = req.params;
const secretModificationsRequested: ModifySecretRequestBody = req.body.secret;

const secretIdUserCanModify = await Secret.findOne({ workspace: workspaceId, environment: environmentName }, { _id: 1 })
.catch((secretIdUserCanModifyError) => {
if (secretIdUserCanModifyError && !secretIdUserCanModify) {
throw BadRequestError();
}
});
const secretIdUserCanModify = await Secret.findOne({ workspace: workspaceId, environment: environmentName }, { _id: 1 });

const sanitizedSecret: SanitizedSecretModify = {
secretKeyCiphertext: secretModificationsRequested.secretKeyCiphertext,
Expand Down
9 changes: 1 addition & 8 deletions backend/src/controllers/v2/tagController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,7 @@ export const createWorkspaceTag = async (req: Request, res: Response) => {
user: new Types.ObjectId(req.user._id),
};

const createdTag = await Tag.create(tagToCreate)
.catch ((err) => {
if ((err as MongoError).code === 11000) {
throw BadRequestError({ message: "Tags must be unique in a workspace" });
}

throw err;
});
const createdTag = await new Tag(tagToCreate);

res.json(createdTag);
};
Expand Down

0 comments on commit ca3f7ba

Please sign in to comment.