import { GraphQLError } from 'graphql' ; import { mixed , GraphQLErrorExtended , GraphQLFormattedErrorExtended } from '../interfaces' ; /**
 * Extracts the requested fields from a pg error object, handling 'code' -> 'errcode' mapping.
 */ function pickPgError ( err , inFields ) { const result = { } ; let fields ; if ( Array . isArray ( inFields ) ) { fields = inFields ; } else if ( typeof inFields === 'string' ) { fields = inFields . split ( ',' ) ; } else { throw new Error ( 'Invalid argument to extendedErrors - expected array of strings' ) ; } if ( err && typeof err === 'object' ) { fields . forEach ( ( field ) => { if ( typeof field !== 'string' ) { throw new Error ( 'Invalid argument to extendedErrors - expected array of strings' ) ; } const errField = field === 'errcode' ? 'code' : field ; result [ field ] = err [ errField ] != null ? String ( err [ errField ] ) : err [ errField ] ; } ) ; } return result ; } /**
 * Given a GraphQLError, format it according to the rules described by the
 * Response Format, Errors section of the GraphQL Specification, plus it can
 * extract additional error codes from the postgres error, such as 'hint',
 * 'detail', 'errcode', 'where', etc. - see `extendedErrors` option.
 */ export function extendedFormatError ( error , fields ) { if ( ! error ) { throw new Error ( 'Received null or undefined error.' ) ; } const originalError = error . originalError as GraphQLErrorExtended ; const exceptionDetails = originalError && fields ? pickPgError ( originalError , fields ) : undefined ; return { ... exceptionDetails , ... ( exceptionDetails ? { extensions : { ... originalError . extensions , exception : exceptionDetails , } , } : null ) , message : error . message , locations : error . locations , path : error . path , } ; }