diff --git a/backend/src/lib/error-codes/database.ts b/backend/src/lib/error-codes/database.ts new file mode 100644 index 000000000..1a8dd41ae --- /dev/null +++ b/backend/src/lib/error-codes/database.ts @@ -0,0 +1,4 @@ +export enum DatabaseErrorCode { + ForeignKeyViolation = "23503", + UniqueViolation = "23505" +} diff --git a/backend/src/lib/error-codes/index.ts b/backend/src/lib/error-codes/index.ts new file mode 100644 index 000000000..c30cd664d --- /dev/null +++ b/backend/src/lib/error-codes/index.ts @@ -0,0 +1 @@ +export * from "./database"; diff --git a/backend/src/services/app-connection/app-connection-service.ts b/backend/src/services/app-connection/app-connection-service.ts index 04cf20e4a..8beaec21d 100644 --- a/backend/src/services/app-connection/app-connection-service.ts +++ b/backend/src/services/app-connection/app-connection-service.ts @@ -3,6 +3,7 @@ import { ForbiddenError, subject } from "@casl/ability"; import { OrgPermissionAppConnectionActions, OrgPermissionSubjects } from "@app/ee/services/permission/org-permission"; import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service"; import { generateHash } from "@app/lib/crypto/encryption"; +import { DatabaseErrorCode } from "@app/lib/error-codes"; import { BadRequestError, DatabaseError, NotFoundError } from "@app/lib/errors"; import { DiscriminativePick, OrgServiceActor } from "@app/lib/types"; import { AppConnection } from "@app/services/app-connection/app-connection-enums"; @@ -172,7 +173,7 @@ export const appConnectionServiceFactory = ({ credentials: validatedCredentials } as TAppConnection; } catch (err) { - if (err instanceof DatabaseError && (err.error as { code: string })?.code === "23505") { + if (err instanceof DatabaseError && (err.error as { code: string })?.code === DatabaseErrorCode.UniqueViolation) { throw new BadRequestError({ message: `An App Connection with the name "${params.name}" already exists` }); } @@ -244,7 +245,7 @@ export const appConnectionServiceFactory = ({ return await decryptAppConnection(updatedConnection, kmsService); } catch (err) { - if (err instanceof DatabaseError && (err.error as { code: string })?.code === "23505") { + if (err instanceof DatabaseError && (err.error as { code: string })?.code === DatabaseErrorCode.UniqueViolation) { throw new BadRequestError({ message: `An App Connection with the name "${params.name}" already exists` }); } @@ -280,7 +281,10 @@ export const appConnectionServiceFactory = ({ return await decryptAppConnection(deletedAppConnection, kmsService); } catch (err) { - if (err instanceof DatabaseError && (err.error as { code: string })?.code === "23503") { + if ( + err instanceof DatabaseError && + (err.error as { code: string })?.code === DatabaseErrorCode.ForeignKeyViolation + ) { throw new BadRequestError({ message: "Cannot delete App Connection with existing connections. Remove all existing connections and try again." diff --git a/backend/src/services/secret-sync/secret-sync-service.ts b/backend/src/services/secret-sync/secret-sync-service.ts index a855f5dfc..8211180e8 100644 --- a/backend/src/services/secret-sync/secret-sync-service.ts +++ b/backend/src/services/secret-sync/secret-sync-service.ts @@ -8,6 +8,7 @@ import { ProjectPermissionSub } from "@app/ee/services/permission/project-permission"; import { KeyStorePrefixes, TKeyStoreFactory } from "@app/keystore/keystore"; +import { DatabaseErrorCode } from "@app/lib/error-codes"; import { BadRequestError, DatabaseError, NotFoundError } from "@app/lib/errors"; import { OrgServiceActor } from "@app/lib/types"; import { TAppConnectionServiceFactory } from "@app/services/app-connection/app-connection-service"; @@ -209,7 +210,7 @@ export const secretSyncServiceFactory = ({ return secretSync as TSecretSync; } catch (err) { - if (err instanceof DatabaseError && (err.error as { code: string })?.code === "23505") { + if (err instanceof DatabaseError && (err.error as { code: string })?.code === DatabaseErrorCode.UniqueViolation) { throw new BadRequestError({ message: `A Secret Sync with the name "${params.name}" already exists for the project with ID "${folder.projectId}"` }); @@ -300,7 +301,7 @@ export const secretSyncServiceFactory = ({ return updatedSecretSync as TSecretSync; } catch (err) { - if (err instanceof DatabaseError && (err.error as { code: string })?.code === "23505") { + if (err instanceof DatabaseError && (err.error as { code: string })?.code === DatabaseErrorCode.UniqueViolation) { throw new BadRequestError({ message: `A Secret Sync with the name "${params.name}" already exists for the project with ID "${secretSync.projectId}"` });