Skip to content

Commit

Permalink
Allowing hooks to return responses that indicate errors
Browse files Browse the repository at this point in the history
Since response objects for failures (4xx/5xx responses) evaluate to False
in a boolean context, any hook which returns such a failure response will
evaluate to False.

The way hooks were setup, any failure response resulting from a hook would
be ignored, and the initial response before it got processed by the hook
would be substituted in its place. This commit changes that logic to test
for None so that hooks that return failures can do so.
  • Loading branch information
Michael Komitee committed Jun 1, 2012
1 parent 77a57c0 commit 662c3ed
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions requests/hooks.py
Expand Up @@ -30,7 +30,6 @@

HOOKS = ('args', 'pre_request', 'pre_send', 'post_request', 'response')


def dispatch_hook(key, hooks, hook_data):
"""Dispatches a hook dictionary on a given piece of data."""

Expand All @@ -44,7 +43,10 @@ def dispatch_hook(key, hooks, hook_data):

for hook in hooks:
try:
hook_data = hook(hook_data) or hook_data
_hook_data = hook(hook_data)
if _hook_data is not None:
hook_data = _hook_data

except Exception:
traceback.print_exc()

Expand Down

0 comments on commit 662c3ed

Please sign in to comment.