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

Rust错误处理实践 #160

Open
AlexiaChen opened this issue May 19, 2022 · 0 comments
Open

Rust错误处理实践 #160

AlexiaChen opened this issue May 19, 2022 · 0 comments
Labels
rust Rust programming language

Comments

@AlexiaChen
Copy link
Owner

AlexiaChen commented May 19, 2022

简单来说就是library用thiserror这个crate, binary工程用anyhow这个crate。

library 一般是给外部调用,所以需要尽可能enum一个对外易懂的错误,因为library的错误是API的一部分,caller需要知道该怎么处理错误,是错误的消费者。所以对于library,尽可能不用这样的返回Boxed错误 Result<String, Box<dyn std::error::Error>> , 目前就是用thiserror了,自己enum出来一些你自定义的Error返回出去,让caller方便处理。至于binary的话,因为再上一层就是终端用户了,用Boxed Error也是可以的,大部分错误都可以不用处理,而是打印日志或者弹出错误框什么的。

References

@AlexiaChen AlexiaChen added the rust Rust programming language label May 19, 2022
@AlexiaChen AlexiaChen reopened this Aug 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rust Rust programming language
Projects
None yet
Development

No branches or pull requests

1 participant