-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3192 from nspcc-dev/rpc-err
neorpc: ensure errors.Is and errors.As work properly with neorpc.Error
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"))) | ||
} |