Hi Chris,
> What is the proper way for handling application-specific logic errors (i.e.
> the ones you'd expect to happen due to erroneous user input) in a handler?
>
> For example, when user submits/posts a string that is supposed to match
> something in the db inside the post method of the handler, but it turns out
> nothing is matched. So you would return something to the user to signal
> that something is wrong.
assuming it's an article ID the user requested or sth. similar HTTP 404 or 410
would be appropriate. But you said POST requests. It depends on your scenario
but when a user enters any sort of "bad" data (illegal values, numbers out of
range, too long or short strings, ...) and submits the form I always return a
HTTP 400 with an appropriate message telling what exactly was wrong. I wrote a
session engine which is also capable of storing messages over multiple
requests in the same session. In my base template all the messages are popped
out and displayed, so this will also work if a redirect must occour for some
reason and messages are only displayed once. Whenever I notice there are wrong
data coming in in my POST request I add the error message to the session, set
the 400 and render the form again with all submitted data prefilled in the
inputs (or at least the valid ones). Most HTTP client error codes don't fit my
actual problems, so 400 is my choice. Having such a simple setup is pretty
convenient in my opinion.
> Currently, I'm doing something like: self.finish('unmatched') for the above
> scenario, and use JavaScript on the client-side to parse the result
> (returned via an AJAX call to the handler) accordingly. However I feel
> there must be a more polished way for this.
So with this approach you're annoying users having JavaScript disabled. They
won't spot the actual problem with their submitted query.
Regards,
Lyse