Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for vektah/gqlparser#281 #282

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gqlerror/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (errs List) Is(target error) bool {

func (errs List) As(target interface{}) bool {
for _, err := range errs {
if errors.As(err, &target) {
if errors.As(err, target) {
return true
}
}
Expand Down
138 changes: 138 additions & 0 deletions gqlerror/error_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
package gqlerror

import (
"reflect"
"testing"

"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser/v2/ast"
)

type testError struct {
message string
}

func (e testError) Error() string {
return e.message
}

var (
underlyingError = testError{
"Underlying error",
}

error1 = &Error{
Message: "Some error 1",
}
error2 = &Error{
Err: underlyingError,
Message: "Some error 2",
}
)

func TestErrorFormatting(t *testing.T) {
t.Run("without filename", func(t *testing.T) {
err := ErrorLocf("", 66, 2, "kabloom")
Expand All @@ -28,3 +51,118 @@
require.Equal(t, `input: a[1].b kabloom`, err.Error())
})
}

func TestList_As(t *testing.T) {
t.Parallel()

tests := []struct {
name string
errs List
target any
wantsTarget any
targetFound bool
}{
{
name: "Empty list",

Check failure on line 66 in gqlerror/error_test.go

View workflow job for this annotation

GitHub Actions / Go Lint

File is not `goimports`-ed (goimports)
errs: List{},
},
{
name: "List with one error",
errs: List{error1},
target: new(*Error),
wantsTarget: &error1,
targetFound: true,
},
{
name: "List with multiple errors 1",
errs: List{error1, error2},
target: new(*Error),
wantsTarget: &error1,
targetFound: true,
},
{
name: "List with multiple errors 2",
errs: List{error1, error2},
target: new(testError),
wantsTarget: &underlyingError,
targetFound: true,
},
}

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

targetFound := tt.errs.As(tt.target)

if targetFound != tt.targetFound {
t.Errorf("List.As() = %v, want %v", targetFound, tt.targetFound)
}

if tt.targetFound && !reflect.DeepEqual(tt.target, tt.wantsTarget) {
t.Errorf("target = %v, want %v", tt.target, tt.wantsTarget)
}
})
}
}

func TestList_Is(t *testing.T) {
t.Parallel()

tests := []struct {
name string
errs List
target error
hasMatchingError bool
}{
{
name: "Empty list",
errs: List{},
target: new(Error),
hasMatchingError: false,
},
{
name: "List with one error",
errs: List{
error1,
},
target: error1,
hasMatchingError: true,
},
{
name: "List with multiple errors 1",
errs: List{
error1,
error2,
},
target: error2,
hasMatchingError: true,
},
{
name: "List with multiple errors 2",
errs: List{
error1,
error2,
},
target: underlyingError,
hasMatchingError: true,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

hasMatchingError := tt.errs.Is(tt.target)
if hasMatchingError != tt.hasMatchingError {
t.Errorf("List.Is() = %v, want %v", hasMatchingError, tt.hasMatchingError)
}
if hasMatchingError && tt.target == nil {
t.Errorf("List.Is() returned nil target, wants concrete error")
}
})
}
}
Loading