Merged in fix/add-logging-when-cache-get-fails (pull request #3215)

fix: add logging when we fail to get cache data

* fix: add logging when we fail to get cache data


Approved-by: Linus Flood
This commit is contained in:
Joakim Jäderberg
2025-11-25 09:52:45 +00:00
parent 2ae3fcb609
commit 1ecbca40a3

View File

@@ -21,7 +21,10 @@ export async function get<T>(key: string) {
}) })
) )
if (!response || !response.ok || error) { const isJson = response?.headers
.get("content-type")
?.includes("application/json")
if (!response || !response.ok || error || !isJson) {
if (response?.status === 404) { if (response?.status === 404) {
cacheLogger.debug( cacheLogger.debug(
`Miss '${key}' took ${(performance.now() - perf).toFixed(2)}ms` `Miss '${key}' took ${(performance.now() - perf).toFixed(2)}ms`
@@ -29,11 +32,19 @@ export async function get<T>(key: string) {
return undefined return undefined
} }
cacheLogger.error(`Failed to get cache for key ${key}`, {
cacheKey: key,
statusCode: response?.status,
statusText: response?.statusText,
contentType: response?.headers.get("content-type"),
})
Sentry.captureException(error ?? new Error("Unable to GET cachekey"), { Sentry.captureException(error ?? new Error("Unable to GET cachekey"), {
extra: { extra: {
cacheKey: key, cacheKey: key,
statusCode: response?.status, statusCode: response?.status,
statusText: response?.statusText, statusText: response?.statusText,
contentType: response?.headers.get("content-type"),
}, },
}) })
return undefined return undefined
@@ -45,8 +56,8 @@ export async function get<T>(key: string) {
if (jsonError) { if (jsonError) {
cacheLogger.error("Failed to parse cache response", { cacheLogger.error("Failed to parse cache response", {
key, cacheKey: key,
error: jsonError, error: serializeError(jsonError),
}) })
await deleteKey(key) await deleteKey(key)
@@ -59,3 +70,23 @@ export async function get<T>(key: string) {
return data?.data return data?.data
} }
function serializeError(error: unknown):
| string
| {
name: string
message: string
stack?: string
cause?: unknown
} {
if (error instanceof Error) {
return {
name: error.name,
message: error.message,
stack: error.stack,
cause: error.cause ? serializeError(error.cause) : undefined,
}
} else {
return String(error)
}
}