import Debug from 'debug' ; import { Fauna } from '~/types/db' ; import { Biota } from '~/biota' ; const debug = Debug ( 'biota' ) . extend ( 'query' ) ; export async function query ( this , fqlQuery ) { return this . client . query ( fqlQuery ) . catch ( ( err ) => { const { requestResult } = err || { } ; const { requestRaw , responseRaw } = requestResult || { } ; let request = { } ; let response = { } ; try { request = JSON . parse ( requestRaw ) ; } catch ( error ) { } try { response = JSON . parse ( responseRaw ) ; } catch ( error ) { } const { errors } = response || ( { } as any ) ; const errorResponse = { errors : errors . map ( ( error ) => { let newError = { } ; let context = { } ; let params = { } ; let location = request ; for ( const key of error . position ) { location = location [ `${ key }` ] ; } if ( error . code === 'transaction aborted' ) { const errorArray = error . description . split ( '%||%' ) . map ( ( i ) => ( i && i . includes ( '%%%' ) ? i . split ( '%%%' ) : i ) ) ; const trace = errorArray [ 1 ] . map ( ( i ) => { if ( i . includes ( '%|%' ) ) { const splitted = i . split ( '%|%' ) ; try { splitted [ 1 ] = JSON . parse ( splitted [ 1 ] ) ; } catch ( error ) { } return splitted ; } return i ; } ) ; try { context = JSON . parse ( errorArray [ 4 ] ) ; } catch ( error ) { } try { params = JSON . parse ( errorArray [ 3 ] ) ; } catch ( error ) { } newError = { message : errorArray [ 2 ] , name : errorArray [ 0 ] , trace , params , code : error . code , context , } ; debug ( JSON . stringify ( newError , null , 2 ) ) ; } else { newError = error ; } newError . position = error . position ; newError . location = location ; return newError ; } ) , response , request , } ; return errorResponse ; } ) ; }