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

Support user-defined error handlers #26

Merged
merged 6 commits into from
Oct 7, 2013

Conversation

russellhaering
Copy link

This change fixes #24 by allowing users to define a chain of error handlers. Handlers can be used in two ways, shown below:

class MyAwesomeThing(object):
    app = Klein()

    @app.route('/{userid}')
    def users_stuff(self, request, userid):
        pass

    @app.handle_errors(SpecificError)
    def handle_specific_error(self, request, failure):
        pass

    @app.handle_errors
    def my_error_handler(self, request, failure):
        pass

Users can register a handler for a specific class (or classes) of Exception, or use the basic syntax for a catch-all. Error handlers end up getting chained together with calls to Failure.check() to scope handlers to the appropriate Exception types.

In a separate PR I intend to cause HTTPExceptions raised during request routing to be passed to the same handler chain, which would allow users to, for example, define custom 404 handlers.

deferred, a failure or a response body.

If more than one error handler is registered, the handlers will be
executed in order until a handler is encountered which completes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should clarify what "in order" means. In this case it's "the order in which the error handlers are defined."

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarified in 47fd2de

dreid added a commit that referenced this pull request Oct 7, 2013
@dreid dreid merged commit 396658c into twisted:master Oct 7, 2013
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

Successfully merging this pull request may close these issues.

Users should be able to define functions for handling errors.
2 participants