Rust errors without dependencies

(vincents.dev)

35 points | by vsgherzi 17 hours ago

10 comments

  • morning-coffee 37 minutes ago
    > I want less code. I want to limit the amount of 3rd party code I pull in. This is mostly due to supply chain disasters over on NPM scaring me and the amount of code dependencies bringing in see rust dependencies scare me.

    `anyhow` has exactly one optional dependency (backtrace). `thiserror` has three (proc-macro2, quote, syn) which are at the base of practically the entire Rust ecosystem.

    Unless the author has zero dependencies in general, I'll bet they have all of the above dependencies already.

    ¯\_(°ペ)_/¯

    • vsgherzi 18 minutes ago
      Anyhow itself is still a dependency. This is more something I wanted to do and not something I recommend for everyone. Google took a similar approach in how they added rust for chrome. They don’t use an error handling library.
    • khuey 27 minutes ago
      From a supply chain security perspective it's worth noting that a version of backtrace already ships in the standard library too.
  • j1elo 1 hour ago
    That's quite a good amount of boilerplate to create a custom, project-specific handling of errors, which itself can have bugs. During reading I thought "anyhow, at this point you are half way to reinvent the wheel and write your own "anyhow'".

    I agree with avoiding an explosion of dependencies; but not at any cost. In any case if custom error handling works, then why not. It's just that it feels like a deviation to do extra work on designing and implementing an ideal error handling system with all the cool features, instead of spending that same time working on the actual target of the project itself.

    • vsgherzi 16 minutes ago
      It is, this is the most verbose way of doing it, it can easily be made smaller. The main reason is rust exposes this where other languages tend to hide it so programmers aren’t used to having so much code on error cases. Just my opinion of course
    • PhilipRoman 41 minutes ago
      Any medium or large C project has these kinds of project-specific (or sometimes company-specific) collections of log macros, error handling macros, etc. The amount of boilerplate here is minimal compared to that.
  • athrowaway3z 51 minutes ago
    My controversial take on Rust errors is to use anyhow everywhere until you have an immediate demand for explicit Enums. YANGNI

    The pros for using anyhow are big: Easily stack errors together - eg file.open().context(path) -, errors are easily kept up to date, and easy to find where they occur.

    An enum error is pleasing when you're finished, but cumbersome in practice.

    It's niche situation that you need to write a function that exposes a meaningful Error state the caller can branch on. If the return state is meaningful, you usually don't put it in the Err part. eg parsers using { Ok,Incomplete,Error }. IMO Error enums are best for encoding known external behavior like IO errors.

    For example: The Sender.send_deadline returning { Timeout, Closed } is the exception in being useful. Most errors are like a Base64 error enums. Branching on the detail is useless for 99.99% of callers.

    i.e. If your crate is meant for >1000 users, build the full enum.

    For any other stuff, use anyhow.

    • sunshowers 7 minutes ago
      LLMs are pretty good at generating and maintaining error enums for you these days.
    • zaphar 32 minutes ago
      If your error domain has only one useful category why not just create an error type with a useful message and be done with it. Why use anyhow at all? You are essentially saying the error domain is small so the work is tiny anyway.

      anyhow seems useful in the very top layers where you basically just want bubble the useful errors modeled elsewhere to a top layer that can appropriately handle them. I don't think a crate should abdicate from modeling the error domain any more than they should abdicate from modeling the other types.

  • drnick1 1 hour ago
    > I want less code. I want to limit the amount of 3rd party code I pull in. This is mostly due to supply chain disasters over on NPM scaring me and the amount of code dependencies bringing in see rust dependencies scare me.

    And this is basically why I like the C/C++ model of not having a centralized repo better. If I need some external piece of software, I simply download the headers and/or sources directly and place them in my project and never touch these dependencies again. Unless somehow these are compromised at the time of download, I will never have to worry about them again. Also these days I am increasingly relying on LLMs to simply generate what I need from scratch and rely less and less on external code.

    • tempest_ 9 minutes ago
      The C/C++ model should go back to 80s where it belongs.

      You can vendor deps with cargo if you want but fighting cmake/make/autoconf/configure/automake build spaghetti is not my idea of a good time.

    • vsgherzi 17 minutes ago
      I’d rather have cargo than not. Dependencies are opt in you don’t have to use them, which is what I’m trying to demonstrate here. The chrome team only uses what they need. Now the culture as a whole in rust in always that way but I believe that to mostly be due to the newness of the lang and the quality of libraries
    • Meneth 17 minutes ago
      I like using shared libraries from my Linux distro. Then I can rely on their automatic security updates to deal with any third-party vulnerabilities.
    • andrepd 12 minutes ago
      > Unless somehow these are compromised at the time of download, I will never have to worry about them again.

      But this is exactly what rust does x) `cargo add some_crate` adds a line `crate_name = "1.2.3"` to your project config, downloading and pinning the dependency to that exact version. It will not change unless you specifically change it.

      • rcxdude 9 minutes ago
        well, not quite. It'll go into the lockfile and you won't get a new version if you just build again, but if you add or remove a dependency that version may shift around a bit as a part of dependency resolution.
        • nateb2022 2 minutes ago
          > but if you add or remove a dependency that version may shift around a bit as a part of dependency resolution

            cargo add crate@version
          
          is completely deterministic
        • andrepd 2 minutes ago
          Only if that different version is a dependency of a dependency. Your own will never change.
    • umvi 55 minutes ago
      External C++ code never has CVEs? Or I guess since you are manually managing it, you are just ignorant of any CVEs?
      • drnick1 44 minutes ago
        I suppose this largely depends on the kind of software that you write. Ideally, you also extract only the part of the external code that you need, audit it, and integrate it into your own code. This way you minimize the attack surface. I don't work on software that is exposed to the Internet however, so admittedly the importance of security vulnerabilities is low.
    • dkdcio 1 hour ago
      you could just do that with Rust, right? you’re just saying cargo makes it too easy not to

      I’m very tempted to go this direction myself with Rust, vendoring in and “maintaining” (using Claude Code to maintain) dependencies. or writing subsets of the crates I need myself and using those. the sprawl with Rust dependencies is concerning

      • drnick1 58 minutes ago
        Yes of course you could do this in Rust. It's just that every resource out there promotes the usage of Cargo, and sells this as an "improvement" over the old school way of managing dependencies manually.
        • rcxdude 6 minutes ago
          it's mainly an improvement because the rust ecosystem has a standardized way to build and distribute packages, so you can reliably add a dependency without build system pain. If you don't think having a reference to a central repo is a good way to go, you can vendor or even just pin your dependencies.
        • FridgeSeal 32 minutes ago
          Cargo will let you vendor code into your repo easily.

          I think you’re conflating the tool, with how people manage deps.

          https://doc.rust-lang.org/cargo/commands/cargo-vendor.html

  • IshKebab 1 hour ago
    I still think it's kind of mad that the standard library doesn't have better options built in. We've had long enough to explore the approaches. It's time to design something that can go into std and be used by everybody.

    As it is any moderately large Rust project ends up including several different error handling crates.

    • burntsushi 50 minutes ago
      I'm on libs-api and I'd personally be on board with something like `thiserror` coming into `std`. But it would need a champion I think.
      • epage 21 minutes ago
        I think we should provide the building blocks (display, etc like derive_more) rather than a specialized version one for errors (thiserror).

        I also feel thiserror encourages a public error enum which to me is an anti-pattern as they are usually tied to your implementation and hard to add context, especially if you have a variants for other error types.

        • burntsushi 12 minutes ago
          Yeah I used the weasel-y "something like" for exactly these reasons. :-)
    • vsgherzi 15 minutes ago
      I do think it’s just a newness issue and the community is still deciding what’s right
  • zahlman 1 hour ago
    > In the recent Cloudlfare outage Cloudlflare's proxy service went down directly due to an unwrap when reading a config file. Me and many other developers jumped the shark, calling out Cloudflare on their best practices. In Cloudflare's defense they treated this file as trusted input and never expected it to be malformed. Due to circumstances the file became invalid causing the programs assumption's to break.

    "Trusted" is a different category from "valid" for a reason. Especially if you're working in a compiled language on something as important as that, anything that isn't either part of the code itself or in a format where literally every byte sequence is acceptable, should be treated as potentially malformed. There is nothing compiling the config file.

    > Why is this better than NodeJS

    ... That feels like it really came out of nowhere, and after seeing so much code to implement what other languages have as a first-class feature (albeit with trade-offs that Rust clearly wanted to avoid), it comes across almost as a coping mechanism.

    • vsgherzi 7 minutes ago
      Nodejs and rust are the languages that I’m most familiar. I mostly mean that part to serve as a contrasting paragraph between the two paradigms. The amount of code is high in rust, even higher due to me writing the most pedantic error possible. If you really want a more try catch approach you can do that with something like dyn error or anyhow. The point is it gives you choice
    • kaoD 1 hour ago
      And it was treated as potentially malformed and hence the panic. That's what panic is for! When invariants are not upheld at runtime, in Cloudflare's case an abnormal amount of entries IIRC.

      I mean, if the error was handled what would you have done if not crashing the service with an error message?

      I think the post's point is that you don't panic if someone submits a malformed PDF (you just reject their request) but I don't think there's any way to gracefully handle a malformed config file that is core to the service.

    • TZubiri 29 minutes ago
      >That feels like it really came out of nowhere, and after seeing so much code to implement what other languages have as a first-class feature (albeit with trade-offs that Rust clearly wanted to avoid), it comes across almost as a coping mechanism.

      It's really not fair to compare these when most of the errors of one language are caught at compile time by the other.

      It reminds me of that scene from silicon valley "Anything related to errors sounds like your area

      https://youtu.be/oyVksFviJVE?si=NVq9xjd1uCnhZkPz&t=55

      Can we not just agree that interpreted languages (save the Ackshually) like python and node need a more elaborate error handling system because they have more errors than compiled languages? It's not a holy war thing, I'm not on either side, in fact I use interpreted languages more than compiled languages, but it's just one of the very well-known trade-offs.

      In the alternative, you would at least admit that error handling in an interpreted language is completely different than error handling in a compiled language.

      • zahlman 17 minutes ago
        > when most of the errors of one language are caught at compile time by the other.

        Yes, that's precisely what I meant about "trade-offs that Rust clearly wanted to avoid".

  • TZubiri 33 minutes ago
    >Rust error handling is a complex topic mostly due to the composability it gives you and no "blessed way" to accomplish this from the community.

    I find it hard to believe. Since a huge class of errors are caught by compile time static analysis, you don't really need an exception system, and errors are basically just return values that you check.

    It's much more productive just to use return values and check them, wrap return values in an optional, do whatever. Just move on, do not recreate the features of your previous language on a new language.

  • osiris88 47 minutes ago
    OP is spot on, no deps is the way.

    I've been using rust for 8+ years, I remember the experiments around `failure` crate, a precursor to anyhow if I remember right... and then eyre, and then thiserror...

    It just felt like too much churn and each one offered barely any distinction to the previous.

    Additionally, the `std::error::Error` trait was very poorly designed when it was initially created. It was `std` only and linked to a concept of backtraces, which made it a non-starter for embedded. It just seemed to me that it was a bad idea ever to use it in a library and that it would harm embedded users.

    And the upside for non-embedded users was minimal. Indeed most of it's interface since then has been deprecated and removed, and it to this day has no built-in idea of "error accumulation". I really can't understand this. That's one of the main things that I would have wanted an generic error interface to solve in order to be actually useful.

    It was also extremely painful 5 years ago when cargo didn't properly do feature unification separately for build dependencies vs. target dependencies. This meant that if you used anything in your build.rs that depended on `failure` with default features, and turned on `std` feature, then you cannot use `failure` anywhere in your actual target or you will get `std` feature and then your build will break. So I rapidly learned that these kinds of crates can cause much bigger problems than they actually solve.

    I think the whole "rust error handling research" area has frankly been an enormous disappointment. Nowadays I try to avoid all of these libraries (failure, anyhow, thiserror, etc.) because they all get abandoned sooner or later, and they brought very little to the table other than being declared "idiomatic" by the illuminati. Why waste my time rewriting it in a year or two for the new cool flavor of suck.

    Usually what I actually do in rust for errors now is, the error is an enum, and I use `displaydoc` to make it implement `Display`, because that is actually very simple and well-scoped, and doesn’t involve std dependencies. I don't bother with implementing `std::error::Error`, because it's pointless. Display is the only thing errors need to implement, for me.

    If I'm writing an application and I come to a point where I need to "box" or "type erase" the error, then it becomes `String` or perhaps `Box<str>` if I care about a few bytes. It may feel crude, but it is simple and it works. That doesn't let you downcast errors later, but the situations where you actually have to do that are very rare and I'm willing to do something ad hoc in those cases. You can also often refactor so that you don't actually have to do that. I'm kind of in the downcasting-is-a-code-smell camp anyways.

    I'm a little bit excited about `rootcause` because it seems better thought out than it's progenitors. But I have yet to try to make systematic use of it in a bigger project.

    • burntsushi 32 minutes ago
      > It was `std` only and linked to a concept of backtraces, which made it a non-started for embedded. It just seemed to me that it was a bad idea ever to use it in a library and that it would harm embedded users.

      It was never linked to backtraces. And if you used `std::error::Error` in a library that you also wanted to support in no-std mode, then you just didn't implement the `std::error::Error` trait when the `std` feature for that library isn't enabled. Nowadays, you can just implement the `core::error::Error` trait unconditionally.

      As for backtrace functionality, that is on the cusp of being stabilized via a generic interface that allows `core::error::Error` to be defined in `core`: https://github.com/rust-lang/rust/issues/99301

      > and it to this day has no built-in idea of "error accumulation".

      The `Error` trait has always had this. It started with `Error::cause`. That was deprecated long ago because of an API bug and replaced with `Error::source`.

      > It just felt like too much churn and each one offered barely any distinction to the previous.

      I wrote about how to do error handling without libraries literally the day Rust 1.0 was published: https://burntsushi.net/rust-error-handling/

      That blog did include a recommendation for `failure` at one point, and now `anyhow`, but it's more of a footnote. The blog shows how to do error handling without any dependencies at all. You didn't have to jump on the error library treadmill. (Although I will say that `anyhow` and `thiserror` have been around for a number of years now and shows no signs of going away.)

      > I don't bother with implementing `std::error::Error`, because it's pointless.

      It's not. `std::error::Error` is what lets you provide an error chain. And soon it will be what you can extract a backtrace from.

      > I'm kind of in the downcasting-is-a-code-smell camp anyways.

      I happily downcast in ripgrep: https://github.com/BurntSushi/ripgrep/blob/0a88cccd5188074de...

      That also shows the utility of an error chain.

      • osiris88 4 minutes ago
        > I wrote about how to do error handling without libraries literally the day Rust 1.0 was published: https://burntsushi.net/rust-error-handling/ > > That blog did include a recommendation for `failure` at one point, and now `anyhow`, but it's more of a footnote. The blog shows how to do error handling without any dependencies at all. You didn't have to jump on the error library treadmill. (Although I will say that `anyhow` and `thiserror` have been around for a number of years now and shows no signs of going away.)

        Thank you -- I just wanna say, I read a lot of your writing and I love your work. I'm not sure if I read that blog post so many years ago but it looks like a good overview that has aged well.

        > I happily downcast in ripgrep: https://github.com/BurntSushi/ripgrep/blob/0a88cccd5188074de... > > That also shows the utility of an error chain.

        Yeah, I mean, that looks pretty nice.

        I still think the error chain abstraction should actually be a tree.

        And I think they should never have stabilized an `std::error::Error` trait that was not in core. I think that itself was a mistake. And 8 years later we're only now maybe able to get there.

        I actually said something on a github issue about this before rust 1.0 stabilization, and that it would cause an ecosystem split with embedded, and that this really should be fixed, but my comment was not well received, and obviously didn't have much impact. I'll see if I can find it, it's on github and I remember withoutboats responded to me.

        Realistically the core team was under a lot of pressure to ship 1.0 and rust has been pretty successful -- I'm still using it for example, and a lot of embedded folks. But I do think I was right that it caused an ecosystem split with embedded and could have been avoided.

      • osiris88 20 minutes ago
        By error accumulation, I mean a tree of errors, not a simple chain. The chain is only useful at the very lowest level.

        The tree allows you to say e.g. this function failed because n distinct preconditions failed, all of which are interesting, and might have lower level details. Or, I tried to do X which failed, and the fallback also failed. The error chain thing doesn’t capture either of these semantics properly.

        Check out `rootcause` which is the first one I’ve seen to actually try to do this.

        I’ll respond to the backtrace comments shortly.

        • burntsushi 15 minutes ago
          I don't see any reason for something like `rootcause` to become foundational. Most errors are a linear chain and that's good enough for most use cases.

          It's correct to say that `std::error::Error` does not support a tree of errors. But it is incorrect to say what you said: that it's pointless and doesn't allow error accumulation. It's not pointless and it does provide error accumulation. Saying it doesn't is a broad overstatement when what you actually mean is something more precise, narrow and niche.

  • hullopa 26 minutes ago
    [dead]
  • llmslave2 37 minutes ago
    So funny how Rust advocates love to bash on Go but even something as trivial as error handling requires either a third party dependency (anyhow) that mimics Go's errors stdlib package, or they invent whatever this was...
    • vsgherzi 4 minutes ago
      The entire blog shows that you don’t need that… sushi showed the same thing in 1.0

      Go dosent deviate from the norm. It’s the same style we’ve had back from the billion dollar mistake. Not saying it’s wrong rust’s is just different. Tradeoffs and such.

    • speedgoose 31 minutes ago
      _, err := fmt.Println("of course golang has some good parts.")

      if err != nil {

        return err
        
      }