Idiomatic error propagation
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 25/100
- Loại issue
- Tài liệu
- Độ rõ ràng
- Cần làm rõ
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- rust
- Lĩnh vực
- backend-api-design
Hướng nghiên cứu
Bắt đầu với hai ví dụ Rust Playground được liên kết và các enum Error, ErrorA và ErrorB được hiển thị trong issue. So sánh cách tiếp cận downcast hiện tại của anyhow với cách tiếp cận forwarding được đề xuất của thiserror, tập trung vào cách các caller match với các variant lồng nhau của std::io::Error. Công việc được xem là hoàn tất khi dự án có một khuyến nghị rõ ràng, được ghi lại tài liệu, dành cho các caller cấp cao.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Currently, we have a number of crates where we have a top-level Error enum, but our functions return anyhow::Error instead of the enum. It's been pointed out that this is not idiomatic and it would be better to just return Error and use thiserror's transparent forwarding. I'm confused about how to idiomatically handle matching on forwarded error variants. Consider the following crate structure:
high-level
/ \
low-level-a low-level-b
where high-level defines Error, low-level-a defines ErrorA and low-level-b defines ErrorB. If each of the enums includes an IoError(std::io::Error) variant, how is code supposed to match on the std::io::Error? To make the question concrete, consider this code, which is what I currently imagine:
#[derive(thiserror::Error, Debug)]
enum Error {
#[error("something bad happened: {0}")]
Bad(String),
#[error(transparent)]
IoError(#[from] std::io::Error),
#[error(transparent)]
A(ErrorA),
#[error(transparent)]
B(ErrorB),
}
#[derive(thiserror::Error, Debug)]
enum ErrorA {
#[error(transparent)]
IoError(#[from] std::io::Error),
}
#[derive(thiserror::Error, Debug)]
enum ErrorB {
#[error(transparent)]
IoError(#[from] std::io::Error),
}
fn f() -> Result<(), Error> {
Err(Error::Bad("ouch".into()))
}
fn main() {
match f() {
Ok(()) => {
}
Err(Error::IoError(err))
| Err(Error::A(ErrorA::IoError(err)))
| Err(Error::B(ErrorB::IoError(err))) => {
// Handle the io error.
eprintln!("io error: {}", err);
}
Err(err) => {
eprintln!("An error occured: {}", err);
}
}
}
This is how we currently do it:
#[derive(thiserror::Error, Debug)]
enum Error {
#[error("something bad happened: {0}")]
Bad(String),
}
#[derive(thiserror::Error, Debug)]
enum ErrorA {
}
#[derive(thiserror::Error, Debug)]
enum ErrorB {
}
fn f() -> anyhow::Result<()> {
Err(std::io::Error::new(std::io::ErrorKind::Other, "oh no!").into())
}
fn main() {
let result = f();
match result {
Err(err) => {
if let Some(err) = err.downcast_ref::<std::io::Error>() {
// Handle the io error.
eprintln!("io error: {}", err);
} else {
eprintln!("Not an io error: {}", err);
}
}
Ok(()) => {
eprintln!("Everything is fine")
}
}
}
That is, we downcast to std::io::Error and it doesn't matter if the std::io::Error comes from high-level, low-level-a, low-level-b, or another crate that high-level starts using later: the user of the high-level API can reliably and compactly catch std::io::Errors.
Thanks for any insights!
- Ngôn ngữ chính
- Rust
- Star
- 5.6k
- Fork
- 220
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của dtolnay/thiserror
-
Improve #[from] documentation example.Có thể đã có người làm @SatvikMishra08 đã nhận 50 ngày trước. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 62/100
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
-
No `unused field` warnings are produced on enums deriving thiserror::ErrorCó thể đã có người làm @usmanovbf đã nhận 240 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 30/100
Tất cả issue của dtolnay/thiserror
Issue tương tự
-
Selected chart highlights only its values, not its series names and category labelsCó thể đã có người làm @Higgs32584 đã nhận hôm nay. Đang mởbug priority: low ui
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
storytold/gridcraft#4390 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug bugfix priority: medium
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
quarto-dev/q2#1219 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100