Error Handling for Large Rust Projects

105 points tison1096

https://greptime.com/blogs/2024-05-07-error-rust

We build an error handling solution with stack trace for large Rust projects like GreptimeDB. In practices, it display errors like:

Failed to handle protocol
0: Failed to handle incoming content, query: blabla, at src/protocol/handler.rs:89:22
1: Failed to reading next message at queue 5 of 10, at src/protocol/loop.rs:254:14
2: Failed to decode `01010001001010001` to ProtocolHeader, at src/protocol/codec.rs:90:14
3: serde_json(invalid character at position 1)

Previously, errors in Rust mainly report the root cause, but it lacks a lot of context with which developers can debug the error. Moreover, std Backtrace brings a lot of unrelated intermediate stacks and increases a significant runtime overhead.

Our solution is built upon snafu, which is like a combination of thiserror and anyhow, with less than 1% runtime overhead and binary size.

Check it out the blog for the design, implementation, and how it can benefit you: https://greptime.com/blogs/2024-05-07-error-rust