File tree Expand file tree Collapse file tree 1 file changed +32
-17
lines changed
Expand file tree Collapse file tree 1 file changed +32
-17
lines changed Original file line number Diff line number Diff line change @@ -243,23 +243,38 @@ export async function postAds(params: {
243243 }
244244
245245 // Store all returned ads in the database (skip duplicates via imp_url unique constraint)
246- for ( const ad of ads ) {
247- const payout = ad . payout || DEFAULT_PAYOUT
248- await db
249- . insert ( schema . adImpression )
250- . values ( {
251- user_id : userId ,
252- ad_text : ad . adText ,
253- title : ad . title ,
254- cta : ad . cta ,
255- url : ad . url ,
256- favicon : ad . favicon ,
257- click_url : ad . clickUrl ,
258- imp_url : ad . impUrl ,
259- payout : String ( payout ) ,
260- credits_granted : 0 ,
261- } )
262- . onConflictDoNothing ( )
246+ // Wrapped in try/catch so DB failures don't prevent serving ads to the client
247+ try {
248+ for ( const ad of ads ) {
249+ const payout = ad . payout || DEFAULT_PAYOUT
250+ await db
251+ . insert ( schema . adImpression )
252+ . values ( {
253+ user_id : userId ,
254+ ad_text : ad . adText ,
255+ title : ad . title ,
256+ cta : ad . cta ,
257+ url : ad . url ,
258+ favicon : ad . favicon ,
259+ click_url : ad . clickUrl ,
260+ imp_url : ad . impUrl ,
261+ payout : String ( payout ) ,
262+ credits_granted : 0 ,
263+ } )
264+ . onConflictDoNothing ( )
265+ }
266+ } catch ( dbError ) {
267+ logger . warn (
268+ {
269+ userId,
270+ adCount : ads . length ,
271+ error :
272+ dbError instanceof Error
273+ ? { name : dbError . name , message : dbError . message }
274+ : dbError ,
275+ } ,
276+ '[ads] Failed to persist ad_impression rows, serving ads anyway' ,
277+ )
263278 }
264279
265280 // Strip payout from all ads before returning to client
You can’t perform that action at this time.
0 commit comments