11import { afterEach , beforeEach , describe , expect , it , mock , spyOn } from 'bun:test'
2+ import { createPostgresError } from '@codebuff/common/testing/errors'
23
34import {
45 getRetryableErrorDescription ,
@@ -204,8 +205,7 @@ describe('transaction error handling', () => {
204205 } )
205206
206207 it ( 'should handle Error object with code property' , ( ) => {
207- const error = new Error ( 'Connection failed' )
208- ; ( error as Error & { code : string } ) . code = '08006'
208+ const error = createPostgresError ( 'Connection failed' , '08006' )
209209 expect ( getRetryableErrorDescription ( error ) ) . toBe ( 'connection_failure' )
210210 } )
211211 } )
@@ -352,9 +352,7 @@ describe('withSerializableTransaction', () => {
352352 async ( callback ) => {
353353 attempts ++
354354 if ( attempts === 1 ) {
355- const error = new Error ( 'serialization failure' )
356- ; ( error as Error & { code : string } ) . code = '40001'
357- throw error
355+ throw createPostgresError ( 'serialization failure' , '40001' )
358356 }
359357 return callback ( { } as Parameters < typeof callback > [ 0 ] )
360358 } ,
@@ -377,9 +375,7 @@ describe('withSerializableTransaction', () => {
377375 async ( callback ) => {
378376 attempts ++
379377 if ( attempts <= 2 ) {
380- const error = new Error ( 'connection failure' )
381- ; ( error as Error & { code : string } ) . code = '08006'
382- throw error
378+ throw createPostgresError ( 'connection failure' , '08006' )
383379 }
384380 return callback ( { } as Parameters < typeof callback > [ 0 ] )
385381 } ,
@@ -401,9 +397,7 @@ describe('withSerializableTransaction', () => {
401397 async ( callback ) => {
402398 attempts ++
403399 if ( attempts === 1 ) {
404- const error = new Error ( 'deadlock detected' )
405- ; ( error as Error & { code : string } ) . code = '40P01'
406- throw error
400+ throw createPostgresError ( 'deadlock detected' , '40P01' )
407401 }
408402 return callback ( { } as Parameters < typeof callback > [ 0 ] )
409403 } ,
@@ -425,9 +419,7 @@ describe('withSerializableTransaction', () => {
425419 async ( callback ) => {
426420 attempts ++
427421 if ( attempts === 1 ) {
428- const error = new Error ( 'serialization failure' )
429- ; ( error as Error & { code : string } ) . code = '40001'
430- throw error
422+ throw createPostgresError ( 'serialization failure' , '40001' )
431423 }
432424 return callback ( { } as Parameters < typeof callback > [ 0 ] )
433425 } ,
@@ -460,9 +452,7 @@ describe('withSerializableTransaction', () => {
460452 transactionSpy = spyOn ( dbModule . db , 'transaction' ) . mockImplementation (
461453 async ( ) => {
462454 attempts ++
463- const error = new Error ( 'unique violation' )
464- ; ( error as Error & { code : string } ) . code = '23505'
465- throw error
455+ throw createPostgresError ( 'unique violation' , '23505' )
466456 } ,
467457 )
468458
@@ -482,9 +472,7 @@ describe('withSerializableTransaction', () => {
482472 transactionSpy = spyOn ( dbModule . db , 'transaction' ) . mockImplementation (
483473 async ( ) => {
484474 attempts ++
485- const error = new Error ( 'syntax error' )
486- ; ( error as Error & { code : string } ) . code = '42601'
487- throw error
475+ throw createPostgresError ( 'syntax error' , '42601' )
488476 } ,
489477 )
490478
@@ -504,9 +492,7 @@ describe('withSerializableTransaction', () => {
504492 transactionSpy = spyOn ( dbModule . db , 'transaction' ) . mockImplementation (
505493 async ( ) => {
506494 attempts ++
507- const error = new Error ( 'foreign key violation' )
508- ; ( error as Error & { code : string } ) . code = '23503'
509- throw error
495+ throw createPostgresError ( 'foreign key violation' , '23503' )
510496 } ,
511497 )
512498
@@ -545,9 +531,7 @@ describe('withSerializableTransaction', () => {
545531 transactionSpy = spyOn ( dbModule . db , 'transaction' ) . mockImplementation (
546532 async ( ) => {
547533 attempts ++
548- const error = new Error ( 'persistent serialization failure' )
549- ; ( error as Error & { code : string } ) . code = '40001'
550- throw error
534+ throw createPostgresError ( 'persistent serialization failure' , '40001' )
551535 } ,
552536 )
553537
0 commit comments