Skip to content

Commit db2a766

Browse files
authored
feat: add status_code property on http error handling (#1185)
Fixes #1183 🦕 Fixes #1255 🦕 - Add status code property in http error handler class. - Resolve issue where error_details is not populated.
1 parent c518472 commit db2a766

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

docs/start.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Creating a request does not actually call the API. To execute the request and ge
9999
try:
100100
response = request.execute()
101101
except HttpError as e:
102-
print('Error response status code : {0}, reason : {1}'.format(e.resp.status, e.error_details))
102+
print('Error response status code : {0}, reason : {1}'.format(e.status_code, e.error_details))
103103
```
104104

105105
Alternatively, you can combine previous steps on a single line:

googleapiclient/errors.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ def __init__(self, resp, content, uri=None):
4343
self.content = content
4444
self.uri = uri
4545
self.error_details = ""
46+
self._get_reason()
47+
48+
@property
49+
def status_code(self):
50+
"""Return the HTTP status code from the response content."""
51+
return self.resp.status
4652

4753
def _get_reason(self):
4854
"""Calculate the reason for the error from the response content."""

tests/test_errors.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ def test_json_body(self):
8383
reason="Failed",
8484
)
8585
error = HttpError(resp, content, uri="http://example.org")
86+
self.assertEqual(error.error_details, "error details")
87+
self.assertEqual(error.status_code, 400)
8688
self.assertEqual(
8789
str(error),
8890
'<HttpError 400 when requesting http://example.org returned "country is required". Details: "error details">',

0 commit comments

Comments
 (0)