Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
fix(cache): enhance error handling in withCache utility to log warnings for invalid JSON and cache write failures
Victor Hugo dos Santos7aad0b4c774d
diff --git a/backend/src/lib/cache/with-cache.test.ts b/backend/src/lib/cache/with-cache.test.tsindex 3f01f76274..5048d32eb5 100644--- a/backend/src/lib/cache/with-cache.test.ts+++ b/backend/src/lib/cache/with-cache.test.ts@@ -195,6 +195,7 @@ describe("withCache", () => {});it("should fall back to fetcher when cached value is invalid JSON", async () => {+ const { logger } = await import("@app/lib/logger");const data = { valid: true };mockKeyStore.getItem.mockResolvedValue("not-valid-json{{{");const fetcher = vi.fn().mockResolvedValue(data);@@ -208,6 +209,10 @@ describe("withCache", () => {expect(result).toEqual(data);expect(fetcher).toHaveBeenCalledOnce();+ expect(logger.warn).toHaveBeenCalledWith(+ expect.objectContaining({ key: "bad-json-key" }),+ expect.stringContaining("cache parse failed")+ );});it("should log warnings on cache read and write failures", async () => {diff --git a/backend/src/lib/cache/with-cache.ts b/backend/src/lib/cache/with-cache.tsindex f976297e6a..901f9a1399 100644--- a/backend/src/lib/cache/with-cache.ts+++ b/backend/src/lib/cache/with-cache.ts@@ -9,20 +9,26 @@ type TWithCacheOpts<T> = {};/**- * Cache-aside helper: attempts to read from Redis, falls back to the fetcher on miss or Redis failure,- * and writes the result back to Redis. Redis errors are caught and logged — the fetcher is always the- * source of truth.+ * Cache-aside helper: attempts to read from Redis, falls back to the fetcher on miss, Redis I/O failure,+ * or invalid cached JSON, and writes the result back to Redis. Redis write errors are caught and logged+ * — the fetcher is always the source of truth.*/export const withCache = async <T>({ keyStore, key, ttlSeconds, fetcher }: TWithCacheOpts<T>): Promise<T> => {+ let cached: string | null = null;try {- const cached = await keyStore.getItem(key);- if (cached !== null) {- return JSON.parse(cached) as T;- }+ cached = await keyStore.getItem(key);} catch (err) {logger.warn({ key, err }, `withCache: cache read failed, falling back to fetcher [key=${key}]`);}+ if (cached !== null) {+ try {+ return JSON.parse(cached) as T;+ } catch (err) {+ logger.warn({ key, err }, `withCache: cache parse failed, falling back to fetcher [key=${key}]`);+ }+ }+const result = await fetcher();try {diff --git a/backend/src/services/super-admin/super-admin-service.ts b/backend/src/services/super-admin/super-admin-service.tsindex d89cae3d47..e6f600cf29 100644--- a/backend/src/services/super-admin/super-admin-service.ts+++ b/backend/src/services/super-admin/super-admin-service.ts@@ -455,7 +455,11 @@ export const superAdminServiceFactory = ({const updatedServerCfg = await serverCfgDAL.updateById(ADMIN_CONFIG_DB_UUID, updatedData);- await keyStore.setItemWithExpiry(ADMIN_CONFIG_KEY, ADMIN_CONFIG_KEY_EXP, JSON.stringify(updatedServerCfg));+ try {+ await keyStore.setItemWithExpiry(ADMIN_CONFIG_KEY, ADMIN_CONFIG_KEY_EXP, JSON.stringify(updatedServerCfg));+ } catch (err) {+ logger.warn({ key: ADMIN_CONFIG_KEY, err }, `updateServerCfg: cache write failed [key=${ADMIN_CONFIG_KEY}]`);+ }if (gitHubAppConnectionSettingsUpdated) {await $syncAdminIntegrationConfig();