Skip to content
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

[NEVER MERGE] Example of migration to unified failure handling system #428

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

sayon
Copy link

@sayon sayon commented Nov 25, 2024

This PR demonstrates how to adopt the PoC for the error handling system.

How to play with it

  1. Build the CLI from https://github.com/sayon/error-codegen-poc. It will produce an executable: zksync-error-codegen-cli
  2. Navigate to the root of era-test-node and execute:
    <path-to-zksync-error-codegen-cli> --definitions root.json --backend rust --verbose  --output=../zksync_error
    

This will generate a crate zksync_error in the directory ../zksync_error with all the errors defined in root.json and errors-era-test-node.json.

A proper way of doing it would be to convert era-test-node to workspace first and place zksync_error in the root, but this is a quick demo.

The root.json file is only for demonstration -- in production, it will be unique for zksync ecosystem and stored in a different, shared repository. The error definitions for era-test-node are located in errors-era-test-node.json.

Changes

NB: This is illustrative: the exact DSL in use can be polished and changed in other ways if you like the approach in general.

  1. The first commit migrates the top level errors from anyhow to the unified framework. It wraps everything in a wildcard "TestNodeError" defined in errors-era-test-node.json

Now if we run touch era_test_node.log && chmod 000 era_test_node.log && cargo run we will see:

Error: SerializedError { code: 20001, message: "[core-TestNode-1] Some error in EraTestNode happened: Permission denied (os error 13).", data: Object {"Core": Object {"TestNode": Object {"TestNodeError": Object {"inner": String("Permission denied (os error 13)")}}}} }
  1. The second commit introduces a custom error type LogFileCreationError and changes main.rs so that failure of log creation results in this custom error , rather than a wildcard.

Now if we run touch era_test_node.log && chmod 000 era_test_node.log && cargo run we will see:

 Error: SerializedError { code: 21002, message: "[core-TestNode-2] Unable to create log file era_test_node.log: Permission denied (os error 13)", data: Object {"Core": Object {"TestNode": Object {"LogFileCreationError": Object {"inner": String("Permission denied (os error 13)"), "path": String("era_test_node.log")}}}} }

The exact output can be of course tuned to (conditionally) hide the technical details.

@sayon sayon requested a review from a team as a code owner November 25, 2024 11:25
@sayon sayon marked this pull request as draft November 25, 2024 11:34
@sayon sayon changed the title Example of migration to unified failure handling system [NEVER MERGE] Example of migration to unified failure handling system Nov 25, 2024
src/main.rs Outdated Show resolved Hide resolved
let (_, client) = ForkDetails::fork_network_and_client("mainnet")
.map_err(|e| anyhow!("Failed to initialize client: {:?}", e))?;
let (_, client) = ForkDetails::fork_network_and_client("mainnet").map_err(|e| {
generic_error_msg(format!("Failed to initialize client: {e:?}"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could genereric_error_msg be a 'macro' -- so that I can direclty replace anyhow! (and dont' have to run the 'format' inside?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! For the PoC we have deliberately tried to not have any macro magic to keep it simple.

@dutterbutter dutterbutter added the do not merge 🛑 Do not merge this PR until label is removed label Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do not merge 🛑 Do not merge this PR until label is removed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants