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
97 lines
2.5 KiB
TypeScript
97 lines
2.5 KiB
TypeScript
import * as Sentry from "@sentry/nextjs"
|
|
|
|
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import * as api from "../../../api"
|
|
import { cache } from "../../../DUPLICATED/cache"
|
|
import {
|
|
extractResponseDetails,
|
|
internalServerError,
|
|
serverErrorByStatus,
|
|
sessionExpiredError,
|
|
} from "../../../errors"
|
|
import { getUserSchema } from "../output"
|
|
|
|
import type { z } from "zod"
|
|
|
|
import type { DeepPartial } from "../../../types/deepPartial"
|
|
|
|
export const getVerifiedUser = cache(
|
|
async ({
|
|
token,
|
|
includeExtendedPartnerData,
|
|
}: {
|
|
token: { expires_at?: number; access_token: string }
|
|
includeExtendedPartnerData?: boolean
|
|
}) => {
|
|
const getVerifiedUserCounter = createCounter("user.getVerifiedUser")
|
|
const metricsGetVerifiedUser = getVerifiedUserCounter.init()
|
|
|
|
metricsGetVerifiedUser.start()
|
|
|
|
const now = Date.now()
|
|
if (token.expires_at && token.expires_at < now) {
|
|
throw sessionExpiredError()
|
|
}
|
|
|
|
const apiResponse = await api.get(
|
|
api.endpoints.v2.Profile.profile,
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${token.access_token}`,
|
|
},
|
|
},
|
|
includeExtendedPartnerData
|
|
? { includes: "extendedPartnerInformation" }
|
|
: {}
|
|
)
|
|
|
|
if (!apiResponse.ok) {
|
|
await metricsGetVerifiedUser.httpError(apiResponse)
|
|
|
|
throw serverErrorByStatus(
|
|
apiResponse.status,
|
|
await extractResponseDetails(apiResponse),
|
|
"getVerifiedUser failed"
|
|
)
|
|
}
|
|
|
|
const apiJson = await apiResponse.json()
|
|
if (!apiJson.data?.attributes) {
|
|
metricsGetVerifiedUser.dataError(
|
|
`Missing data attributes in API response`,
|
|
{
|
|
data: apiJson,
|
|
}
|
|
)
|
|
throw internalServerError("Missing data attributes in API response")
|
|
}
|
|
|
|
const verifiedData = getUserSchema.safeParse(apiJson)
|
|
|
|
if (!verifiedData.success) {
|
|
addUserToSentry(apiJson)
|
|
metricsGetVerifiedUser.validationError(verifiedData.error)
|
|
throw verifiedData.error
|
|
}
|
|
|
|
metricsGetVerifiedUser.success()
|
|
return verifiedData.data
|
|
}
|
|
)
|
|
|
|
function addUserToSentry(apiJson: unknown) {
|
|
const typedData = apiJson as DeepPartial<z.input<typeof getUserSchema>>
|
|
if (
|
|
typeof typedData.data?.attributes?.profileId === "undefined" ||
|
|
typeof typedData.data?.attributes?.membershipNumber === "undefined"
|
|
) {
|
|
return
|
|
}
|
|
|
|
Sentry.setUser({
|
|
id: typedData?.data?.attributes?.profileId,
|
|
username: typedData?.data?.attributes?.membershipNumber,
|
|
})
|
|
}
|