« BackBetter Error Handlingmeowbark.devSubmitted by zdgeier 3 hours ago
  • ivanjermakov 7 minutes ago

    Errors as values approach suffers similar problem as async/await - it's leaky. Once the function is altered to possibly return an error, its signature changes and every caller needs to be updated (potentially all the way to the main(), if error is not handled before that).

    This approach is great when:

    * program requirements are clear

    * correctness is more important than prototyping speed, because every error has to be handled

    * no need for concise stack trace, which would require additional layer above simple tuples

    * language itself has a great support for binding and mapping values, e.g. first class monads or a bind operator

    Good job by the author on acknowledging that this error handling approach is not a solver bullet and has tradeoffs.

    • zeroq 5 minutes ago

      JS aside, I recently tried my very best to introduce proper logging and error handling to otherwise "look ma, no handlebars" codebase.

      Call it a thought experiment. We start with a clean implementation that satisfies requirements. It makes a bold assumption that every star in the universe will align to help us achieve to goal.

      Now we add logging and error handling.

      Despite my best intentions and years of experience, starting with clean code, the outcome was a complete mess.

      It brings back memories when in 2006 I was implementing deep linking for Wikia. I started with a "true to the documention" implemention which was roughly 10 lines of code. After handling all edge cases and browser incompatibilites I ended up with a whooping 400 lines.

      Doing exactly the same as the original lines did, but cross compatible.

      • domlebo70 an hour ago

        Very balanced post thank you. Often these posts tout an approach, and never consider downsides.

        • teddyh an hour ago

          > Lack of Type System Integration

          Well, IIUC, Java had (and still has) something called “checked exceptions”, but people have, by and large, elected to not use those kind of exceptions, since it makes the rest of the code balloon out with enormous lists of exceptions, each of which must be changed when some library at the bottom of the stack changes slightly.

          • remexre an hour ago

            I think it's fair to say that having some sort of syntactically lightweight sum or union type facility makes this way nicer than anything Java ever had -- subclassing isn't really a solution, because you often want something like:

                type FooError = YoureHoldingItWrong | FileError
                type BarError = YoureHoldingItWrong | NetworkError
                fn foo() -> Result<int, FooError> { ... }
                fn bar() -> Result<int, BarError> { ... }
                fn baz() -> Result<String, BarError> { ... }
            
            TypeScript's type system would hypothetically make this pretty nice if there were a common Result type with compiler support.

            Rust needs a bit more boilerplate to declare FooError, but the ? syntax automatically calling into(), and into() being free to rearrange errors it bubbles up really help a lot too.

            The big problem with Java's checked exceptions was that you need to list all the exceptions on every function, every time.

          • keybored 31 minutes ago

            I love libraries that does a simple check and signals that it "failed" with ThingWasNotTrueException.

            In surprising twist: Java has ConcurrentModificationException. And, to counter its own culture of exception misuse, the docs have a stern reminder that this exception is supposed to be thrown when there are bugs. You are not supposed to use it to, I dunno, iterate over the collection and bail out (control flow) based on getting this exception.