Skip to content

Commit e7dad83

Browse files
Indy2222Jon Wayne Parrott
authored andcommitted
Fix parsing of API errors with Unicode err message (#4251)
1 parent 6873d14 commit e7dad83

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

google/api_core/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ def from_http_response(response):
378378
error_message = payload.get('error', {}).get('message', 'unknown error')
379379
errors = payload.get('error', {}).get('errors', ())
380380

381-
message = '{method} {url}: {error}'.format(
381+
message = u'{method} {url}: {error}'.format(
382382
method=response.request.method,
383383
url=response.request.url,
384384
error=error_message)

tests/unit/test_exceptions.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,22 @@ def test_from_http_response_bad_json_content():
135135
assert exception.message == 'POST https://example.com/: unknown error'
136136

137137

138+
def test_from_http_response_json_unicode_content():
139+
response = make_response(json.dumps({
140+
'error': {
141+
'message': u'\u2019 message',
142+
'errors': ['1', '2']
143+
}
144+
}).encode('utf-8'))
145+
146+
exception = exceptions.from_http_response(response)
147+
148+
assert isinstance(exception, exceptions.NotFound)
149+
assert exception.code == http_client.NOT_FOUND
150+
assert exception.message == u'POST https://example.com/: \u2019 message'
151+
assert exception.errors == ['1', '2']
152+
153+
138154
def test_from_grpc_status():
139155
message = 'message'
140156
exception = exceptions.from_grpc_status(

0 commit comments

Comments
 (0)