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