3 ms·
"goodResult | barError | bazError" method just don't scale; eventually, especially if using third-party libraries, you may end up with dozens of not hundreds of
by aldanor 3y ago
"goodResult | barError | bazError" method just don't scale; eventually, especially if using third-party libraries, you may end up with dozens of not hundreds of error types this way.
The way Rust does it with its try operator (?), you have to tell it if it's barError (in which case it's up to you to tell it how bazError is convertible to it), or perhaps bazError, or something else altogether (e.g. sum type like you suggested, but conversion still needs to be supplied). There's also some libraries that provide error types for the lazy that are convertible from everything, so you can use try operator without much thinking, but for libraries you almost never use them.
- valenterry 3y agoI use exactly that way of error handling and I find it scales very very well and makes refactoring a breeze. Nothing stops you from defining certain "borders" in your application where you wrap errors into something meaningful. E.g. if I write a file-IO library, I will return a custom "CannotOpenFile" error in my public methods which will encapsulate the underlying error(s) instead of returning a list of potential low-level errors had caused it. The difference is that with uniontypes it is me who decides the level of abstraction and granularity, whereas without it, I'm forced into manually handling errors types pretty much everywhere. Rust is an example where you are always forced to either align error types or where you lose information about what kind of errors it could be by using a more abstract error type. (which is the last thing you mentioned)
- foldr 3y ago> I use exactly that way of error handling and I find it scales very very well and makes refactoring a breeze. The enormous number of Rust crates that attempt to automate various aspects of defining error types suggests that not everyone finds it so pleasant.
- valenterry 3y agoPlease read my post again, especially the last part...
- foldr 3y agoI have read your post. Apologies if I’ve misunderstood it, but just replying in that way doesn’t really help.
- valenterry 3y agoI'm confused because I didn't say that Rust makes refactoring a breeze right? In fact, I said it does the opposite. So I don't really understand your response.
- foldr 3y agoAh, I read "I use exactly that way of error handling..." as "I use exactly that way of error handling [in Rust]..." (given the post you were replying to), but I guess you were talking about Zig. Sorry for the misunderstanding.
- valenterry 3y agoHaha, I'm actually using neither Rust nor Zig and I think both don't support the way of error-handling that I described. No worries, happens. :)