-
Notifications
You must be signed in to change notification settings - Fork 40
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
chore: add error handling #1560
base: main
Are you sure you want to change the base?
Conversation
WalkthroughRecent changes enhance error handling across various parts of the codebase. Notably, Changes
Poem
Tip Early access features: enabledWe are currently testing the following features in early access:
Note:
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 as PR comments)
Additionally, you can add CodeRabbit Configration 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: 2
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: 1
Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Files selected for processing (1)
- mod/cli/pkg/commands/genesis/deposit.go (1 hunks)
Additional comments not posted (4)
mod/cli/pkg/commands/genesis/deposit.go (4)
175-175
: Enhanced error handling in file operations.The addition of detailed error messages when opening, marshaling, and writing to a file enhances the traceability of errors. This is a solid improvement in error handling practices.
Also applies to: 189-189, 194-194
Line range hint
34-34
: Robust error handling throughout the command setup.The command setup includes comprehensive error handling at every step, from initializing node validator files to verifying the deposit message. This thorough approach ensures that any issues are caught and handled appropriately, aligning with the PR's objectives.
Also applies to: 50-50, 58-58, 66-66, 74-74, 82-82, 90-90, 98-98, 106-106, 114-114, 122-122, 130-130, 138-138, 146-146, 154-154, 162-162
Line range hint
149-149
: Proper error handling in directory creation.The function
makeOutputFilepath
handles potential errors during directory creation effectively, providing clear error messages if the operation fails. This is consistent with best practices in error handling.Also applies to: 151-151
197-197
: Correct use of dependency injection ingetBLSSigner
.The function
getBLSSigner
effectively uses dependency injection to retrieve the BLS signer, handling errors appropriately. This implementation is clean and follows best practices.
[APROVED]Also applies to: 199-199
35c6024
to
57695e4
Compare
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.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1560 +/- ##
==========================================
+ Coverage 22.15% 25.07% +2.92%
==========================================
Files 266 258 -8
Lines 11771 12074 +303
Branches 18 0 -18
==========================================
+ Hits 2608 3028 +420
+ Misses 9048 8884 -164
- Partials 115 162 +47
|
Hi @itsdevbear @nidhi-singh02 please take a look |
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
Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Files selected for processing (2)
- mod/cli/pkg/commands/genesis/deposit.go (1 hunks)
- mod/cli/pkg/commands/genesis/errors.go (1 hunks)
Additional comments not posted (3)
mod/cli/pkg/commands/genesis/errors.go (1)
25-37
: Centralized error declarations are well-defined and clear.The error declarations are specific and appropriately named, which enhances maintainability and readability. This approach also makes it easier to handle errors consistently across the
genesis
package.mod/cli/pkg/commands/genesis/deposit.go (2)
175-175
: Enhanced error handling with contextual information.The updated error handling in
writeDepositToFile
uses the centralized error variables effectively, providing more context by including theoutputDocument
anddepositMessage
in the error messages. This approach aids in debugging and traceability.Also applies to: 189-189, 194-194
178-185
: Consider logging file closure errors independently.The current logic only sets the file closure error if no other error has occurred. It might be beneficial to log this error independently to ensure that file closure issues are always captured, which could be critical for diagnosing issues in production environments.
defer func() { if cerr := outputFile.Close(); cerr != nil { + fmt.Printf("Error closing file: %v\n", cerr) // Consider replacing with structured logging if err == nil { err = errors.Newf("failed to close file: %w", cerr) } } }()
cc73744
to
80d984f
Compare
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.
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.
lgtm
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.
|
||
bz, err := json.Marshal(depositMessage) | ||
if err != nil { | ||
return err | ||
return errors.Wrapf(ErrMarshalDepositMessage, "%v, %w", depositMessage, err) |
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.
Enhance error message for marshaling failure.
The error message here could also benefit from more details about the data being marshaled, which could help in debugging.
- return errors.Wrapf(ErrMarshalDepositMessage, "%v, %w", depositMessage, err)
+ return errors.Wrapf(ErrMarshalDepositMessage, "failed to marshal deposit message %v: %w", depositMessage, err)
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
return errors.Wrapf(ErrMarshalDepositMessage, "%v, %w", depositMessage, err) | |
return errors.Wrapf(ErrMarshalDepositMessage, "failed to marshal deposit message %v: %w", depositMessage, err) |
_, err = fmt.Fprintf(outputFile, "%s\n", bz) | ||
if err != nil { | ||
return errors.Wrapf(ErrWriteFile, "%w", err) |
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.
Improve error message for write failures.
Similar to the open and marshal errors, including the file path in the error message when a write fails can aid in troubleshooting.
- return errors.Wrapf(ErrWriteFile, "%w", err)
+ return errors.Wrapf(ErrWriteFile, "failed to write to file %s: %w", outputDocument, err)
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
return errors.Wrapf(ErrWriteFile, "%w", err) | |
return errors.Wrapf(ErrWriteFile, "failed to write to file %s: %w", outputDocument, err) |
@@ -174,19 +174,29 @@ | |||
0o644, //nolint:mnd // file permissions. | |||
) | |||
if err != nil { | |||
return err | |||
return errors.Wrapf(ErrOpenFile, "%s: %w", outputDocument, err) |
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.
Improve error message for better traceability.
The error message for failing to open a file is good, but it could include the file path for better traceability.
- return errors.Wrapf(ErrOpenFile, "%s: %w", outputDocument, err)
+ return errors.Wrapf(ErrOpenFile, "failed to open file %s: %w", outputDocument, err)
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
return errors.Wrapf(ErrOpenFile, "%s: %w", outputDocument, err) | |
return errors.Wrapf(ErrOpenFile, "failed to open file %s: %w", outputDocument, err) |
defer func() { | ||
if cerr := outputFile.Close(); cerr != nil { | ||
// If there's no error yet, set it to the close error | ||
if err == nil { | ||
err = errors.Wrapf(ErrCloseFile, "%w", cerr) | ||
} | ||
} | ||
}() |
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.
Consider logging file closure errors independently.
The current logic only sets the file closure error if no other error has occurred. It might be beneficial to log this error independently to ensure that file closure issues are always captured, which could be critical for diagnosing issues in production environments.
defer func() {
if cerr := outputFile.Close(); cerr != nil {
+ fmt.Printf("Error closing file: %v\n", cerr) // Consider replacing with structured logging
if err == nil {
err = errors.Wrapf(ErrCloseFile, "%w", cerr)
}
}
}()
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
defer func() { | |
if cerr := outputFile.Close(); cerr != nil { | |
// If there's no error yet, set it to the close error | |
if err == nil { | |
err = errors.Wrapf(ErrCloseFile, "%w", cerr) | |
} | |
} | |
}() | |
defer func() { | |
if cerr := outputFile.Close(); cerr != nil { | |
fmt.Printf("Error closing file: %v\n", cerr) // Consider replacing with structured logging | |
// If there's no error yet, set it to the close error | |
if err == nil { | |
err = errors.Wrapf(ErrCloseFile, "%w", cerr) | |
} | |
} | |
}() |
Description
Add missing error handling and proper logging.
Summary by CodeRabbit
Set
method in the storage module to handle file closing errors more robustly by logging any encountered errors.