-
Notifications
You must be signed in to change notification settings - Fork 911
refactor: unify the error handling methods in the crypto package that are different from the project style & use errors.New to replace fmt.Errorf with no parameters #3017
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
Conversation
Signed-off-by: ChengenH <[email protected]>
|
Warning There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure. 🔧 golangci-lint (1.62.2)level=warning msg="[config_reader] The configuration option WalkthroughThe changes in this pull request focus on modifying error handling across multiple files in the Changes
Possibly related PRs
Suggested reviewers
Poem
Tip CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (3)
x/evm/types/utils.go (1)
73-73: LGTM! Consider standardizing error handling patternsThe change to
errors.Newis appropriate for static error messages. However, for consistency, consider using a custom error type or error wrapping for all error cases in this file.Consider creating custom error types for common errors:
var ( ErrNilTransaction = errors.New("invalid tx: nil") ErrInvalidTxType = func(t any) error { return fmt.Errorf("invalid tx type: %T", t) } ErrTxNotFound = func(hash string) error { return fmt.Errorf("eth tx not found: %s", hash) } )x/evm/types/msg.go (1)
230-230: LGTM! Consider adding error documentationThe change to
errors.Newis appropriate. The error handling throughout this file consistently uses error wrapping where context is needed.Consider adding error documentation for common errors:
// Common errors returned by MsgEthereumTx methods var ( // ErrSenderNotDefined is returned when attempting to sign a transaction without a defined sender ErrSenderNotDefined = errors.New("sender address not defined for message") )x/evm/core/tracers/js/goja.go (1)
86-86: LGTM! Consider adding error documentation.The change from
fmt.Errorftoerrors.Newis appropriate since no string formatting is needed. This follows Go best practices for error creation.Consider documenting the scenarios that can trigger this error by adding a comment above the function explaining what constitutes an "invalid buffer type". This would help developers understand and handle the error case better.
+// fromBuf converts a JavaScript value to a byte slice. It accepts: +// - Uint8Array: converted to []byte directly +// - String: converted from hex if allowString is true +// - Array: converted to []byte if elements are valid +// Returns "invalid buffer type" error for other types. func fromBuf(vm *goja.Runtime, bufType goja.Value, buf goja.Value, allowString bool) ([]byte, error) {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (7)
x/evm/core/tracers/js/goja.go(1 hunks)x/evm/core/vm/eips.go(2 hunks)x/evm/core/vm/stack.go(3 hunks)x/evm/types/config.go(2 hunks)x/evm/types/config_testing.go(1 hunks)x/evm/types/msg.go(1 hunks)x/evm/types/utils.go(2 hunks)
🔇 Additional comments (4)
x/evm/types/config.go (1)
13-13: LGTM: Appropriate use of errors.New
The change from fmt.Errorf to errors.New is correct here as the error message is static with no format parameters.
Also applies to: 23-23
x/evm/types/config_testing.go (1)
27-27: LGTM: Maintains consistency with config.go
The error message and usage of errors.New matches config.go, maintaining consistency across the codebase.
x/evm/core/vm/stack.go (1)
20-20: LGTM: Consistent error handling improvements
The changes appropriately replace fmt.Errorf with errors.New for static error messages in both NewStack and NewReturnStack functions. The error messages maintain their descriptive nature while following a consistent pattern for type assertion failures.
Also applies to: 43-43, 129-129
x/evm/core/vm/eips.go (1)
57-60: LGTM! Consider enhancing error tests
The changes to use errors.New for static error messages are appropriate.
Let's verify the test coverage for these error cases:
|
Hey @ChengenH, thanks for the contribution! Could you please sign the CLA? |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3017 +/- ##
===========================================
- Coverage 70.45% 23.51% -46.94%
===========================================
Files 293 435 +142
Lines 22559 88845 +66286
===========================================
+ Hits 15893 20895 +5002
- Misses 5800 66265 +60465
- Partials 866 1685 +819
|
|
Hey @ChengenH could you please also address the linter issues? |
Signed-off-by: ChengenH <[email protected]>
I have added the test cases mentioned in the linter issues, please recheck. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (2)
x/evm/core/vm/stack_test.go (2)
24-55: Consider using table-driven tests and cleanupWhile the test cases are comprehensive, consider these improvements:
- Convert to table-driven tests for better maintainability
- Add cleanup to reset stackPool after the test
Example refactor:
func TestNewStack(t *testing.T) { - stackPool = sync.Pool{ - New: func() interface{} { - return &Stack{} - }, - } - - stack, err := NewStack() - if err != nil { - t.Errorf("Expected no error, got %v", err) - } - if stack == nil { - t.Errorf("Expected a non-nil Stack, got nil") - } - - stackPool = sync.Pool{ - New: func() interface{} { - return "not a Stack" - }, - } + originalPool := stackPool + defer func() { + stackPool = originalPool + }() + + tests := []struct { + name string + poolNew func() interface{} + wantErr string + }{ + { + name: "success", + poolNew: func() interface{} { + return &Stack{} + }, + }, + { + name: "type assertion failure", + poolNew: func() interface{} { + return "not a Stack" + }, + wantErr: "type assertion failure: cannot get Stack pointer from stackPool", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stackPool = sync.Pool{New: tt.poolNew} + + stack, err := NewStack() + if tt.wantErr != "" { + if err == nil || err.Error() != tt.wantErr { + t.Errorf("NewStack() error = %v, wantErr %v", err, tt.wantErr) + } + if stack != nil { + t.Errorf("Expected nil Stack when error, got %v", stack) + } + } else { + if err != nil { + t.Errorf("NewStack() unexpected error = %v", err) + } + if stack == nil { + t.Errorf("Expected non-nil Stack") + } + } + }) + }
57-88: Consider reducing test duplicationThe
TestNewReturnStackfunction is nearly identical toTestNewStackwith just type differences. Consider extracting common test logic to reduce duplication.Example approach using a generic test helper:
+func testPoolCreation[T any](t *testing.T, newFunc func() (*T, error), pool *sync.Pool, validObj T) { + originalPool := *pool + defer func() { + *pool = originalPool + }() + + tests := []struct { + name string + poolNew func() interface{} + wantErr string + }{ + { + name: "success", + poolNew: func() interface{} { + return &validObj + }, + }, + { + name: "type assertion failure", + poolNew: func() interface{} { + return "not valid" + }, + wantErr: fmt.Sprintf("type assertion failure: cannot get %T pointer from pool", validObj), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + *pool = sync.Pool{New: tt.poolNew} + + result, err := newFunc() + if tt.wantErr != "" { + if err == nil || err.Error() != tt.wantErr { + t.Errorf("error = %v, wantErr %v", err, tt.wantErr) + } + if result != nil { + t.Errorf("Expected nil result when error, got %v", result) + } + } else { + if err != nil { + t.Errorf("unexpected error = %v", err) + } + if result == nil { + t.Errorf("Expected non-nil result") + } + } + }) + } +} + func TestNewStack(t *testing.T) { - // Current implementation + testPoolCreation(t, NewStack, &stackPool, Stack{}) } func TestNewReturnStack(t *testing.T) { - // Current implementation + testPoolCreation(t, NewReturnStack, &rStackPool, ReturnStack{}) }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
x/evm/core/vm/stack_test.go(1 hunks)
🔇 Additional comments (1)
x/evm/core/vm/stack_test.go (1)
1-22: LGTM: License and imports are properly structured
The file includes the appropriate license header and necessary imports for testing.
…up to reset stackPool after the test. Signed-off-by: ChengenH <[email protected]>
… are different from the project style Signed-off-by: ChengenH <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (1)
x/evm/core/vm/interpreter.go (1)
260-262: Excellent improvement in error handling robustnessThe change from direct error comparison to
errors.Isis a significant improvement that:
- Enables detection of wrapped errors containing
errStopToken- Follows Go 1.13+ error handling best practices
- Makes the code more maintainable and future-proof
This change goes beyond the PR's original scope (replacing
fmt.Errorfwitherrors.New) but adds valuable robustness to the error handling.Consider applying similar
errors.Isimprovements to other error checks in the codebase for consistency.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
rpc/websockets.go(2 hunks)server/json_rpc.go(2 hunks)wallets/usbwallet/ledger.go(1 hunks)x/evm/core/tracers/native/revertreason.go(2 hunks)x/evm/core/vm/instructions.go(5 hunks)x/evm/core/vm/interpreter.go(2 hunks)
🔇 Additional comments (10)
x/evm/core/vm/instructions.go (4)
708-708: LGTM: Improved error handling in opCall
The change from direct error comparison to errors.Is() follows Go best practices for error handling and allows for proper detection of wrapped errors.
744-744: LGTM: Improved error handling in opCallCode
The change from direct error comparison to errors.Is() follows Go best practices for error handling and allows for proper detection of wrapped errors.
773-773: LGTM: Improved error handling in opDelegateCall
The change from direct error comparison to errors.Is() follows Go best practices for error handling and allows for proper detection of wrapped errors.
802-802: LGTM: Improved error handling in opStaticCall
The change from direct error comparison to errors.Is() follows Go best practices for error handling and allows for proper detection of wrapped errors.
x/evm/core/vm/interpreter.go (1)
20-20: LGTM: Import addition follows standard Go conventions
The addition of the errors package import follows standard Go import ordering (standard library imports first) and is necessary for the improved error handling implementation.
server/json_rpc.go (1)
6-6: LGTM! Improved error handling using errors.Is
The change from direct error comparison to errors.Is is a good improvement as it:
- Handles wrapped errors correctly
- Follows Go best practices for error handling
- Makes the code more maintainable
Also applies to: 91-91
x/evm/core/tracers/native/revertreason.go (1)
22-22: LGTM! Enhanced error handling with errors.Is
The change from direct error comparison to errors.Is is a good improvement as it:
- Handles wrapped errors correctly
- Follows Go best practices for error handling
- Makes the code more maintainable
Also applies to: 62-62
wallets/usbwallet/ledger.go (1)
93-93: LGTM! Better error handling using errors.Is
The change from direct error comparison to errors.Is is a good improvement as it:
- Handles wrapped errors correctly
- Follows Go best practices for error handling
- Makes the code more maintainable
rpc/websockets.go (2)
9-9: LGTM: Import added for errors package
The addition of the errors import is necessary for the enhanced error handling using errors.Is.
110-110: LGTM: Improved error handling with errors.Is
The change from direct error comparison to errors.Is follows Go best practices for error handling. This improvement allows for proper detection of http.ErrServerClosed even when it's wrapped by middleware or other error chains.
|
@ChengenH Thanks for the contribution! Please address the linter errors |
Co-authored-by: Tom <[email protected]> Signed-off-by: Andi <[email protected]>
Done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
x/evm/core/vm/stack_test.go (2)
24-75: Consider refactoring the helper function for better maintainability.The
runTestfunction could benefit from some refactoring to improve maintainability and reduce code duplication:Consider splitting the function into smaller, more focused helpers:
-func runTest(t *testing.T, name string, newFunc func() interface{}, expectErr bool, errMsg string, testFunc interface{}, pool *sync.Pool) { +// Generic test result checker +func checkTestResult[T any](t *testing.T, result T, err error, expectErr bool, errMsg string) { + if expectErr { + if err == nil { + t.Errorf("Expected error, got nil") + } else if err.Error() != errMsg { + t.Errorf("Expected error message %q, got %q", errMsg, err.Error()) + } + if result != nil { + t.Errorf("Expected nil result, got %v", result) + } + return + } + if err != nil { + t.Errorf("Expected no error, got %v", err) + } + if result == nil { + t.Errorf("Expected a non-nil result, got nil") + } +} + +func runTest(t *testing.T, name string, newFunc func() interface{}, expectErr bool, errMsg string, testFunc interface{}, pool *sync.Pool) { t.Run(name, func(t *testing.T) { originalPool := *pool defer func() { *pool = originalPool }() pool.New = newFunc switch tf := testFunc.(type) { case func() (*Stack, error): stack, err := tf() - if expectErr { - if err == nil { - t.Errorf("Expected error, got nil") - } else if err.Error() != errMsg { - t.Errorf("Expected error message %q, got %q", errMsg, err.Error()) - } - if stack != nil { - t.Errorf("Expected nil Stack, got %v", stack) - } - } else { - if err != nil { - t.Errorf("Expected no error, got %v", err) - } - if stack == nil { - t.Errorf("Expected a non-nil Stack, got nil") - } - } + checkTestResult(t, stack, err, expectErr, errMsg) case func() (*ReturnStack, error): rStack, err := tf() - if expectErr { - if err == nil { - t.Errorf("Expected error, got nil") - } else if err.Error() != errMsg { - t.Errorf("Expected error message %q, got %q", errMsg, err.Error()) - } - if rStack != nil { - t.Errorf("Expected nil ReturnStack, got %v", rStack) - } - } else { - if err != nil { - t.Errorf("Expected no error, got %v", err) - } - if rStack == nil { - t.Errorf("Expected a non-nil ReturnStack, got nil") - } - } + checkTestResult(t, rStack, err, expectErr, errMsg) default: t.Fatalf("Unexpected test function signature") } }) }This refactoring:
- Extracts the common test result checking logic into a generic helper
- Reduces code duplication
- Makes the code more maintainable
- Improves readability
102-125: Consider reducing duplication between test functions.While the implementation is correct, there's an opportunity to reduce duplication between
TestNewStackandTestNewReturnStack.Consider creating a generic test function:
+type stackTestCase struct { + name string + newFunc func() interface{} + expectErr bool + errMsg string +} + +func runStackTest(t *testing.T, tests []stackTestCase, newStackFunc interface{}, pool *sync.Pool) { + for _, tt := range tests { + runTest(t, tt.name, tt.newFunc, tt.expectErr, tt.errMsg, newStackFunc, pool) + } +} + func TestNewStack(t *testing.T) { - tests := []struct { - name string - newFunc func() interface{} - expectErr bool - errMsg string - }{ + tests := []stackTestCase{ { name: "Valid Stack allocation", newFunc: func() interface{} { return &Stack{} }, expectErr: false, }, { name: "Invalid Stack allocation", newFunc: func() interface{} { return "not a Stack" }, expectErr: true, errMsg: "type assertion failure: cannot get Stack pointer from stackPool", }, } - for _, tt := range tests { - runTest(t, tt.name, tt.newFunc, tt.expectErr, tt.errMsg, NewStack, &stackPool) - } + runStackTest(t, tests, NewStack, &stackPool) }This would:
- Reduce code duplication
- Make it easier to add new stack-related tests
- Keep the test structure consistent
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
x/evm/core/vm/stack_test.go(1 hunks)
🔇 Additional comments (2)
x/evm/core/vm/stack_test.go (2)
17-22: LGTM! Package declaration and imports are correct.
The package is correctly declared as vm_test and imports are minimal and appropriate for the test requirements.
77-100: LGTM! Well-structured table-driven tests.
The test implementation follows good practices:
- Comprehensive test cases covering both success and failure scenarios
- Clear test case names and structure
- Error messages align with the project's unified error handling approach
|
There are some compilation errors. Please address these |
Summary by CodeRabbit
New Features
Bug Fixes
fmt.Errorfwitherrors.Newin various methods, ensuring clearer error reporting.errors.Isfor better handling of wrapped errors.Chores
fmtin several files to streamline code.Tests
NewStack()andNewReturnStack()functions to ensure proper functionality and error handling.