import { find , keys , pick } from 'ramda' ; export const PICKED_AXIOS_PROPS = [ 'baseURL' , 'cacheable' , 'data' , 'finished' , 'headers' , 'method' , 'timeout' , 'status' , 'path' , 'url' , 'metric' , 'inflightKey' , 'forceMaxAge' , 'params' , 'responseType' ] ; export const SENSITIVE_EXCEPTION_FIELDS = [ 'config' , 'request' , 'stack' ] ; const MAX_ERROR_STRING_LENGTH = process . env . MAX_ERROR_STRING_LENGTH ? parseInt ( process . env . MAX_ERROR_STRING_LENGTH , 10 ) : 8 * 1024 ; const findCaseInsensitive = ( target , set ) => find ( t => t . toLocaleLowerCase ( ) === target , set ) ; const destroyCircular = ( from , seen ) => { const to = Array . isArray ( from ) ? [ ] : { } ; seen . push ( from ) ; for ( const [ key , value ] of Object . entries ( from ) ) { if ( typeof value === 'function' ) { continue ; } if ( key . startsWith ( '_' ) ) { continue ; } if ( ! value || typeof value !== 'object' ) { if ( typeof value === 'string' && value . length > MAX_ERROR_STRING_LENGTH ) { to [ key ] = value . substr ( 0 , MAX_ERROR_STRING_LENGTH ) + '[...TRUNCATED]' ; } else { to [ key ] = value ; } continue ; } if ( ! seen . includes ( from [ key ] ) ) { to [ key ] = destroyCircular ( from [ key ] , seen . slice ( ) ) ; continue ; } to [ key ] = '[Circular]' ; } const commonProperties = [ 'name' , 'message' , 'stack' , 'code' , ] ; for ( const property of commonProperties ) { if ( typeof from [ property ] === 'string' ) { to [ property ] = from [ property ] ; } } const axiosProperties = [ 'config' , 'request' , 'response' , ] ; for ( const property of axiosProperties ) { if ( to [ property ] != null && typeof to [ property ] === 'object' ) { to [ property ] = pick ( PICKED_AXIOS_PROPS , to [ property ] ) ; const headers = to [ property ] && to [ property ] . headers ; if ( headers ) { const headerNames = keys ( headers ) ; const authorization = findCaseInsensitive ( 'authorization' , headerNames as string [ ] ) ; if ( ! ! authorization ) { delete headers [ authorization ] ; } const proxyAuth = findCaseInsensitive ( 'proxy-authorization' , headerNames as string [ ] ) ; if ( ! ! proxyAuth ) { delete headers [ proxyAuth ] ; } const vtexIdClientAutCookie = findCaseInsensitive ( 'vtexidclientautcookie' , headerNames as string [ ] ) ; if ( ! ! vtexIdClientAutCookie ) { delete headers [ vtexIdClientAutCookie ] ; } } } } if ( ! to . code && to . response ) { to . code = to . response . status && `E_HTTP_${ to . response . status }` || 'E_UNKNOWN' ; } return to ; } ; param value export const cleanError = ( value ) => { if ( typeof value === 'object' ) { return destroyCircular ( value , [ ] ) ; } if ( typeof value === 'function' ) { return `[Function: ${ ( value . name || 'anonymous' ) }]` ; } return value ; } ;