Skip to content

Commit ba52787

Browse files
author
Gael du Plessix
committed
Include stack trace in errors
1 parent 1c504cf commit ba52787

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

executor.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"reflect"
8+
"runtime/debug"
89
"strings"
910

1011
"github.com/graphql-go/graphql/gqlerrors"
@@ -61,7 +62,9 @@ func Execute(p ExecuteParams) (result *Result) {
6162
if r, ok := r.(error); ok {
6263
err = gqlerrors.FormatError(r)
6364
}
64-
exeContext.Errors = append(exeContext.Errors, gqlerrors.FormatError(err))
65+
formattedErr := gqlerrors.FormatError(err)
66+
formattedErr.StackTrace = string(debug.Stack())
67+
exeContext.Errors = append(exeContext.Errors, formattedErr)
6568
result.Errors = exeContext.Errors
6669
select {
6770
case out <- result:

gqlerrors/formatted.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
)
88

99
type FormattedError struct {
10-
Message string `json:"message"`
11-
Locations []location.SourceLocation `json:"locations"`
10+
Message string `json:"message"`
11+
Locations []location.SourceLocation `json:"locations"`
12+
StackTrace string `json:"-"`
1213
}
1314

1415
func (g FormattedError) Error() string {

nonnull_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,10 +1032,19 @@ func TestNonNull_NullsTheTopLevelIfSyncNonNullableFieldThrows(t *testing.T) {
10321032
if len(result.Errors) != len(expected.Errors) {
10331033
t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors))
10341034
}
1035+
ignoreStackTraces(result)
10351036
if !reflect.DeepEqual(expected, result) {
1037+
t.Logf("=> err: %v", result.Errors[0].StackTrace)
10361038
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
10371039
}
10381040
}
1041+
1042+
func ignoreStackTraces(res *graphql.Result) {
1043+
for i, _ := range res.Errors {
1044+
res.Errors[i].StackTrace = ""
1045+
}
1046+
}
1047+
10391048
func TestNonNull_NullsTheTopLevelIfSyncNonNullableFieldErrors(t *testing.T) {
10401049
doc := `
10411050
query Q { nonNullPromise }
@@ -1064,6 +1073,7 @@ func TestNonNull_NullsTheTopLevelIfSyncNonNullableFieldErrors(t *testing.T) {
10641073
if len(result.Errors) != len(expected.Errors) {
10651074
t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors))
10661075
}
1076+
ignoreStackTraces(result)
10671077
if !reflect.DeepEqual(expected, result) {
10681078
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
10691079
}
@@ -1096,6 +1106,7 @@ func TestNonNull_NullsTheTopLevelIfSyncNonNullableFieldReturnsNull(t *testing.T)
10961106
if len(result.Errors) != len(expected.Errors) {
10971107
t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors))
10981108
}
1109+
ignoreStackTraces(result)
10991110
if !reflect.DeepEqual(expected, result) {
11001111
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
11011112
}
@@ -1128,6 +1139,7 @@ func TestNonNull_NullsTheTopLevelIfSyncNonNullableFieldResolvesNull(t *testing.T
11281139
if len(result.Errors) != len(expected.Errors) {
11291140
t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors))
11301141
}
1142+
ignoreStackTraces(result)
11311143
if !reflect.DeepEqual(expected, result) {
11321144
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
11331145
}

0 commit comments

Comments
 (0)