From 70484eac6547ecc5f8837ac745e0ad4015f32bdc Mon Sep 17 00:00:00 2001 From: Antoine Huret Date: Tue, 4 Jan 2022 17:47:53 +0100 Subject: [PATCH] added some informations when hitting ErrInvalidType using fmt.Errorf + %w that wraps the error changed tests and now use errors.Is function --- request.go | 2 +- request_test.go | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/request.go b/request.go index f665857..d94eeeb 100644 --- a/request.go +++ b/request.go @@ -430,7 +430,7 @@ func unmarshalAttribute( // As a final catch-all, ensure types line up to avoid a runtime panic. if fieldValue.Kind() != value.Kind() { - err = ErrInvalidType + err = fmt.Errorf("%w: %s is of type %s, cannot set %s", ErrInvalidType, structField.Name, fieldValue.Type().String(), value.Type().String()) return } diff --git a/request_test.go b/request_test.go index 300c7de..71c6554 100644 --- a/request_test.go +++ b/request_test.go @@ -281,14 +281,13 @@ func TestUnmarshalInvalidJSON_BadType(t *testing.T) { out := new(ModelBadTypes) in := map[string]interface{}{} in[test.Field] = test.BadValue - expectedErrorMessage := test.Error.Error() err := UnmarshalPayload(samplePayloadWithBadTypes(in), out) if err == nil { t.Fatalf("Expected error due to invalid type.") } - if err.Error() != expectedErrorMessage { + if !errors.Is(err, test.Error) { t.Fatalf("Unexpected error message: %s", err.Error()) } }) @@ -959,7 +958,7 @@ func TestUnmarshalCustomTypeAttributes_ErrInvalidType(t *testing.T) { t.Fatal("Expected an error unmarshalling the payload due to type mismatch, got none") } - if err != ErrInvalidType { + if !errors.Is(err, ErrInvalidType) { t.Fatalf("Expected error to be %v, was %v", ErrInvalidType, err) } }