misc: added proper handling for gateway name conflicts

This commit is contained in:
Sheen Capadngan
2025-09-04 19:16:50 +08:00
parent 4d22030bb8
commit 4f7b0eaa0d
@@ -6,7 +6,8 @@ import * as x509 from "@peculiar/x509";
import { TProxies } from "@app/db/schemas";
import { PgSqlLock } from "@app/keystore/keystore";
import { crypto } from "@app/lib/crypto";
import { BadRequestError, NotFoundError } from "@app/lib/errors";
import { DatabaseErrorCode } from "@app/lib/error-codes";
import { BadRequestError, DatabaseError, NotFoundError } from "@app/lib/errors";
import { GatewayProxyProtocol } from "@app/lib/gateway/types";
import { withGatewayV2Proxy } from "@app/lib/gateway-v2/gateway-v2";
import { OrgServiceActor } from "@app/lib/types";
@@ -439,6 +440,7 @@ export const gatewayV2ServiceFactory = ({
throw new NotFoundError({ message: `Proxy ${proxyName} not found` });
}
try {
const [gateway] = await gatewayV2DAL.upsert(
[
{
@@ -525,6 +527,13 @@ export const gatewayV2ServiceFactory = ({
serverCAPublicKey: proxyCredentials.serverCAPublicKey
}
};
} catch (err) {
if (err instanceof DatabaseError && (err.error as { code: string })?.code === DatabaseErrorCode.UniqueViolation) {
throw new BadRequestError({ message: `Gateway with name "${name}" already exists` });
}
throw err;
}
};
const heartbeat = async ({ orgPermission }: { orgPermission: OrgServiceActor }) => {