Repository navigation
Conversation
Signed-off-by: Roxen-AS <akash.arjun12@gmail.com>
Sun-Wukhan
left a comment
There was a problem hiding this comment.
This fixes #3472, which I filed, and the shape of the fix is better than a plain try around the old expression. Two points worth recording:
-
The
hasattr(error, 'get')check preserves a case the old code got wrong even for dict bodies. The old expression evaluatedresponse_json.get('error', {}).get('code', None)eagerly as the default argument, so{'code': 400, 'error': None}raised AttributeError even though the top-level code was present. This version returns 400 there, and the addedtest_constructor_falsy_code_dict_with_top_level_code_and_null_errorpins exactly that case. The duplicate PR #3474 takes the plain try approach and still returns None for that body, losing the 400, so this PR is the one I would keep of the two. -
Precedence is unchanged on the normal paths: a top-level
codestill wins overerror.code, and a body of only{'error': {'code': 400}}still yields 400 through the nested lookup.
Two small test suggestions, neither blocking:
- The nested-only case
{'error': {'code': 400}}with a falsy constructor code is the path this refactor rewires most, and the new tests only reach a nested code inside a list body, where the guard returns None before any nested lookup happens. One direct dict case would pin that the nested lookup still works. - #3474's tests also parametrize the ClientError and ServerError subclasses. The constructor path is shared, so this is belt and braces, but one subclass case would close the loop on the issue report, which named all three classes.
Signed-off-by: Roxen-AS <akash.arjun12@gmail.com>
|
Thanks for the review @Sun-Wukhan! Added both test cases to
Verified locally ( |
Guarded
APIError._get_codeagainst non-dict response bodies (such as websocket close reasons, integers, or multi-entry list payloads) and non-dict'error'fields so constructingAPIErrorwith a falsycodereturnscode=Noneinstead of raisingAttributeError, matching_get_statusand_get_message.Fixes #3472