Skip to content

Adds message to stack trace of GraphQLError.#718

Merged
leebyron merged 4 commits intographql:masterfrom
tcr:tcr-errortrace
May 18, 2017
Merged

Adds message to stack trace of GraphQLError.#718
leebyron merged 4 commits intographql:masterfrom
tcr:tcr-errortrace

Conversation

@tcr
Copy link
Copy Markdown
Contributor

@tcr tcr commented Feb 16, 2017

With this sample code run in Node:

import { graphql, buildSchema } from 'graphql';

const schema = buildSchema(`
  type Query error_here {
    value: String
  }
`);

You get an error that looks like it should have a message, but because of how we create GraphQLErrors, it does not:

$ babel-node test.js

/Users/trim/github/graphql-js/dist/language/parser.js:965
  throw (0, _error.syntaxError)(lexer.source, token.start, 'Expected ' + kind + ', found ' + (0, _lexer.getTokenDesc)(token));
  ^
GraphQLError
    at syntaxError (/Users/trim/github/graphql-js/dist/error/syntaxError.js:28:15)
    at expect (/Users/trim/github/graphql-js/dist/language/parser.js:966:9)
    at many (/Users/trim/github/graphql-js/dist/language/parser.js:1014:3)
    at parseObjectTypeDefinition (/Users/trim/github/graphql-js/dist/language/parser.js:683:16)
    at parseTypeSystemDefinition (/Users/trim/github/graphql-js/dist/language/parser.js:607:16)
    at parseDefinition (/Users/trim/github/graphql-js/dist/language/parser.js:148:16)
    at parseDocument (/Users/trim/github/graphql-js/dist/language/parser.js:106:22)
    at parse (/Users/trim/github/graphql-js/dist/language/parser.js:43:10)
    at buildSchema (/Users/trim/github/graphql-js/dist/utilities/buildASTSchema.js:461:25)
    at Object.<anonymous> (/Users/trim/Desktop/ok3/what.js:3:16

With this PR, we add the message before generating the stack trace. The result becomes:

$ babel-node test.js

/Users/trim/github/graphql-js/dist/language/parser.js:965
  throw (0, _error.syntaxError)(lexer.source, token.start, 'Expected ' + kind + ', found ' + (0, _lexer.getTokenDesc)(token));
  ^
GraphQLError: Syntax Error GraphQL (2:14) Expected {, found Name "error_here"

1:
2:   type Query error_here {
                ^
3:     value: String

    at syntaxError (/Users/trim/github/graphql-js/dist/error/syntaxError.js:28:15)
    at expect (/Users/trim/github/graphql-js/dist/language/parser.js:966:9)
    at many (/Users/trim/github/graphql-js/dist/language/parser.js:1014:3)
    at parseObjectTypeDefinition (/Users/trim/github/graphql-js/dist/language/parser.js:683:16)
    at parseTypeSystemDefinition (/Users/trim/github/graphql-js/dist/language/parser.js:607:16)
    at parseDefinition (/Users/trim/github/graphql-js/dist/language/parser.js:148:16)
    at parseDocument (/Users/trim/github/graphql-js/dist/language/parser.js:106:22)
    at parse (/Users/trim/github/graphql-js/dist/language/parser.js:43:10)
    at buildSchema (/Users/trim/github/graphql-js/dist/utilities/buildASTSchema.js:461:25)
    at Object.<anonymous> (/Users/trim/Desktop/ok3/what.js:3:16)

@wincent
Copy link
Copy Markdown
Contributor

wincent commented Feb 16, 2017

Thanks for this. I added a commit that fixes a small (pre-existing) typo in the comment. This is subtle and I wouldn't be surprised if the behavior differs according to the JS engine at play, but at the same time it does seem relatively low risk. I'll let this bake for a while to give others chance to comment.

@wincent
Copy link
Copy Markdown
Contributor

wincent commented Mar 20, 2017

Closing this one, as it has been superseded by #722.

@wincent wincent closed this Mar 20, 2017
@tcr
Copy link
Copy Markdown
Contributor Author

tcr commented Mar 20, 2017

@wincent Thanks :) I was considering leaving it open since I believe Github should auto-close this if the commits landed in the other PR? But it is less confusing to close this proactively.

@leebyron leebyron reopened this May 18, 2017
@leebyron
Copy link
Copy Markdown
Contributor

Seems like the issue was creating the stack property before creating the message property, so I updated this to move stack property creation to happen last instead of first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants