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 -6
View File
@@ -2,7 +2,7 @@ import { Knex } from "knex";
import { TDbClient } from "@app/db";
import { ProjectsSchema, ProjectUpgradeStatus, ProjectVersion, TableName, TProjectsUpdate } from "@app/db/schemas";
import { BadRequestError, DatabaseError } from "@app/lib/errors";
import { BadRequestError, DatabaseError, NotFoundError, UnauthorizedError } from "@app/lib/errors";
import { ormify, selectAllTableCols, sqlNestRelationships } from "@app/lib/knex";
import { Filter, ProjectFilterType } from "./project-types";
@@ -186,7 +186,7 @@ export const projectDALFactory = (db: TDbClient) => {
})?.[0];
if (!project) {
throw new BadRequestError({ message: "Project not found" });
throw new NotFoundError({ message: "Project not found" });
}
return project;
@@ -198,7 +198,7 @@ export const projectDALFactory = (db: TDbClient) => {
const findProjectBySlug = async (slug: string, orgId: string | undefined) => {
try {
if (!orgId) {
throw new BadRequestError({ message: "Organization ID is required when querying with slugs" });
throw new UnauthorizedError({ message: "Organization ID is required when querying with slugs" });
}
const projects = await db
@@ -235,7 +235,7 @@ export const projectDALFactory = (db: TDbClient) => {
})?.[0];
if (!project) {
throw new BadRequestError({ message: "Project not found" });
throw new NotFoundError({ message: "Project not found" });
}
return project;
@@ -251,7 +251,7 @@ export const projectDALFactory = (db: TDbClient) => {
}
if (filter.type === ProjectFilterType.SLUG) {
if (!filter.orgId) {
throw new BadRequestError({
throw new UnauthorizedError({
message: "Organization ID is required when querying with slugs"
});
}
@@ -295,7 +295,7 @@ export const projectDALFactory = (db: TDbClient) => {
.first();
if (!project) {
throw new BadRequestError({ message: "Project not found" });
throw new NotFoundError({ message: "Project not found" });
}
return {
+2 -2
View File
@@ -2,7 +2,7 @@ import crypto from "crypto";
import { ProjectVersion, TProjects } from "@app/db/schemas";
import { decryptAsymmetric, encryptAsymmetric } from "@app/lib/crypto";
import { BadRequestError } from "@app/lib/errors";
import { NotFoundError } from "@app/lib/errors";
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
import { TProjectDALFactory } from "@app/services/project/project-dal";
@@ -75,7 +75,7 @@ export const getProjectKmsCertificateKeyId = async ({
const keyId = await projectDAL.transaction(async (tx) => {
const project = await projectDAL.findOne({ id: projectId }, tx);
if (!project) {
throw new BadRequestError({ message: "Project not found" });
throw new NotFoundError({ message: "Project not found" });
}
if (!project.kmsCertificateKeyId) {
@@ -175,7 +175,7 @@ export const projectServiceFactory = ({
const kms = await kmsService.getKmsById(kmsKeyId, tx);
if (kms.orgId !== organization.id) {
throw new BadRequestError({
throw new ForbiddenRequestError({
message: "KMS does not belong in the organization"
});
}
@@ -321,7 +321,7 @@ export const projectServiceFactory = ({
// If identity org membership not found, throw error
if (!identityOrgMembership) {
throw new BadRequestError({
throw new NotFoundError({
message: `Failed to find identity with id ${actorId}`
});
}
@@ -490,7 +490,7 @@ export const projectServiceFactory = ({
}: TUpdateProjectVersionLimitDTO) => {
const project = await projectDAL.findProjectBySlug(workspaceSlug, actorOrgId);
if (!project) {
throw new BadRequestError({
throw new NotFoundError({
message: "Project not found"
});
}
@@ -504,7 +504,9 @@ export const projectServiceFactory = ({
);
if (!hasRole(ProjectMembershipRole.Admin))
throw new BadRequestError({ message: "Only admins are allowed to take this action" });
throw new ForbiddenRequestError({
message: "Insufficient privileges, only admins are allowed to take this action"
});
return projectDAL.updateById(project.id, { pitVersionLimit });
};
@@ -533,7 +535,9 @@ export const projectServiceFactory = ({
);
if (!hasRole(ProjectMembershipRole.Admin)) {
throw new BadRequestError({ message: "Only admins are allowed to take this action" });
throw new ForbiddenRequestError({
message: "Insufficient privileges, only admins are allowed to take this action"
});
}
const plan = await licenseService.getPlan(project.orgId);
@@ -624,7 +628,7 @@ export const projectServiceFactory = ({
const project = await projectDAL.findProjectById(projectId);
if (!project) {
throw new BadRequestError({
throw new NotFoundError({
message: `Project with id ${projectId} not found`
});
}
@@ -982,7 +986,7 @@ export const projectServiceFactory = ({
ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionActions.Edit, ProjectPermissionSub.Settings);
if (slackIntegration.orgId !== project.orgId) {
throw new BadRequestError({
throw new ForbiddenRequestError({
message: "Selected slack integration is not in the same organization"
});
}