Skip to content

Commit

Permalink
Ported changes for gqlerrors
Browse files Browse the repository at this point in the history
- Include originalError in GraphQL error to preserve any additional info
	- Fixes graphql-go#251
	- graphql/graphql-js@3d27953

- Remove remaining references to u2028 and u2029
  	- graphql/graphql-js@5ee1edc
  • Loading branch information
sogko committed Mar 7, 2016
1 parent c21493f commit bfa307c
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 32 deletions.
28 changes: 15 additions & 13 deletions gqlerrors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ import (
)

type Error struct {
Message string
Stack string
Nodes []ast.Node
Source *source.Source
Positions []int
Locations []location.SourceLocation
Message string
Stack string
Nodes []ast.Node
Source *source.Source
Positions []int
Locations []location.SourceLocation
OriginalError error
}

// implements Golang's built-in `error` interface
func (g Error) Error() string {
return fmt.Sprintf("%v", g.Message)
}

func NewError(message string, nodes []ast.Node, stack string, source *source.Source, positions []int) *Error {
func NewError(message string, nodes []ast.Node, stack string, source *source.Source, positions []int, origError error) *Error {
if stack == "" && message != "" {
stack = message
}
Expand All @@ -49,11 +50,12 @@ func NewError(message string, nodes []ast.Node, stack string, source *source.Sou
locations = append(locations, loc)
}
return &Error{
Message: message,
Stack: stack,
Nodes: nodes,
Source: source,
Positions: positions,
Locations: locations,
Message: message,
Stack: stack,
Nodes: nodes,
Source: source,
Positions: positions,
Locations: locations,
OriginalError: origError,
}
}
8 changes: 8 additions & 0 deletions gqlerrors/located.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package gqlerrors

import (
"errors"
"github.com/graphql-go/graphql/language/ast"
)

// NewLocatedError
// @deprecated 0.4.18
// Already exists in `graphql.NewLocatedError()`
func NewLocatedError(err interface{}, nodes []ast.Node) *Error {
var origError error
message := "An unknown error occurred."
if err, ok := err.(error); ok {
message = err.Error()
origError = err
}
if err, ok := err.(string); ok {
message = err
origError = errors.New(err)
}
stack := message
return NewError(
Expand All @@ -19,6 +26,7 @@ func NewLocatedError(err interface{}, nodes []ast.Node) *Error {
stack,
nil,
[]int{},
origError,
)
}

Expand Down
3 changes: 2 additions & 1 deletion gqlerrors/syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func NewSyntaxError(s *source.Source, position int, description string) *Error {
"",
s,
[]int{position},
nil,
)
}

Expand All @@ -26,7 +27,7 @@ func highlightSourceAtLocation(s *source.Source, l location.SourceLocation) stri
lineNum := fmt.Sprintf("%d", line)
nextLineNum := fmt.Sprintf("%d", (line + 1))
padLen := len(nextLineNum)
lines := regexp.MustCompile("\r\n|[\n\r\u2028\u2029]").Split(s.Body, -1)
lines := regexp.MustCompile("\r\n|[\n\r]").Split(s.Body, -1)
var highlight string
if line >= 2 {
highlight += fmt.Sprintf("%s: %s\n", lpad(padLen, prevLineNum), lines[line-2])
Expand Down
18 changes: 0 additions & 18 deletions language/lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,24 +171,6 @@ func TestLexReportsUsefulStringErrors(t *testing.T) {
Body: "\"multi\rline\"",
Expected: `Syntax Error GraphQL (1:7) Unterminated string.
1: "multi
^
2: line"
`,
},
Test{
Body: "\"multi\u2028line\"",
Expected: `Syntax Error GraphQL (1:7) Unterminated string.
1: "multi
^
2: line"
`,
},
Test{
Body: "\"multi\u2029line\"",
Expected: `Syntax Error GraphQL (1:7) Unterminated string.
1: "multi
^
2: line"
Expand Down
5 changes: 5 additions & 0 deletions located.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package graphql

import (
"errors"
"github.com/graphql-go/graphql/gqlerrors"
"github.com/graphql-go/graphql/language/ast"
)

func NewLocatedError(err interface{}, nodes []ast.Node) *gqlerrors.Error {
var origError error
message := "An unknown error occurred."
if err, ok := err.(error); ok {
message = err.Error()
origError = err
}
if err, ok := err.(string); ok {
message = err
origError = errors.New(err)
}
stack := message
return gqlerrors.NewError(
Expand All @@ -20,6 +24,7 @@ func NewLocatedError(err interface{}, nodes []ast.Node) *gqlerrors.Error {
stack,
nil,
[]int{},
origError,
)
}

Expand Down
1 change: 1 addition & 0 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func newValidationRuleError(message string, nodes []ast.Node) (string, error) {
"",
nil,
[]int{},
nil, // TODO: this is interim, until we port "better-error-messages-for-inputs"
)
}

Expand Down
3 changes: 3 additions & 0 deletions values.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func getVariableValue(schema Schema, definitionAST *ast.VariableDefinition, inpu
"",
nil,
[]int{},
nil,
)
}

Expand All @@ -99,6 +100,7 @@ func getVariableValue(schema Schema, definitionAST *ast.VariableDefinition, inpu
"",
nil,
[]int{},
nil,
)
}
inputStr := ""
Expand All @@ -113,6 +115,7 @@ func getVariableValue(schema Schema, definitionAST *ast.VariableDefinition, inpu
"",
nil,
[]int{},
nil,
)
}

Expand Down

0 comments on commit bfa307c

Please sign in to comment.