diff --git a/docs/start.md b/docs/start.md index def0c4a481a..9207620a8c2 100644 --- a/docs/start.md +++ b/docs/start.md @@ -99,7 +99,7 @@ Creating a request does not actually call the API. To execute the request and ge try: response = request.execute() except HttpError as e: - print('Error response status code : {0}, reason : {1}'.format(e.resp.status, e.error_details)) + print('Error response status code : {0}, reason : {1}'.format(e.status_code, e.error_details)) ``` Alternatively, you can combine previous steps on a single line: diff --git a/googleapiclient/errors.py b/googleapiclient/errors.py index 7163645ef70..33ee682a4d0 100644 --- a/googleapiclient/errors.py +++ b/googleapiclient/errors.py @@ -43,6 +43,12 @@ def __init__(self, resp, content, uri=None): self.content = content self.uri = uri self.error_details = "" + self._get_reason() + + @property + def status_code(self): + """Return the HTTP status code from the response content.""" + return self.resp.status def _get_reason(self): """Calculate the reason for the error from the response content.""" diff --git a/tests/test_errors.py b/tests/test_errors.py index 78dee17a4c0..9c139a6b60e 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -83,6 +83,8 @@ def test_json_body(self): reason="Failed", ) error = HttpError(resp, content, uri="http://example.org") + self.assertEqual(error.error_details, "error details") + self.assertEqual(error.status_code, 400) self.assertEqual( str(error), '',