Skip to content

Commit

Permalink
fix for #281 (#282)
Browse files Browse the repository at this point in the history
Co-authored-by: Jan Zombik <[email protected]>
  • Loading branch information
Metamogul and Jan Zombik authored Sep 15, 2023
1 parent d734cf8 commit cb1d657
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 1 deletion.
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 @@ func TestErrorFormatting(t *testing.T) {
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",
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")
}
})
}
}

0 comments on commit cb1d657

Please sign in to comment.