import { TransactionCancelledException } from '@typedorm/common' ; import { AWSError , Request } from 'aws-sdk' ; export function handleTransactionResult < T > ( transactionRequest ) { let cancellationReasons ; transactionRequest . on ( 'extractError' , response => { try { cancellationReasons = JSON . parse ( response . httpResponse . body . toString ( ) ) . CancellationReasons ; } catch ( err ) { console . error ( 'Error extracting cancellation error' , err ) ; } } ) ; return new Promise ( ( resolve , reject ) => { transactionRequest . send ( ( err , response ) => { if ( err ) { const reasons = cancellationReasons . map ( reason => { return { code : reason . Code , message : reason . Message , } ; } ) ; return reject ( new TransactionCancelledException ( err . code , reasons ) ) ; } return resolve ( response ) ; } ) ; } ) as Promise < T > ; }