Skip to content

Commit 7263ba2

Browse files
committed
Include status code in ads responses
1 parent 07b2e81 commit 7263ba2

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

web/src/app/api/v1/ads/_post.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
LoggerWithContextFn,
1414
} from '@codebuff/common/types/contracts/logger'
1515
import type { NextRequest } from 'next/server'
16+
import { getErrorObject } from '@codebuff/common/util/error'
1617

1718
const messageSchema = z.object({
1819
role: z.string(),
@@ -109,7 +110,7 @@ export async function postAds(params: {
109110
// Handle 204 No Content first (no body to parse)
110111
if (response.status === 204) {
111112
logger.debug(
112-
{ request: requestBody },
113+
{ request: requestBody, status: response.status },
113114
'[ads] No ad available from Gravity API',
114115
)
115116
return NextResponse.json({ ad: null }, { status: 200 })
@@ -120,7 +121,7 @@ export async function postAds(params: {
120121

121122
if (!response.ok) {
122123
logger.error(
123-
{ request: requestBody, response: ad },
124+
{ request: requestBody, response: ad, status: response.status },
124125
'[ads] Gravity API returned error',
125126
)
126127
return NextResponse.json({ ad: null }, { status: 200 })
@@ -130,6 +131,7 @@ export async function postAds(params: {
130131
{
131132
ad,
132133
request: requestBody,
134+
status: response.status,
133135
},
134136
'[ads] Fetched ad from Gravity API',
135137
)
@@ -150,7 +152,7 @@ export async function postAds(params: {
150152
})
151153

152154
logger.info(
153-
{ userId, impUrl: ad.impUrl },
155+
{ userId, impUrl: ad.impUrl, status: response.status },
154156
'[ads] Created ad_impression record for served ad',
155157
)
156158
} catch (error) {
@@ -160,6 +162,7 @@ export async function postAds(params: {
160162
{
161163
userId,
162164
impUrl: ad.impUrl,
165+
status: response.status,
163166
error:
164167
error instanceof Error
165168
? { name: error.name, message: error.message }
@@ -177,13 +180,17 @@ export async function postAds(params: {
177180
{
178181
userId,
179182
messages,
183+
status: 500,
180184
error:
181185
error instanceof Error
182186
? { name: error.name, message: error.message }
183187
: error,
184188
},
185189
'[ads] Failed to fetch ad from Gravity API',
186190
)
187-
return NextResponse.json({ ad: null }, { status: 200 })
191+
return NextResponse.json(
192+
{ ad: null, error: getErrorObject(error) },
193+
{ status: 500 },
194+
)
188195
}
189196
}

0 commit comments

Comments
 (0)