Files
web/packages/trpc/lib/routers/contentstack/destinationCountryPage/query.ts
Joakim Jäderberg 99537b13e8 Merged in chore/add-error-details-for-sentry (pull request #3378)
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
2026-01-12 09:01:44 +00:00

144 lines
4.2 KiB
TypeScript

import { createCounter } from "@scandic-hotels/common/telemetry"
import { router } from "../../.."
import { notFoundError } from "../../../errors"
import {
GetDestinationCountryPage,
GetDestinationCountryPageRefs,
} from "../../../graphql/Query/DestinationCountryPage/DestinationCountryPage.graphql"
import { request } from "../../../graphql/request"
import {
contentStackBaseWithServiceProcedure,
contentstackExtendedProcedureUID,
} from "../../../procedures"
import { ApiCountry } from "../../../types/country"
import { generateRefsResponseTag } from "../../../utils/generateTag"
import { getCityPagesInput } from "./input"
import {
destinationCountryPageRefsSchema,
destinationCountryPageSchema,
} from "./output"
import { generatePageTags, getCityPages } from "./utils"
import type {
GetDestinationCountryPageData,
GetDestinationCountryPageRefsSchema,
} from "../../../types/destinationCountryPage"
import type { TrackingPageData } from "../../types"
export const destinationCountryPageQueryRouter = router({
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
const { lang, uid } = ctx
const getDestinationCountryPageRefsCounter = createCounter(
"trpc.contentstack.destinationCountryPage.get.refs"
)
const metricsGetDestinationCountryPageRefs =
getDestinationCountryPageRefsCounter.init({ lang, uid })
metricsGetDestinationCountryPageRefs.start()
const variables = { locale: lang, uid }
const refsResponse = await request<GetDestinationCountryPageRefsSchema>(
GetDestinationCountryPageRefs,
variables,
{
key: generateRefsResponseTag(lang, uid),
ttl: "max",
}
)
if (!refsResponse.data) {
metricsGetDestinationCountryPageRefs.noDataError()
throw notFoundError({
message: "GetDestinationCountryPageRefs returned no data",
errorDetails: variables,
})
}
const validatedRefsData = destinationCountryPageRefsSchema.safeParse(
refsResponse.data
)
if (!validatedRefsData.success) {
metricsGetDestinationCountryPageRefs.validationError(
validatedRefsData.error
)
return null
}
metricsGetDestinationCountryPageRefs.success()
const tags = generatePageTags(validatedRefsData.data, lang)
const getDestinationCountryPageCounter = createCounter(
"trpc.contentstack.destinationCountryPage.get"
)
const metricsGetDestinationCountryPage =
getDestinationCountryPageCounter.init({ lang, uid })
metricsGetDestinationCountryPage.start()
const response = await request<GetDestinationCountryPageData>(
GetDestinationCountryPage,
variables,
{
key: tags,
ttl: "max",
}
)
if (!response.data) {
metricsGetDestinationCountryPage.noDataError()
throw notFoundError({
message: "GetDestinationCountryPage returned no data",
errorDetails: variables,
})
}
const validatedResponse = destinationCountryPageSchema.safeParse(
response.data
)
if (!validatedResponse.success) {
metricsGetDestinationCountryPage.validationError(validatedResponse.error)
return null
}
const destinationCountryPage =
validatedResponse.data.destination_country_page
const country = destinationCountryPage.destination_settings.country
metricsGetDestinationCountryPage.success()
const system = destinationCountryPage.system
const pageName = `destinations|${country}`
const tracking: TrackingPageData = {
pageId: system.uid,
domainLanguage: system.locale,
publishDate: system.updated_at,
createDate: system.created_at,
channel: "hotels",
pageType: "countrypage",
pageName,
siteSections: pageName,
siteVersion: "new-web",
}
return {
destinationCountryPage,
translatedCountry: ApiCountry[lang][country],
tracking,
}
}),
cityPages: contentStackBaseWithServiceProcedure
.input(getCityPagesInput)
.query(async ({ ctx, input }) => {
const { lang, serviceToken } = ctx
const { country } = input
const cities = await getCityPages(lang, serviceToken, country)
return cities
}),
})