fix: improve logging for api requests
This commit is contained in:
@@ -32,6 +32,7 @@ export const hotelQueryRouter = router({
|
||||
params.include = include.join(",")
|
||||
}
|
||||
|
||||
console.log("api.hotels.hotel start")
|
||||
const apiResponse = await api.get(
|
||||
`${api.endpoints.v1.hotels}/${hotelId}`,
|
||||
{
|
||||
@@ -44,16 +45,17 @@ export const hotelQueryRouter = router({
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
console.info(`API Response Failed - Getting Hotel`)
|
||||
console.error(apiResponse)
|
||||
console.error("api.hotels.hotel error, ", JSON.stringify(apiResponse))
|
||||
return null
|
||||
}
|
||||
const apiJson = await apiResponse.json()
|
||||
const validatedHotelData = getHotelDataSchema.safeParse(apiJson)
|
||||
|
||||
if (!validatedHotelData.success) {
|
||||
console.error(`Get Individual Hotel Data - Verified Data Error`)
|
||||
console.error(validatedHotelData.error)
|
||||
console.error(
|
||||
"api.hotels.hotel validation error ",
|
||||
JSON.stringify(validatedHotelData.error)
|
||||
)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
@@ -72,7 +74,7 @@ export const hotelQueryRouter = router({
|
||||
return validatedRoom.data
|
||||
})
|
||||
: []
|
||||
|
||||
console.log("api.hotels.hotel success")
|
||||
return {
|
||||
hotel: validatedHotelData.data.data.attributes,
|
||||
roomCategories: roomCategories,
|
||||
@@ -88,25 +90,39 @@ export const hotelQueryRouter = router({
|
||||
// const apiLang = toApiLang(language)
|
||||
// params.set("hotelId", hotelId.toString())
|
||||
// params.set("language", apiLang)
|
||||
|
||||
console.log("api.hotels.rates start")
|
||||
const validatedHotelData = getRatesSchema.safeParse(tempRatesData)
|
||||
|
||||
if (!tempRatesData) {
|
||||
console.error("api.hotels.rates error, ", {})
|
||||
return null
|
||||
}
|
||||
if (!validatedHotelData.success) {
|
||||
console.error(`Get Individual Rates Data - Verified Data Error`)
|
||||
console.error(validatedHotelData.error)
|
||||
console.error(
|
||||
"api.hotels.rates validation error ",
|
||||
JSON.stringify(validatedHotelData.error)
|
||||
)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
console.log("api.hotels.rates success")
|
||||
return validatedHotelData.data
|
||||
}),
|
||||
getFilters: publicProcedure
|
||||
.input(getFiltersInputSchema)
|
||||
.query(async ({ input, ctx }) => {
|
||||
console.log("api.hotels.filters start")
|
||||
|
||||
if (!tempFilterData) {
|
||||
console.error("api.hotels.filters error, ", {})
|
||||
return null
|
||||
}
|
||||
const validateFilterData = getFiltersSchema.safeParse(tempFilterData)
|
||||
|
||||
if (!validateFilterData.success) {
|
||||
console.info(`Get Individual Filter Data - Verified Data Error`)
|
||||
console.error(validateFilterData.error)
|
||||
console.error(
|
||||
"api.hotels.filters validation error",
|
||||
JSON.stringify(validateFilterData.error)
|
||||
)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ async function getVerifiedUser({ session }: { session: Session }) {
|
||||
if (session.token.expires_at && session.token.expires_at < now) {
|
||||
return { error: true, cause: "token_expired" } as const
|
||||
}
|
||||
|
||||
console.info("api.user.profile start,")
|
||||
const apiResponse = await api.get(api.endpoints.v1.profile, {
|
||||
cache: "no-store",
|
||||
headers: {
|
||||
@@ -51,6 +51,7 @@ async function getVerifiedUser({ session }: { session: Session }) {
|
||||
})
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
console.error("api.user.profile error ", JSON.stringify(apiResponse))
|
||||
if (apiResponse.status === 401) {
|
||||
return { error: true, cause: "unauthorized" } as const
|
||||
} else if (apiResponse.status === 403) {
|
||||
@@ -67,19 +68,16 @@ async function getVerifiedUser({ session }: { session: Session }) {
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
if (!apiJson.data?.attributes) {
|
||||
console.error(`User has no data - (user: ${JSON.stringify(session.user)})`)
|
||||
console.error("api.user.profile data error")
|
||||
return null
|
||||
}
|
||||
|
||||
const verifiedData = getUserSchema.safeParse(apiJson.data.attributes)
|
||||
if (!verifiedData.success) {
|
||||
console.error(
|
||||
`Failed to validate User - (User: ${JSON.stringify(session.user)})`
|
||||
)
|
||||
console.error(verifiedData.error)
|
||||
console.error("api.user.profile validation error")
|
||||
return null
|
||||
}
|
||||
|
||||
console.info("api.user.profile success")
|
||||
return verifiedData
|
||||
}
|
||||
|
||||
@@ -109,6 +107,7 @@ async function updateStaysBookingUrl(
|
||||
lang: Lang
|
||||
) {
|
||||
// Tenporary API call needed till we have user name in ctx session data
|
||||
console.info("api.user.profile start")
|
||||
const apiResponse = await api.get(api.endpoints.v1.profile, {
|
||||
cache: "no-store",
|
||||
headers: {
|
||||
@@ -145,6 +144,7 @@ async function updateStaysBookingUrl(
|
||||
}
|
||||
|
||||
if (apiResponse.ok) {
|
||||
console.info("api.user.profile success")
|
||||
const apiJson = await apiResponse.json()
|
||||
if (apiJson.data?.attributes) {
|
||||
return data.map((d) => {
|
||||
@@ -170,6 +170,8 @@ async function updateStaysBookingUrl(
|
||||
})
|
||||
}
|
||||
}
|
||||
console.info("api.user.profile error", JSON.stringify(apiResponse))
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
@@ -279,6 +281,7 @@ export const userQueryRouter = router({
|
||||
const params = new URLSearchParams()
|
||||
params.set("limit", "1")
|
||||
|
||||
console.info("api.booking.stays.past start")
|
||||
const previousStaysResponse = await api.get(
|
||||
api.endpoints.v1.previousStays,
|
||||
{
|
||||
@@ -291,9 +294,9 @@ export const userQueryRouter = router({
|
||||
|
||||
if (!previousStaysResponse.ok) {
|
||||
console.error(
|
||||
`API Response Failed - Getting Previous Stays for tracking user`
|
||||
"api.booking.stays.past error ",
|
||||
JSON.stringify(previousStaysResponse)
|
||||
)
|
||||
console.error(previousStaysResponse)
|
||||
return notLoggedInUserTrackingData
|
||||
}
|
||||
|
||||
@@ -301,10 +304,13 @@ export const userQueryRouter = router({
|
||||
const verifiedPreviousStaysData =
|
||||
getStaysSchema.safeParse(previousStaysApiJson)
|
||||
if (!verifiedPreviousStaysData.success) {
|
||||
console.error(`Failed to validate Previous Stays Data for tracking user`)
|
||||
console.error(verifiedPreviousStaysData.error)
|
||||
console.error(
|
||||
"api.booking.stays.past validation error, ",
|
||||
JSON.stringify(verifiedPreviousStaysData.error)
|
||||
)
|
||||
return notLoggedInUserTrackingData
|
||||
}
|
||||
console.info("api.booking.stays.past success")
|
||||
|
||||
const membership = getMembership(verifiedUserData.data.memberships)
|
||||
|
||||
@@ -317,7 +323,6 @@ export const userQueryRouter = router({
|
||||
totalPointsAvailableToSpend: membership?.currentPoints,
|
||||
loginAction: "login success",
|
||||
}
|
||||
|
||||
return loggedInUserTrackingData
|
||||
}),
|
||||
benefits: router({
|
||||
@@ -340,6 +345,7 @@ export const userQueryRouter = router({
|
||||
if (cursor) {
|
||||
params.offset = cursor
|
||||
}
|
||||
console.info("api.booking.stays.past start")
|
||||
|
||||
const apiResponse = await api.get(
|
||||
api.endpoints.v1.previousStays,
|
||||
@@ -362,9 +368,10 @@ export const userQueryRouter = router({
|
||||
// default:
|
||||
// throw internalServerError(apiResponse)
|
||||
// }
|
||||
console.error(`API Response Failed - Getting Previous Stays`)
|
||||
console.error(`User: (${JSON.stringify(ctx.session.user)})`)
|
||||
console.error(apiResponse)
|
||||
console.error(
|
||||
"api.booking.stays.past error ",
|
||||
JSON.stringify(apiResponse)
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -372,12 +379,14 @@ export const userQueryRouter = router({
|
||||
|
||||
const verifiedData = getStaysSchema.safeParse(apiJson)
|
||||
if (!verifiedData.success) {
|
||||
console.error(`Failed to validate Previous Stays Data`)
|
||||
console.error(`User: (${JSON.stringify(ctx.session.user)})`)
|
||||
console.error(verifiedData.error)
|
||||
console.error(
|
||||
"api.booking.stays.past validation error ",
|
||||
JSON.stringify(verifiedData.error)
|
||||
)
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
console.info("api.booking.stays.past success")
|
||||
const nextCursor =
|
||||
verifiedData.data.links &&
|
||||
verifiedData.data.links.offset < verifiedData.data.links.totalCount
|
||||
@@ -405,7 +414,7 @@ export const userQueryRouter = router({
|
||||
if (cursor) {
|
||||
params.offset = cursor
|
||||
}
|
||||
|
||||
console.info("api.booking.stays.future start")
|
||||
const apiResponse = await api.get(
|
||||
api.endpoints.v1.upcomingStays,
|
||||
{
|
||||
@@ -427,21 +436,23 @@ export const userQueryRouter = router({
|
||||
// default:
|
||||
// throw internalServerError(apiResponse)
|
||||
// }
|
||||
console.error(`API Response Failed - Getting Upcoming Stays`)
|
||||
console.error(`User: (${JSON.stringify(ctx.session.user)})`)
|
||||
console.error(apiResponse)
|
||||
console.error(
|
||||
"api.booking.stays.future error ",
|
||||
JSON.stringify(apiResponse)
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
const verifiedData = getStaysSchema.safeParse(apiJson)
|
||||
if (!verifiedData.success) {
|
||||
console.error(`Failed to validate Upcoming Stays Data`)
|
||||
console.error(`User: (${JSON.stringify(ctx.session.user)})`)
|
||||
console.error(verifiedData.error)
|
||||
console.error(
|
||||
"api.booking.stays.future validation error ",
|
||||
JSON.stringify(verifiedData.error)
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
console.info("api.booking.stays.future success")
|
||||
const nextCursor =
|
||||
verifiedData.data.links &&
|
||||
verifiedData.data.links.offset < verifiedData.data.links.totalCount
|
||||
@@ -465,6 +476,7 @@ export const userQueryRouter = router({
|
||||
.input(friendTransactionsInput)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const { limit, page } = input
|
||||
console.info("api.transaction.friendTransactions start")
|
||||
const apiResponse = await api.get(api.endpoints.v1.friendTransactions, {
|
||||
cache: undefined, // override defaultOptions
|
||||
headers: {
|
||||
@@ -484,21 +496,24 @@ export const userQueryRouter = router({
|
||||
// default:
|
||||
// throw internalServerError()
|
||||
// }
|
||||
console.error(`API Response Failed - Getting Friend Transactions`)
|
||||
console.error(`User: (${JSON.stringify(ctx.session.user)})`)
|
||||
console.error(apiResponse)
|
||||
console.error(
|
||||
"api.transaction.friendTransactions error ",
|
||||
JSON.stringify(apiResponse)
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
const verifiedData = getFriendTransactionsSchema.safeParse(apiJson)
|
||||
if (!verifiedData.success) {
|
||||
console.error(`Failed to validate Friend Transactions Data`)
|
||||
console.error(`User: (${JSON.stringify(ctx.session.user)})`)
|
||||
console.error(verifiedData.error)
|
||||
console.error(
|
||||
"api.transaction.friendTransactions validation error ",
|
||||
JSON.stringify(verifiedData.error)
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
console.info("api.transaction.friendTransactions success")
|
||||
const updatedData = await updateStaysBookingUrl(
|
||||
verifiedData.data.data,
|
||||
ctx.session.token.access_token,
|
||||
@@ -565,6 +580,7 @@ export const userQueryRouter = router({
|
||||
}),
|
||||
|
||||
creditCards: protectedProcedure.query(async function ({ ctx }) {
|
||||
console.info("api.profile.creditCards start")
|
||||
const apiResponse = await api.get(api.endpoints.v1.creditCards, {
|
||||
cache: "no-store",
|
||||
headers: {
|
||||
@@ -573,25 +589,28 @@ export const userQueryRouter = router({
|
||||
})
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
console.error(`API Response Failed - Getting Creadit Cards`)
|
||||
console.error(`User: (${JSON.stringify(ctx.session.user)})`)
|
||||
console.error(apiResponse)
|
||||
console.error(
|
||||
"api.profile.creditCards error ",
|
||||
JSON.stringify(apiResponse)
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
const verifiedData = creditCardsSchema.safeParse(apiJson)
|
||||
if (!verifiedData.success) {
|
||||
console.error(`Failed to validate Credit Cards Data`)
|
||||
console.error(`User: (${JSON.stringify(ctx.session.user)})`)
|
||||
console.error(verifiedData.error)
|
||||
console.error(
|
||||
"api.profile.creditCards validation error ",
|
||||
JSON.stringify(verifiedData.error)
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
console.info("api.profile.creditCards success")
|
||||
return verifiedData.data.data
|
||||
}),
|
||||
|
||||
membershipCards: protectedProcedure.query(async function ({ ctx }) {
|
||||
console.info("api.profile start")
|
||||
const apiResponse = await api.get(api.endpoints.v1.profile, {
|
||||
cache: "no-store",
|
||||
headers: {
|
||||
@@ -610,9 +629,7 @@ export const userQueryRouter = router({
|
||||
// default:
|
||||
// throw internalServerError()
|
||||
// }
|
||||
console.error(`API Response Failed - Getting Membership Cards`)
|
||||
console.error(`User: (${JSON.stringify(ctx.session.user)})`)
|
||||
console.error(apiResponse)
|
||||
console.log("api.profile error ", JSON.stringify(apiResponse))
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
@@ -622,11 +639,13 @@ export const userQueryRouter = router({
|
||||
)
|
||||
|
||||
if (!verifiedData.success) {
|
||||
console.error(`Failed to validate Memberships Cards Data`)
|
||||
console.error(`User: (${JSON.stringify(ctx.session.user)})`)
|
||||
console.error(verifiedData.error)
|
||||
console.error(
|
||||
"api.profile validation error ",
|
||||
JSON.stringify(verifiedData)
|
||||
)
|
||||
return null
|
||||
}
|
||||
console.info("api.profile success")
|
||||
const cards = getMembershipCards(verifiedData.data)
|
||||
|
||||
return cards
|
||||
|
||||
Reference in New Issue
Block a user