Skip to content

Commit 6754a2f

Browse files
vam-googleparthea
authored andcommitted
fix: Improve transcoding error message (#442)
* fix: Improve transcodding error message This fixes #441 and #440. Also, with this change we stop outputting the whole request message in transcodding erro message to prevent leaking any confidential information from a request message in a form of an error in a log message. * reformat code with black
1 parent 4e7d40e commit 6754a2f

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

google/api_core/path_template.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,23 +272,27 @@ def transcode(http_options, message=None, **request_kwargs):
272272
ValueError: If the request does not match the given template.
273273
"""
274274
transcoded_value = message or request_kwargs
275+
bindings = []
275276
for http_option in http_options:
276277
request = {}
277278

278279
# Assign path
279280
uri_template = http_option["uri"]
280-
path_fields = [
281-
match.group("name") for match in _VARIABLE_RE.finditer(uri_template)
281+
fields = [
282+
(m.group("name"), m.group("template"))
283+
for m in _VARIABLE_RE.finditer(uri_template)
282284
]
283-
path_args = {field: get_field(transcoded_value, field) for field in path_fields}
285+
bindings.append((uri_template, fields))
286+
287+
path_args = {field: get_field(transcoded_value, field) for field, _ in fields}
284288
request["uri"] = expand(uri_template, **path_args)
285289

286290
if not validate(uri_template, request["uri"]) or not all(path_args.values()):
287291
continue
288292

289293
# Remove fields used in uri path from request
290294
leftovers = copy.deepcopy(transcoded_value)
291-
for path_field in path_fields:
295+
for path_field, _ in fields:
292296
delete_field(leftovers, path_field)
293297

294298
# Assign body and query params
@@ -316,8 +320,27 @@ def transcode(http_options, message=None, **request_kwargs):
316320
request["method"] = http_option["method"]
317321
return request
318322

323+
bindings_description = [
324+
'\n\tURI: "{}"'
325+
"\n\tRequired request fields:\n\t\t{}".format(
326+
uri,
327+
"\n\t\t".join(
328+
[
329+
'field: "{}", pattern: "{}"'.format(n, p if p else "*")
330+
for n, p in fields
331+
]
332+
),
333+
)
334+
for uri, fields in bindings
335+
]
336+
319337
raise ValueError(
320-
"Request {} does not match any URL path template in available HttpRule's {}".format(
321-
request_kwargs, [opt["uri"] for opt in http_options]
338+
"Invalid request."
339+
"\nSome of the fields of the request message are either not initialized or "
340+
"initialized with an invalid value."
341+
"\nPlease make sure your request matches at least one accepted HTTP binding."
342+
"\nTo match a binding the request message must have all the required fields "
343+
"initialized with values matching their patterns as listed below:{}".format(
344+
"\n".join(bindings_description)
322345
)
323346
)

tests/unit/test_path_template.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,8 +629,9 @@ def test_transcode_with_additional_bindings(
629629
)
630630
def test_transcode_fails(http_options, message, request_kwargs):
631631
http_options, _ = helper_test_transcode(http_options, range(4))
632-
with pytest.raises(ValueError):
632+
with pytest.raises(ValueError) as exc_info:
633633
path_template.transcode(http_options, message, **request_kwargs)
634+
assert str(exc_info.value).count("URI") == len(http_options)
634635

635636

636637
def helper_test_transcode(http_options_list, expected_result_list):

0 commit comments

Comments
 (0)