Skip to content

Commit

Permalink
Merge pull request #3248 from OpenNeuroOrg/cache-miss-exception
Browse files Browse the repository at this point in the history
fix(server): Prevent doNotCache error when an exception occurs during a cache miss
  • Loading branch information
nellh authored Dec 3, 2024
2 parents 1d2173d + a261074 commit e18cf44
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
32 changes: 32 additions & 0 deletions packages/openneuro-server/src/cache/__tests__/item.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import CacheItem from "../item"
import { CacheType } from "../types"

const redisMock = vi.fn(() => ({
getBuffer: vi.fn(),
setex: vi.fn(),
set: vi.fn(),
}))

describe("CacheItem", () => {
it("should succeed when the cache miss sets doNotCache but throws an exception", async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const item = new CacheItem(redisMock() as any, CacheType.commitFiles, [
"ds000001",
"12345678",
])
let fail = true
expect(
await item.get(async (doNotCache) => {
// On the first try
if (fail) {
fail = false
doNotCache(true)
throw new Error("expected failure")
} else {
doNotCache(false)
return true
}
}),
).toBe(true)
})
})
6 changes: 4 additions & 2 deletions packages/openneuro-server/src/cache/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Redis } from "ioredis"
import * as zlib from "zlib"
import { promisify } from "util"
import type { CacheType } from "./types"
import * as Sentry from "@sentry/node"
export { CacheType } from "./types"

const compress = promisify(zlib.gzip)
Expand Down Expand Up @@ -77,9 +78,10 @@ class CacheItem {
}
return data
}
} catch {
} catch (err) {
Sentry.captureException(err)
// Keep going as though we had a cache miss if there is a problem but don't cache it
return miss()
return miss((_doNotCache: boolean) => {})
}
}
/**
Expand Down

0 comments on commit e18cf44

Please sign in to comment.