Skip to content

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

feat(northflank): enhance error handling for Northflank API responses

Victor Santoscf7421df87d8
diff --git a/backend/src/services/secret-sync/northflank/northflank-sync-fns.ts b/backend/src/services/secret-sync/northflank/northflank-sync-fns.ts
index bfe845c5ea..396aa9ac8f 100644
--- a/backend/src/services/secret-sync/northflank/northflank-sync-fns.ts
+++ b/backend/src/services/secret-sync/northflank/northflank-sync-fns.ts
@@ -1,3 +1,5 @@
+import { AxiosError } from "axios";
+
import { request } from "@app/lib/config/request";
import { matchesSchema } from "@app/services/secret-sync/secret-sync-fns";
import { TSecretMap } from "@app/services/secret-sync/secret-sync-types";
@@ -7,6 +9,46 @@ import { TNorthflankSyncWithCredentials } from "./northflank-sync-types";
const NORTHFLANK_API_URL = "https://api.northflank.com";
+const buildNorthflankAPIErrorMessage = (error: unknown): string => {
+ let errorMessage = "Northflank API returned an error.";
+
+ if (error && typeof error === "object" && "response" in error) {
+ const axiosError = error as AxiosError;
+
+ if (axiosError.response?.data) {
+ // This is the shape of the error response from the Northflank API
+ const responseData = axiosError.response.data as {
+ error?: { message?: string; details?: Record<string, string[]> };
+ message?: string;
+ };
+ const errorParts = [];
+
+ if (responseData.error?.message) {
+ errorParts.push(responseData.error.message);
+ } else if (responseData.message) {
+ errorParts.push(responseData.message);
+ }
+
+ if (responseData.error?.details) {
+ const { details } = responseData.error;
+
+ // Flatten the details object into a string
+ Object.entries(details).forEach(([field, fieldErrors]) => {
+ if (Array.isArray(fieldErrors)) {
+ fieldErrors.forEach((fieldError) => errorParts.push(`${field}: ${fieldError}`));
+ } else {
+ errorParts.push(`${field}: ${String(fieldErrors)}`);
+ }
+ });
+ }
+
+ errorMessage += ` ${errorParts.join(". ")}`;
+ }
+ }
+
+ return errorMessage;
+};
+
const getNorthflankSecrets = async (secretSync: TNorthflankSyncWithCredentials): Promise<Record<string, string>> => {
const {
destinationConfig: { projectId, secretGroupId },
@@ -39,7 +81,7 @@ const getNorthflankSecrets = async (secretSync: TNorthflankSyncWithCredentials):
} catch (error: unknown) {
throw new SecretSyncError({
error,
- message: "Failed to fetch Northflank secrets"
+ message: `Failed to fetch Northflank secrets. ${buildNorthflankAPIErrorMessage(error)}`
});
}
};
@@ -73,7 +115,7 @@ const updateNorthflankSecrets = async (
} catch (error: unknown) {
throw new SecretSyncError({
error,
- message: "Failed to update Northflank secrets"
+ message: `Failed to update Northflank secrets. ${buildNorthflankAPIErrorMessage(error)}`
});
}
};