On Fri, 26 Jul 2013 23:40:54 GMT, Dicebot wrote:

On Fri, 26 Jul 2013 14:40:47 GMT, Manu Evans wrote:

If simple form handling logic is potentially throw-ing all over the place, what is the typical structure/flow to handle errors gracefully?
Are there any idiomatic examples of this sort of code?

In my experience it always boils down to separating validation of user input (which is expected to throw a lot) into separate part of application and process already sanitized input further. You can catch exceptions in top-most block of former and use it to render nice error message to user about invalid input. Any exception thrown after is likely to be some weird internal server error and default handler which renders status code 500 makes perfect sense.

I think designing even small apps in generic and clean way really pays it off and using script-like approach of simply going and processing stuff is fundamentally flawed.

Can you demonstrate this approach?
There are a few issues here to my mind...

1) An outer block to catch validation exceptions doesn't customise the error for whatever particular thing is missing.
2) Once past the validation process, an unexpected exception will still be caught by the same outer catch that caught the missing args, unless you start messing with specific exception types, which in turn precludes generic handling of argument exceptions.
3) If you go to the effort of defining and throwing specific exceptions for various cases, and/or catching a general exception and performing a bunch of logic to decide what the error case that threw actually was, either way, that's a lot of logic. I don't imagine that's less lines, or more readable than just handling the missing argument with an 'if' at the point I first grab it...

Can you demonstrate what code would look like that:

  • Validates some args, accepting that some args are optional.
  • May receive the args from either post, query, or path args.
  • Gives the opportunity for customised error output for the particular thing that was missing.
  • Doesn't require additional logic to perform those tasks; should be less code/simpler than an if.

If not, then I don't buy that exceptions are a simplification, they would seem more like a convolution, since the flow of code is split up and becomes harder to follow.