New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IDE friendly errors #2752
IDE friendly errors #2752
Changes from all commits
5acfdc1
b9dff0b
f6afa96
496fc94
025a41f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,7 +110,7 @@ function createCombinedError( | |
`${prefix}Encountered ${errors.length} error(s):\n` + | ||
errors | ||
.map(error => | ||
String(error) | ||
(error instanceof GraphQLError ? printError(error) : String(error)) | ||
.split('\n') | ||
.map((line, index) => (index === 0 ? `- ${line}` : ` ${line}`)) | ||
.join('\n'), | ||
|
@@ -192,6 +192,34 @@ function printLocations(locations: $ReadOnlyArray<Location>): Array<string> { | |
return printedLocations; | ||
} | ||
|
||
/** | ||
* Prints a GraphQLError to a string, representing useful location information | ||
* about the error's position in the source. | ||
*/ | ||
function printError(error: GraphQLError): string { | ||
const printedLocations = []; | ||
if (error.nodes) { | ||
for (const node of error.nodes) { | ||
if (node.loc) { | ||
printedLocations.push( | ||
highlightSourceAtLocation( | ||
node.loc.source, | ||
getLocation(node.loc.source, node.loc.start), | ||
), | ||
); | ||
} | ||
} | ||
} else if (error.source && error.locations) { | ||
const source = error.source; | ||
for (const location of error.locations) { | ||
printedLocations.push(highlightSourceAtLocation(source, location)); | ||
} | ||
} | ||
return printedLocations.length === 0 | ||
? error.message | ||
: [error.message, ...printedLocations].join('\n\n') + '\n'; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All only needed so we can use our own copy of |
||
|
||
/** | ||
* Render a helpful description of the location of the error in the GraphQL | ||
* Source document. | ||
|
@@ -212,7 +240,7 @@ function highlightSourceAtLocation( | |
|
||
const lines = body.split(/\r\n|[\n\r]/g); | ||
return ( | ||
`${source.name} (${lineNum}:${columnNum})\n` + | ||
`${source.name}:${lineNum}:${columnNum}\n` + | ||
printPrefixedLines([ | ||
// Lines specified like this: ["prefix", "string"], | ||
[`${lineNum - 1}: `, lines[lineIndex - 1]], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,14 +23,14 @@ import type { | |
GraphQLTagFinder, | ||
} from '../language/RelayLanguagePluginInterface'; | ||
|
||
const cache = new RelayCompilerCache('RelayFindGraphQLTags', 'v1'); | ||
const cache = new RelayCompilerCache('RelayFindGraphQLTags', 'v2'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have to bust the cache, as we should now cache the entire There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
function memoizedFind( | ||
tagFinder: GraphQLTagFinder, | ||
text: string, | ||
baseDir: string, | ||
file: File, | ||
): $ReadOnlyArray<string> { | ||
): $ReadOnlyArray<GraphQLTag> { | ||
invariant( | ||
file.exists, | ||
'RelayFindGraphQLTags: Called with non-existent file `%s`', | ||
|
@@ -46,11 +46,11 @@ function find( | |
tagFinder: GraphQLTagFinder, | ||
text: string, | ||
absPath: string, | ||
): $ReadOnlyArray<string> { | ||
): $ReadOnlyArray<GraphQLTag> { | ||
const tags = tagFinder(text, absPath); | ||
const moduleName = getModuleName(absPath); | ||
tags.forEach(tag => validateTemplate(tag, moduleName, absPath)); | ||
return tags.map(tag => tag.template); | ||
return tags; | ||
} | ||
|
||
function validateTemplate( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,14 +73,18 @@ module.exports = ( | |
|
||
const astDefinitions = []; | ||
const sources = []; | ||
memoizedTagFinder(text, baseDir, file).forEach(template => { | ||
const source = new GraphQL.Source(template, file.relPath); | ||
memoizedTagFinder(text, baseDir, file).forEach(tag => { | ||
const source = new GraphQL.Source( | ||
tag.template, | ||
path.join(path.relative(process.cwd(), baseDir), file.relPath), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep path short by making it relative, but in a way that the IDE can find it (i.e. relative to the working dir). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kassens This too. |
||
tag.sourceLocationOffset, | ||
); | ||
const ast = parseGraphQL(source); | ||
invariant( | ||
ast.definitions.length, | ||
'RelaySourceModuleParser: Expected GraphQL text to contain at least one ' + | ||
'definition (fragment, mutation, query, subscription), got `%s`.', | ||
template, | ||
tag.template, | ||
); | ||
sources.push(source.body); | ||
astDefinitions.push(...ast.definitions); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use our own formatter for
GraphQLError
instances, instead of the one from graphql-js.