Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

improve trycatch

x4cc13d19b6f7
diff --git a/backend/src/ee/services/gateway-v2/gateway-v2-fns.ts b/backend/src/ee/services/gateway-v2/gateway-v2-fns.ts
index f5a7eff493..ddadfdf460 100644
--- a/backend/src/ee/services/gateway-v2/gateway-v2-fns.ts
+++ b/backend/src/ee/services/gateway-v2/gateway-v2-fns.ts
@@ -1,4 +1,3 @@
-import { BadRequestError } from "@app/lib/errors";
import { GatewayProxyProtocol } from "@app/lib/gateway";
import { withGatewayV2Proxy } from "@app/lib/gateway-v2/gateway-v2";
import { callTestConnection, TestConnectionResponse } from "@app/lib/gateway-v2/test-connection-rpc";
@@ -17,19 +16,27 @@ export const testConnectionWithGateway = async (
request: Record<string, unknown>,
timeoutMs: number,
signal?: AbortSignal
-): Promise<TestConnectionResponse> => {
+): Promise<TestConnectionResponse | null> => {
const [host] = await verifyHostInputValidity({ host: targetHost, isGateway: true, isDynamicSecret: false });
- const platform = await gatewayV2Service.getPlatformConnectionDetailsByGatewayId({
- gatewayId,
- targetHost: host,
- targetPort
- });
- if (!platform) throw new BadRequestError({ message: "Unable to connect to gateway" });
- return withGatewayV2Proxy((proxyPort) => callTestConnection({ port: proxyPort, body: request, timeoutMs, signal }), {
- protocol: GatewayProxyProtocol.ConnectionTest,
- relayHost: platform.relayHost,
- gateway: platform.gateway,
- relay: platform.relay
- });
+ try {
+ const platform = await gatewayV2Service.getPlatformConnectionDetailsByGatewayId({
+ gatewayId,
+ targetHost: host,
+ targetPort
+ });
+ if (!platform) return null;
+
+ return await withGatewayV2Proxy(
+ (proxyPort) => callTestConnection({ port: proxyPort, body: request, timeoutMs, signal }),
+ {
+ protocol: GatewayProxyProtocol.ConnectionTest,
+ relayHost: platform.relayHost,
+ gateway: platform.gateway,
+ relay: platform.relay
+ }
+ );
+ } catch {
+ return null;
+ }
};
diff --git a/backend/src/ee/services/pam-account/pam-account-service.ts b/backend/src/ee/services/pam-account/pam-account-service.ts
index 90bba91423..a5b2d74394 100644
--- a/backend/src/ee/services/pam-account/pam-account-service.ts
+++ b/backend/src/ee/services/pam-account/pam-account-service.ts
@@ -314,22 +314,17 @@ export const pamAccountServiceFactory = (deps: TPamAccountServiceFactoryDep) =>
? { mode: TestConnectionMode.Tcp }
: buildRequest(connectionDetails, credentials);
- let result;
- try {
- result = await testConnectionWithGateway(
- cd.host,
- cd.port,
- gatewayId,
- gatewayV2Service,
- request,
- CONNECTION_TEST_TIMEOUT_MS
- );
- } catch {
- // gateway is unreachable or predates the connection-test protocol; don't block the write
- return;
- }
+ const result = await testConnectionWithGateway(
+ cd.host,
+ cd.port,
+ gatewayId,
+ gatewayV2Service,
+ request,
+ CONNECTION_TEST_TIMEOUT_MS
+ );
- if (!result.ok) {
+ // a null result means the gateway couldn't be reached (offline / pre-protocol) — skip rather than block
+ if (result && !result.ok) {
throw new BadRequestError({ message: `Connection test failed: ${result.errorMessage}` });
}
};