Fix/logging warmup background * fix: logging in background functions * fix logging * fix logging * fix logging * fix: add error logging when unable to get from cache * Merge branch 'master' of bitbucket.org:scandic-swap/web into fix/logging-warmup-background Approved-by: Linus Flood
104 lines
2.5 KiB
TypeScript
104 lines
2.5 KiB
TypeScript
import * as Sentry from "@sentry/nextjs"
|
|
|
|
import { env } from "../../env/server"
|
|
import { safeTry } from "../../utils/safeTry"
|
|
import { cacheLogger } from "../logger"
|
|
import { getCacheEndpoint } from "./endpoints"
|
|
|
|
const API_KEY = env.REDIS_API_KEY ?? ""
|
|
export async function get<T>(key: string) {
|
|
const perf = performance.now()
|
|
|
|
const [response, error] = await safeTry(
|
|
fetch(getCacheEndpoint(key), {
|
|
method: "GET",
|
|
cache: "no-cache",
|
|
headers: {
|
|
"x-api-key": API_KEY,
|
|
},
|
|
signal: AbortSignal.timeout(3_000),
|
|
})
|
|
)
|
|
|
|
const isJson = response?.headers
|
|
.get("content-type")
|
|
?.includes("application/json")
|
|
if (!response || !response.ok || error || !isJson) {
|
|
if (response?.status === 404) {
|
|
cacheLogger.debug(
|
|
`Miss '${key}' took ${(performance.now() - perf).toFixed(2)}ms`
|
|
)
|
|
return undefined
|
|
}
|
|
|
|
cacheLogger.error(`Failed to get cache for key '${key}'`, {
|
|
cacheKey: key,
|
|
statusCode: response?.status,
|
|
statusText: response?.statusText,
|
|
isJson,
|
|
contentType: response?.headers.get("content-type"),
|
|
error: serializeError(error),
|
|
})
|
|
|
|
Sentry.captureException(error ?? new Error("Unable to GET cachekey"), {
|
|
extra: {
|
|
cacheKey: key,
|
|
statusCode: response?.status,
|
|
statusText: response?.statusText,
|
|
contentType: response?.headers.get("content-type"),
|
|
},
|
|
})
|
|
return undefined
|
|
}
|
|
|
|
const [data, jsonError] = await safeTry(
|
|
response.json() as Promise<{ data: T }>
|
|
)
|
|
|
|
if (jsonError) {
|
|
cacheLogger.error(`Failed to parse cache response for key '${key}'`, {
|
|
cacheKey: key,
|
|
error: serializeError(jsonError),
|
|
})
|
|
Sentry.captureException(
|
|
jsonError ?? new Error("Unable to parse cache response"),
|
|
{
|
|
extra: {
|
|
cacheKey: key,
|
|
statusCode: response?.status,
|
|
statusText: response?.statusText,
|
|
contentType: response?.headers.get("content-type"),
|
|
},
|
|
}
|
|
)
|
|
|
|
return undefined
|
|
}
|
|
|
|
cacheLogger.debug(
|
|
`Hit '${key}' took ${(performance.now() - perf).toFixed(2)}ms`
|
|
)
|
|
|
|
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)
|
|
}
|
|
}
|