Skip to content

Commit

Permalink
adds OriginalError support
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-ramon committed Dec 2, 2018
1 parent 8c25a9a commit bfe0f60
Show file tree
Hide file tree
Showing 5 changed files with 565 additions and 361 deletions.
35 changes: 19 additions & 16 deletions abstract_test.go
Expand Up @@ -491,6 +491,7 @@ func TestResolveTypeOnInterfaceYieldsUsefulError(t *testing.T) {
}
}`

originalError := gqlerrors.NewFormattedError(`Runtime Object type "Human" is not a possible type for "Pet".`)
expected := &graphql.Result{
Data: map[string]interface{}{
"pets": []interface{}{
Expand All @@ -505,27 +506,27 @@ func TestResolveTypeOnInterfaceYieldsUsefulError(t *testing.T) {
nil,
},
},
Errors: []gqlerrors.FormattedError{
{
Message: `Runtime Object type "Human" is not a possible type for "Pet".`,
Locations: []location.SourceLocation{
{
Line: 2,
Column: 7,
},
},
Path: []interface{}{
"pets",
2,
Errors: []gqlerrors.FormattedError{gqlerrors.FormatError(gqlerrors.Error{
Message: originalError.Message,
Locations: []location.SourceLocation{
{
Line: 2,
Column: 7,
},
},
},
Path: []interface{}{
"pets",
2,
},
OriginalError: originalError,
})},
}

result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: query,
})

if len(result.Errors) == 0 {
t.Fatalf("wrong result, expected errors: %v, got: %v", len(expected.Errors), len(result.Errors))
}
Expand Down Expand Up @@ -618,6 +619,7 @@ func TestResolveTypeOnUnionYieldsUsefulError(t *testing.T) {
}
}`

originalError := gqlerrors.NewFormattedError(`Runtime Object type "Human" is not a possible type for "Pet".`)
expected := &graphql.Result{
Data: map[string]interface{}{
"pets": []interface{}{
Expand All @@ -633,8 +635,8 @@ func TestResolveTypeOnUnionYieldsUsefulError(t *testing.T) {
},
},
Errors: []gqlerrors.FormattedError{
{
Message: `Runtime Object type "Human" is not a possible type for "Pet".`,
gqlerrors.FormatError(gqlerrors.Error{
Message: originalError.Message,
Locations: []location.SourceLocation{
{
Line: 2,
Expand All @@ -645,7 +647,8 @@ func TestResolveTypeOnUnionYieldsUsefulError(t *testing.T) {
"pets",
2,
},
},
OriginalError: originalError,
}),
},
}

Expand Down
47 changes: 24 additions & 23 deletions executor_test.go
Expand Up @@ -515,18 +515,19 @@ func TestNullsOutErrorSubtrees(t *testing.T) {
"sync": "sync",
"syncError": nil,
}
expectedErrors := []gqlerrors.FormattedError{
{
Message: "Error getting syncError",
Locations: []location.SourceLocation{
{
Line: 3, Column: 7,
},
},
Path: []interface{}{
"syncError",
originalError := errors.New("Error getting syncError")
expectedErrors := []gqlerrors.FormattedError{gqlerrors.FormatError(gqlerrors.Error{
Message: originalError.Error(),
Locations: []location.SourceLocation{
{
Line: 3, Column: 7,
},
},
Path: []interface{}{
"syncError",
},
OriginalError: originalError,
}),
}

data := map[string]interface{}{
Expand Down Expand Up @@ -1296,6 +1297,7 @@ func TestFailsWhenAnIsTypeOfCheckIsNotMet(t *testing.T) {
},
}

originalError := gqlerrors.NewFormattedError(`Expected value of type "SpecialType" but got: graphql_test.testNotSpecialType.`)
expected := &graphql.Result{
Data: map[string]interface{}{
"specials": []interface{}{
Expand All @@ -1305,21 +1307,20 @@ func TestFailsWhenAnIsTypeOfCheckIsNotMet(t *testing.T) {
nil,
},
},
Errors: []gqlerrors.FormattedError{
{
Message: `Expected value of type "SpecialType" but got: graphql_test.testNotSpecialType.`,
Locations: []location.SourceLocation{
{
Line: 1,
Column: 3,
},
},
Path: []interface{}{
"specials",
1,
Errors: []gqlerrors.FormattedError{gqlerrors.FormatError(gqlerrors.Error{
Message: originalError.Message,
Locations: []location.SourceLocation{
{
Line: 1,
Column: 3,
},
},
},
Path: []interface{}{
"specials",
1,
},
OriginalError: originalError,
})},
}

specialType := graphql.NewObject(graphql.ObjectConfig{
Expand Down
2 changes: 1 addition & 1 deletion gqlerrors/formatted.go
Expand Up @@ -41,7 +41,7 @@ func FormatError(err error) FormattedError {
Message: err.Error(),
Locations: err.Locations,
Path: err.Path,
originalError: err,
originalError: err.OriginalError,
}
if err := err.OriginalError; err != nil {
if extended, ok := err.(ExtendedError); ok {
Expand Down

0 comments on commit bfe0f60

Please sign in to comment.