Skip to content

Commit

Permalink
executor: adds OriginalError tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-ramon committed Dec 3, 2018
1 parent bfe0f60 commit 7995f6a
Showing 1 changed file with 76 additions and 4 deletions.
80 changes: 76 additions & 4 deletions executor_test.go
Expand Up @@ -2046,7 +2046,7 @@ func (err extendedError) Extensions() map[string]interface{} {

var _ gqlerrors.ExtendedError = &extendedError{}

func testErrors(t *testing.T, nameType graphql.Output, extensions map[string]interface{}) *graphql.Result {
func testErrors(t *testing.T, nameType graphql.Output, extensions map[string]interface{}, formatErrorFn func(err error) error) *graphql.Result {
type Hero struct {
Id string `graphql:"id"`
Name string
Expand All @@ -2073,7 +2073,12 @@ func testErrors(t *testing.T, nameType graphql.Output, extensions map[string]int
if hero.Name != "" {
return hero.Name, nil
}

err := fmt.Errorf("Name for character with ID %v could not be fetched.", hero.Id)
if formatErrorFn != nil {
err = formatErrorFn(err)
}

if extensions != nil {
return nil, &extendedError{
error: err,
Expand Down Expand Up @@ -2134,7 +2139,7 @@ func testErrors(t *testing.T, nameType graphql.Output, extensions map[string]int

// http://facebook.github.io/graphql/June2018/#example-bc485
func TestQuery_ErrorPath(t *testing.T) {
result := testErrors(t, graphql.String, nil)
result := testErrors(t, graphql.String, nil, nil)

assertJSON(t, `{
"errors": [
Expand Down Expand Up @@ -2168,7 +2173,7 @@ func TestQuery_ErrorPath(t *testing.T) {

// http://facebook.github.io/graphql/June2018/#example-08b62
func TestQuery_ErrorPathForNonNullField(t *testing.T) {
result := testErrors(t, graphql.NewNonNull(graphql.String), nil)
result := testErrors(t, graphql.NewNonNull(graphql.String), nil, nil)

assertJSON(t, `{
"errors": [
Expand Down Expand Up @@ -2202,7 +2207,7 @@ func TestQuery_ErrorExtensions(t *testing.T) {
result := testErrors(t, graphql.NewNonNull(graphql.String), map[string]interface{}{
"code": "CAN_NOT_FETCH_BY_ID",
"timestamp": "Fri Feb 9 14:33:09 UTC 2018",
})
}, nil)

assertJSON(t, `{
"errors": [
Expand Down Expand Up @@ -2233,3 +2238,70 @@ func TestQuery_ErrorExtensions(t *testing.T) {
}
}`, result)
}

func TestQuery_OriginalErrorBuiltin(t *testing.T) {
result := testErrors(t, graphql.String, nil, nil)
originalError := result.Errors[0].OriginalError()
switch originalError.(type) {
case error:
default:
t.Fatalf("unexpected error: %v", reflect.TypeOf(originalError))
}
}

func TestQuery_OriginalErrorExtended(t *testing.T) {
result := testErrors(t, graphql.String, map[string]interface{}{
"code": "CAN_NOT_FETCH_BY_ID",
}, nil)
originalError := result.Errors[0].OriginalError()
switch originalError.(type) {
case *extendedError:
case extendedError:
default:
t.Fatalf("unexpected error: %v", reflect.TypeOf(originalError))
}
}

type customError struct {
error
}

func (e customError) Error() string {
return e.error.Error()
}

func TestQuery_OriginalErrorCustom(t *testing.T) {
result := testErrors(t, graphql.String, nil, func(err error) error {
return customError{error: err}
})
originalError := result.Errors[0].OriginalError()
switch originalError.(type) {
case customError:
default:
t.Fatalf("unexpected error: %v", reflect.TypeOf(originalError))
}
}

func TestQuery_OriginalErrorCustomPtr(t *testing.T) {
result := testErrors(t, graphql.String, nil, func(err error) error {
return &customError{error: err}
})
originalError := result.Errors[0].OriginalError()
switch originalError.(type) {
case *customError:
default:
t.Fatalf("unexpected error: %v", reflect.TypeOf(originalError))
}
}

func TestQuery_OriginalErrorPanic(t *testing.T) {
result := testErrors(t, graphql.String, nil, func(err error) error {
panic(errors.New("panic error"))
})
originalError := result.Errors[0].OriginalError()
switch originalError.(type) {
case error:
default:
t.Fatalf("unexpected error: %v", reflect.TypeOf(originalError))
}
}

0 comments on commit 7995f6a

Please sign in to comment.