3 ms·
So far the best error handling pattern I like is actor based (like Erlang for example), where you usually code the main "happy" path separately, where the busin
by rdtsc 12y ago
So far the best error handling pattern I like is actor based (like Erlang for example), where you usually code the main "happy" path separately, where the business logic is cleanly seen, perhaps the error handling is an external supervisor or monitor process. That works for higher level use cases (like handling http requests, parsing, talking to database) and works especially well with heap isolated concurrency units (processes). Maybe it doesn't work for a language that can interface with hardware drivers ...
But still wonder how much of an anti-pattern that would be in Rust -- spawn a separate thread, and then do all the IO, HTTP, XML, RSS parsing in a clean way. Then let it panic and somehow find the reason for panic in the original main thread. Then decide there what to do (exit, log, bubble up failure etc).
- burntsushi 12y agoIt's definitely not idiomatic error handling, but quickcheck uses a mechanism like you speak of: https://github.com/BurntSushi/quickcheck/blob/master/src/tester.rs#L403-L413 https://github.com/BurntSushi/quickcheck/blob/master/src/tes... (This is just implementation details---I'm not saying this is actor based!) This lets it handle failures that panic, and then shrink the witnesses to find (hopefully) minimal witnesses. :-)
- rdtsc 12y agoInteresting, thanks for the link! I'll have to look at that some more. This calls for a Rust actor library perhaps. Memory and type safety in Rust seems like might work well there (making sure memory is not accidentally shared between concurrency units -- really one of the main advantage of Erlang's VM).
- charlieflowers 12y agoWhy do you need a separate thread here? Is Rust's panic tied to the currently executing thread?
- burntsushi 12y agoKinda sorta. My knowledge is limited on the topic, but AFAIK, unwinding for Rust is designed to have at most one try/catch block per thread. That said, there is a `catch_panic` function (http://doc.rust-lang.org/std/thread/fn.catch_panic.html http://doc.rust-lang.org/std/thread/fn.catch_panic.html), but it's unstable. So in stable Rust, the only way to catch a panic is to spawn a new thread, I think.
- pcwalton 12y agoRust's thread library is designed to encourage this kind of programming. In fact, one of the goals of Servo is to use this pattern in a finer-grained way than existing browser engines do. (For example, if an image decoder crashes, wouldn't it be nice if you just got a broken image icon instead of a crashed tab?)
- saurik 12y agoBut with Result encouraged over panic, you can't get to the point of being able to code the main "happy" path without peppering it throughout with calls to unwrap and expansions of try! :(. If this is really how people are being encouraged to develop (and that would be amazing, I just haven't seen it in the Rust code I've seen, which all seems to be closer to how BurntSushi's article deals with error composition), then the error propagation mechanism that developers use as their main way of indicating failure needs to be implicit, something like unchecked exceptions, much closer to panic than Result.