Mirror: The small sibling of the graphql package, slimmed down for client-side libraries.
at v15.5.1000 1.5 kB view raw
1import { getLocation } from 'graphql/language/location'; 2import { 3 printLocation, 4 printSourceLocation, 5} from 'graphql/language/printLocation'; 6 7export class GraphQLError extends Error { 8 constructor( 9 message, 10 nodes, 11 source, 12 positions, 13 path, 14 originalError, 15 extensions 16 ) { 17 super(message); 18 19 this.name = 'GraphQLError'; 20 this.message = message; 21 22 if (path) this.path = path; 23 if (nodes) this.nodes = nodes; 24 if (source) this.source = source; 25 if (positions) this.positions = positions; 26 if (originalError) this.originalError = originalError; 27 28 let _extensions = extensions; 29 if (_extensions == null && originalError != null) { 30 const originalExtensions = originalError.extensions; 31 if (isObjectLike(originalExtensions)) { 32 _extensions = originalExtensions; 33 } 34 } 35 36 if (_extensions) { 37 this.extensions = _extensions; 38 } 39 } 40 41 toString() { 42 return printError(this); 43 } 44} 45 46/** 47 * Prints a GraphQLError to a string, representing useful location information 48 * about the error's position in the source. 49 */ 50export function printError(error) { 51 let output = error.message; 52 53 if (error.nodes) { 54 for (const node of error.nodes) { 55 if (node.loc) { 56 output += '\n\n' + printLocation(node.loc); 57 } 58 } 59 } else if (error.source && error.locations) { 60 for (const location of error.locations) { 61 output += '\n\n' + printSourceLocation(error.source, location); 62 } 63 } 64 65 return output; 66}