improvement: add error codes enum for re-use

This commit is contained in:
Scott Wilson
2025-02-04 08:37:06 -08:00
parent 22b57b7a74
commit 5bdbf37171
4 changed files with 15 additions and 5 deletions
+4
View File
@@ -0,0 +1,4 @@
export enum DatabaseErrorCode {
ForeignKeyViolation = "23503",
UniqueViolation = "23505"
}
+1
View File
@@ -0,0 +1 @@
export * from "./database";
@@ -3,6 +3,7 @@ import { ForbiddenError, subject } from "@casl/ability";
import { OrgPermissionAppConnectionActions, OrgPermissionSubjects } from "@app/ee/services/permission/org-permission"; import { OrgPermissionAppConnectionActions, OrgPermissionSubjects } from "@app/ee/services/permission/org-permission";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service"; import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service";
import { generateHash } from "@app/lib/crypto/encryption"; import { generateHash } from "@app/lib/crypto/encryption";
import { DatabaseErrorCode } from "@app/lib/error-codes";
import { BadRequestError, DatabaseError, NotFoundError } from "@app/lib/errors"; import { BadRequestError, DatabaseError, NotFoundError } from "@app/lib/errors";
import { DiscriminativePick, OrgServiceActor } from "@app/lib/types"; import { DiscriminativePick, OrgServiceActor } from "@app/lib/types";
import { AppConnection } from "@app/services/app-connection/app-connection-enums"; import { AppConnection } from "@app/services/app-connection/app-connection-enums";
@@ -172,7 +173,7 @@ export const appConnectionServiceFactory = ({
credentials: validatedCredentials credentials: validatedCredentials
} as TAppConnection; } as TAppConnection;
} catch (err) { } 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` }); 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); return await decryptAppConnection(updatedConnection, kmsService);
} catch (err) { } 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` }); 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); return await decryptAppConnection(deletedAppConnection, kmsService);
} catch (err) { } 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({ throw new BadRequestError({
message: message:
"Cannot delete App Connection with existing connections. Remove all existing connections and try again." "Cannot delete App Connection with existing connections. Remove all existing connections and try again."
@@ -8,6 +8,7 @@ import {
ProjectPermissionSub ProjectPermissionSub
} from "@app/ee/services/permission/project-permission"; } from "@app/ee/services/permission/project-permission";
import { KeyStorePrefixes, TKeyStoreFactory } from "@app/keystore/keystore"; import { KeyStorePrefixes, TKeyStoreFactory } from "@app/keystore/keystore";
import { DatabaseErrorCode } from "@app/lib/error-codes";
import { BadRequestError, DatabaseError, NotFoundError } from "@app/lib/errors"; import { BadRequestError, DatabaseError, NotFoundError } from "@app/lib/errors";
import { OrgServiceActor } from "@app/lib/types"; import { OrgServiceActor } from "@app/lib/types";
import { TAppConnectionServiceFactory } from "@app/services/app-connection/app-connection-service"; import { TAppConnectionServiceFactory } from "@app/services/app-connection/app-connection-service";
@@ -209,7 +210,7 @@ export const secretSyncServiceFactory = ({
return secretSync as TSecretSync; return secretSync as TSecretSync;
} catch (err) { } 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({ throw new BadRequestError({
message: `A Secret Sync with the name "${params.name}" already exists for the project with ID "${folder.projectId}"` 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; return updatedSecretSync as TSecretSync;
} catch (err) { } 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({ throw new BadRequestError({
message: `A Secret Sync with the name "${params.name}" already exists for the project with ID "${secretSync.projectId}"` message: `A Secret Sync with the name "${params.name}" already exists for the project with ID "${secretSync.projectId}"`
}); });