feat(api): improve errors and error handling

This commit is contained in:
Daniel Hougaard
2024-09-27 20:02:43 +04:00
parent 02c51b05b6
commit 2d51ed317f
99 changed files with 922 additions and 847 deletions
@@ -6,7 +6,7 @@ import {
infisicalSymmetricDecrypt,
infisicalSymmetricEncypt
} from "@app/lib/crypto/encryption";
import { BadRequestError } from "@app/lib/errors";
import { NotFoundError } from "@app/lib/errors";
import { TProjectBotDALFactory } from "@app/services/project-bot/project-bot-dal";
import { TProjectDALFactory } from "../project/project-dal";
@@ -27,7 +27,7 @@ export const getBotKeyFnFactory = (
const getBotKeyFn = async (projectId: string) => {
const project = await projectDAL.findById(projectId);
if (!project)
throw new BadRequestError({
throw new NotFoundError({
message: "Project not found during bot lookup. Are you sure you are using the correct project ID?"
});
@@ -39,7 +39,7 @@ export const getBotKeyFnFactory = (
if (!bot || !bot.isActive || !bot.encryptedProjectKey || !bot.encryptedProjectKeyNonce) {
// trying to set bot automatically
const projectV1Keys = await projectBotDAL.findProjectUserWorkspaceKey(projectId);
if (!projectV1Keys) throw new BadRequestError({ message: "Bot not found. Please ask admin user to login" });
if (!projectV1Keys) throw new NotFoundError({ message: "Bot not found. Please ask admin user to login" });
let userPrivateKey = "";
if (
@@ -5,7 +5,7 @@ import { TPermissionServiceFactory } from "@app/ee/services/permission/permissio
import { ProjectPermissionActions, ProjectPermissionSub } from "@app/ee/services/permission/project-permission";
import { generateAsymmetricKeyPair } from "@app/lib/crypto";
import { infisicalSymmetricEncypt } from "@app/lib/crypto/encryption";
import { BadRequestError } from "@app/lib/errors";
import { BadRequestError, NotFoundError } from "@app/lib/errors";
import { TProjectDALFactory } from "../project/project-dal";
import { TProjectBotDALFactory } from "./project-bot-dal";
@@ -91,7 +91,7 @@ export const projectBotServiceFactory = ({
const bot = await projectBotDAL.findProjectByBotId(botId);
return bot;
} catch (e) {
throw new BadRequestError({ message: "Failed to find bot by ID" });
throw new NotFoundError({ message: "Failed to find bot by ID" });
}
};
@@ -105,7 +105,7 @@ export const projectBotServiceFactory = ({
isActive
}: TSetActiveStateDTO) => {
const bot = await projectBotDAL.findById(botId);
if (!bot) throw new BadRequestError({ message: "Bot not found" });
if (!bot) throw new NotFoundError({ message: "Bot not found" });
const { permission } = await permissionService.getProjectPermission(
actor,
@@ -119,7 +119,7 @@ export const projectBotServiceFactory = ({
const project = await projectBotDAL.findProjectByBotId(botId);
if (!project) {
throw new BadRequestError({ message: "Failed to find project by bot ID" });
throw new NotFoundError({ message: "Failed to find project by bot ID" });
}
if (project.version === ProjectVersion.V2) {
@@ -128,7 +128,7 @@ export const projectBotServiceFactory = ({
if (isActive) {
if (!botKey?.nonce || !botKey?.encryptedKey) {
throw new BadRequestError({ message: "Failed to set bot active - missing bot key" });
throw new NotFoundError({ message: "Bot key not found, failed to set bot active" });
}
const doc = await projectBotDAL.updateById(botId, {
isActive: true,