import { AnyErrorData , interpretError } from '../errors/Types' ; import { StructuredError } from '../../utils/StructuredError' ; import { Browser } from '../../interface/IBrowser' ; import { StepActionArgs } from '../../utils/StepActionArgs' ; /**
 * Defines a Function Decorator which wraps a method with class local before and after
 */ export function addCallbacks ( ) { const errorInterpreters = [ ] ; return function ( _target , propertyKey , descriptor ) { const originalFn = descriptor . value ; descriptor . value = async function ( ... args ) { let ret ; const browser = this as Browser ; const calltimeError = new Error ( ) ; Error . captureStackTrace ( calltimeError ) ; const calltimeStack = calltimeError . stack ; try { if ( browser . beforeFunc instanceof Function ) { const arg = StepActionArgs ( args ) ; await browser . beforeFunc ( browser , propertyKey , arg ) ; } ret = await originalFn . apply ( browser , args ) ; } catch ( e ) { const newError = interpretError < Browser , AnyErrorData > ( errorInterpreters , e , browser , propertyKey , args ) ; const sErr = StructuredError . liftWithSource ( newError , 'browser' , `browser.${ propertyKey }` ) ; sErr . stack = calltimeStack ; throw sErr ; } finally { if ( browser . afterFunc instanceof Function ) { const arg = StepActionArgs ( args ) ; await browser . afterFunc ( browser , propertyKey , arg ) ; } } return ret ; } ; return descriptor ; } ; }