import { StructuredError } from '../../utils/StructuredError' ; export type ErrorInterpreter < T , U extends ErrorData > = ( err : Error , target : T , key : string , ... args : any [ ] ) => StructuredError < U > | undefined ; export type AnyErrorData = EmptyErrorData | NetworkErrorData | ActionErrorData | AssertionErrorData | LocatorErrorData | PuppeteerErrorData ; type ErrorDataKind = 'net' | 'action' | 'empty' | 'assertion' | 'locator' | 'puppeteer' ; export interface ErrorData { _kind : ErrorDataKind ; } export interface EmptyErrorData { _kind : 'empty' ; } export const emptyErrorData = { _kind : 'empty' as ErrorDataKind } ; type NetworkErrorKind = 'net' | 'http' ; export interface NetworkErrorData { _kind : 'net' ; kind : NetworkErrorKind ; subKind : string ; url : string ; reason ? : string ; code ? : string ; } export type ActionErrorKind = 'wait-timeout' | 'node-detached' ; export interface ActionErrorData { _kind : 'action' ; kind : ActionErrorKind ; action : string ; } export interface AssertionErrorData { _kind : 'assertion' ; } export interface LocatorErrorData { _kind : 'locator' ; kind : string ; locator : string ; } export type PuppeteerErrorKind = 'execution-context-destroyed' | 'evaluation-timeout' ; export interface PuppeteerErrorData { _kind : 'puppeteer' ; kind : PuppeteerErrorKind ; } export function interpretError < T , U extends ErrorData > ( interpreters , inputError , target , propertyKey , args ) { for ( const interp of interpreters ) { const outputErr = interp ( inputError , target , propertyKey , ... args ) ; if ( outputErr !== undefined ) return outputErr ; } return inputError ; } export function castStructuredError < T extends ErrorData > ( error , kind ) { if ( ( < StructuredError < T > > error ) . _structured === 'yes' && ( < StructuredError < T > > error ) . data . _kind === kind ) { return < StructuredError < T > > error ; } else { return undefined ; } } export function getStructuredData < T extends ErrorData > ( error , kind ) { if ( ( < StructuredError < T > > error ) . _structured === 'yes' && ( < StructuredError < T > > error ) . data . _kind === kind ) { return ( < StructuredError < T > > error ) . data ; } }