News for hackers. Stuff that matters.
Re:You’re better off using Exceptions by hombre_fatal on Monday February 03, 2020 @12:37PM (#22226582)
Well, in real code you are likely bubbling up an error in some way for your 404 Not Found and 500 Internal Error handlers to kick in.

    item = db.findItem(id)
    assert(NotFoundError, item != null)
    render('show-item', item)
upstream middleware:

    try {
      res = await downstream() 
    } catch(e) {
      if (e is NotFoundError)
        render('not-found')
      else
        render('internal-error')
    }
you can do this with other patterns for upstreaming errors like Result + Rust's handy short-circuiting, but your example doesn't demonstrate much. what about internal errors like your store throwing a database error, your first example doesn't even have a store capable of erroring? your comparison incomplete. and surely you don't do all error checking at every callsite no matter which pattern you use.
Re:You’re better off using Exceptions by clon on Monday February 03, 2020 @12:51PM (#22226767)
> Well, in real code you are likely bubbling up an error in some way for your 404 Not Found and 500 Internal Error handlers to kick in.

Which is essentially GOTO 404

A horrible idea that I see often in clever frameworks disobeying encapsulation and reasonable control flow in favour of magic

Re:You’re better off using Exceptions by _ZeD_ on Monday February 03, 2020 @01:15PM (#22227128)
That is a wonderful idea. You don't need to deal with 404, 500 or other stuff, you just write for the "happy path". The framework deals with the rest.
Re:You’re better off using Exceptions by mumblemumble on Monday February 03, 2020 @01:43PM (#22227441)
It's both wonderful and terrible.

On the one hand, it's terribly convenient when you're writing the code.

On the other hand, as clon says, it breaks encapsulation. Not necessarily, but I've rarely seen these style of Web frameworks used in a way that doesn't result in network communication concerns wormholing their way through the entire codebase. And it sometimes does do weird things with control flow. Again, not necessarily but in practice. For example, JAX-RS frameworks do the exception mapping off in some magic location that's outside the part of the call stack that you directly control, meaning that customizing the logic requires hacking some fairly complex special-purpose mechanisms into the framework.

I suspect that there's a way to achieve convenience without relying on spooky action at a distance. But one could be forgiven for looking at the way popular frameworks on popular platforms work and concluding that cleanliness must be the price of convenience.

Re:You’re better off using Exceptions by majewsky on Monday February 03, 2020 @01:29PM (#22227282)
Then you fill in a complex form with 6 object references, and the framework just spits out "NOT FOUND", which means "have fun figuring out which one is the missing object".
Re:You’re better off using Exceptions by joshuamorton on Monday February 03, 2020 @01:59PM (#22227616)
`raise MissingField(field_name)` solves this problem.
Re:You’re better off using Exceptions by ravenstine on Monday February 03, 2020 @02:01PM (#22227633)
At which point you end up doing the same work you would have had to do in the first place, except with extra lines for exception handling. ;)