Skip to content
Myr

Allowing hooks to return responses that indicate errors

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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.

authored by Michael Komitee · committed by Michael Komitee · parent 77a57c0 · Browse files · 1 file+4 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+4 added−2 removed

  • other:+4 added−2 removed

1 file+4 added−2 removed

modified:requests/hooks.py+4 added-2 removed
28 unmodified lines
293031HOOKS = ('args', 'pre_request', 'pre_send', 'post_request', 'response')323333def dispatch_hook(key, hooks, hook_data):34 """Dispatches a hook dictionary on a given piece of data."""3536 hooks = hooks or dict()
5 unmodified lines
42 hooks = [hooks]4344 for hook in hooks:45 try:46 _hook_data = hook(hook_data)47 if _hook_data is not None:47 hook_data = hook(hook_data) or hook_data48 hook_data = _hook_data4950 except Exception:51 traceback.print_exc()5253 return hook_data