From fca7eb35a1ff214a5c2414b8da4ca50c8d2906df Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 7 Nov 2023 23:54:24 +0300 Subject: [PATCH] neorpc: ensure errors.Is and errors.As work properly with neorpc.Error Close #3188. Signed-off-by: Anna Shaleva --- pkg/neorpc/errors_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 pkg/neorpc/errors_test.go diff --git a/pkg/neorpc/errors_test.go b/pkg/neorpc/errors_test.go new file mode 100644 index 000000000..f9fa21214 --- /dev/null +++ b/pkg/neorpc/errors_test.go @@ -0,0 +1,35 @@ +package neorpc + +import ( + "errors" + "fmt" + "io/fs" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestError_ErrorsAs(t *testing.T) { + err := NewInternalServerError("some error") + wrapped := fmt.Errorf("some meaningful error: %w", err) + + // Check that Error can be used as a target for errors.As: + var actual *Error + require.True(t, errors.As(wrapped, &actual)) + require.Equal(t, "Internal error (-32603) - some error", actual.Error()) + + var bad *fs.PathError + require.False(t, errors.As(wrapped, &bad)) +} + +func TestError_ErrorsIs(t *testing.T) { + err := NewInternalServerError("some error") + wrapped := fmt.Errorf("some meaningful error: %w", err) + + // Check that Error can be recognized via errors.Is: + ref := NewInternalServerError("another server error") + require.True(t, errors.Is(wrapped, ref)) + + // Invalid target type: + require.False(t, errors.Is(wrapped, NewInvalidParamsError("invalid params"))) +}