Include more details when throwing errors for debugging in Sentry * WIP throw errors with more details for debugging in Sentry * Fix throwing response-data * Clearer message when a response fails * Add message to errors * better typings * . * Try to send profileID and membershipNumber to Sentry when we fail to parse the apiResponse * rename notFound -> notFoundError * Merge branch 'master' of bitbucket.org:scandic-swap/web into chore/add-error-details-for-sentry Approved-by: Linus Flood
135 lines
3.6 KiB
TypeScript
135 lines
3.6 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import { router } from "../../.."
|
|
import { notFoundError } from "../../../errors"
|
|
import {
|
|
GetStartPage,
|
|
GetStartPageRefs,
|
|
} from "../../../graphql/Query/StartPage/StartPage.graphql"
|
|
import { request } from "../../../graphql/request"
|
|
import { contentstackExtendedProcedureUID } from "../../../procedures"
|
|
import {
|
|
generateRefsResponseTag,
|
|
generateTag,
|
|
generateTagsFromAssetSystem,
|
|
generateTagsFromSystem,
|
|
} from "../../../utils/generateTag"
|
|
import { startPageRefsSchema, startPageSchema } from "./output"
|
|
import { getConnections, getConnectionsFromAssets } from "./utils"
|
|
|
|
import type { z } from "zod"
|
|
|
|
import type { TrackingPageData } from "../../types"
|
|
import type { blocksSchema } from "./output"
|
|
|
|
export interface GetStartPageData extends z.input<typeof startPageSchema> {}
|
|
export interface StartPage extends z.output<typeof startPageSchema> {}
|
|
|
|
export interface GetStartPageRefsSchema extends z.input<
|
|
typeof startPageRefsSchema
|
|
> {}
|
|
|
|
export type Block = z.output<typeof blocksSchema>
|
|
|
|
export const startPageQueryRouter = router({
|
|
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
|
const { lang, uid } = ctx
|
|
|
|
const getStartPageRefsCounter = createCounter(
|
|
"trpc.contentstack.startPage.get.refs"
|
|
)
|
|
const metricsGetStartPageRefs = getStartPageRefsCounter.init({ lang, uid })
|
|
|
|
metricsGetStartPageRefs.start()
|
|
|
|
const refsResponse = await request<GetStartPageRefsSchema>(
|
|
GetStartPageRefs,
|
|
{
|
|
locale: lang,
|
|
uid,
|
|
},
|
|
{
|
|
key: generateRefsResponseTag(lang, uid),
|
|
ttl: "max",
|
|
}
|
|
)
|
|
if (!refsResponse.data) {
|
|
metricsGetStartPageRefs.noDataError()
|
|
throw notFoundError({
|
|
message: "StartPage refs returned no data",
|
|
errorDetails: { lang, uid },
|
|
})
|
|
}
|
|
|
|
const validatedRefsData = startPageRefsSchema.safeParse(refsResponse.data)
|
|
|
|
if (!validatedRefsData.success) {
|
|
metricsGetStartPageRefs.validationError(validatedRefsData.error)
|
|
return null
|
|
}
|
|
|
|
metricsGetStartPageRefs.success()
|
|
|
|
const getStartPageCounter = createCounter("trpc.contentstack.startPage.get")
|
|
const metricsGetStartPage = getStartPageCounter.init({ lang, uid })
|
|
|
|
metricsGetStartPage.start()
|
|
|
|
const connections = getConnections(validatedRefsData.data)
|
|
const assetConnections = getConnectionsFromAssets(validatedRefsData.data)
|
|
|
|
const tags = [
|
|
generateTagsFromSystem(lang, connections),
|
|
generateTagsFromAssetSystem(assetConnections),
|
|
generateTag(lang, validatedRefsData.data.start_page.system.uid),
|
|
].flat()
|
|
|
|
const response = await request<GetStartPageData>(
|
|
GetStartPage,
|
|
{
|
|
locale: lang,
|
|
uid,
|
|
},
|
|
{
|
|
key: tags,
|
|
ttl: "max",
|
|
}
|
|
)
|
|
|
|
if (!response.data) {
|
|
metricsGetStartPage.noDataError()
|
|
throw notFoundError({
|
|
message: "StartPage not found",
|
|
errorDetails: { lang, uid },
|
|
})
|
|
}
|
|
|
|
const startPage = startPageSchema.safeParse(response.data)
|
|
|
|
if (!startPage.success) {
|
|
metricsGetStartPage.validationError(startPage.error)
|
|
return null
|
|
}
|
|
|
|
metricsGetStartPage.success()
|
|
|
|
const system = startPage.data.start_page.system
|
|
const tracking: TrackingPageData = {
|
|
pageName: "startpage",
|
|
pageType: "startpage",
|
|
pageId: system.uid,
|
|
channel: "homepage",
|
|
domainLanguage: lang,
|
|
createDate: system.created_at,
|
|
publishDate: system.updated_at,
|
|
siteSections: "startpage",
|
|
siteVersion: "new-web",
|
|
}
|
|
|
|
return {
|
|
startPage: startPage.data.start_page,
|
|
tracking,
|
|
}
|
|
}),
|
|
})
|