Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handlers aren't working on apps #58

Closed
SamuelMarks opened this issue Oct 26, 2014 · 7 comments
Closed

Error handlers aren't working on apps #58

SamuelMarks opened this issue Oct 26, 2014 · 7 comments

Comments

@SamuelMarks
Copy link

Here's my simple TODO app with basic error handling using your handle_errors decorator: https://gist.github.com/SamuelMarks/9d7c796b8a336bb556f3

Unfortunately it is always throwing 500s. Is there some other way I'm meant to be passing errors? E.g.: in Node.js most of my functions have the signature (err, res).

Thanks for all assistance

PS: The functions from line 103 to 110 was another attempt of mine to get exceptions handled

@twm
Copy link
Contributor

twm commented Nov 6, 2014

Your gist doesn't even run for me:

 $ temp/bin/python todo_app.py
Traceback (most recent call last):
  File "todo_app.py", line 21, in <module>
    class CustomerStore(object):
  File "todo_app.py", line 81, in CustomerStore
    @app.handle_errors(UniqueKeyError, ValidationError)
TypeError: _f() takes exactly 3 arguments (1 given)

This is because Klein uses type checking to determine if @app.handle_errors is called with arguments or not. See

if not isinstance(f_or_exception, type) or not issubclass(f_or_exception, Exception):

The problem is that your exceptions subclass BaseException instead of Exception. BaseException is only supposed to be used for "special" exceptions like StopIteration and KeyboardInterrupt that normal exception handlers shouldn't catch---see the Python docs for more information on this. Switching to subclassing Exception seems to resolve your problem.

@SamuelMarks
Copy link
Author

Thanks

@hynek
Copy link
Member

hynek commented Nov 7, 2014

If either of you could contribute a chapter on error handling for the docs, that would be really amazing.

@SamuelMarks
Copy link
Author

Sure thing: #60

@hynek
Copy link
Member

hynek commented Nov 7, 2014

I was more thinking of a narrative explanation how error handling works in klein. :) TBH I didn’t know about handle_errors before for example.

A complete example like #60 is useful too of course.

@SamuelMarks
Copy link
Author

Oh right. Yeah, I'm not sure how I know about it, might've been from: https://github.com/twisted/klein/blob/master/klein/test/test_resource.py#L707

Anyway, I've updated the pull request with a new commit. So now it shows how to work with error handlers.

@hynek
Copy link
Member

hynek commented Nov 21, 2014

JFTR, my wish has been fulfilled with #51

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants