@@ -272,23 +272,27 @@ def transcode(http_options, message=None, **request_kwargs):
272
272
ValueError: If the request does not match the given template.
273
273
"""
274
274
transcoded_value = message or request_kwargs
275
+ bindings = []
275
276
for http_option in http_options :
276
277
request = {}
277
278
278
279
# Assign path
279
280
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 )
282
284
]
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 }
284
288
request ["uri" ] = expand (uri_template , ** path_args )
285
289
286
290
if not validate (uri_template , request ["uri" ]) or not all (path_args .values ()):
287
291
continue
288
292
289
293
# Remove fields used in uri path from request
290
294
leftovers = copy .deepcopy (transcoded_value )
291
- for path_field in path_fields :
295
+ for path_field , _ in fields :
292
296
delete_field (leftovers , path_field )
293
297
294
298
# Assign body and query params
@@ -316,8 +320,27 @@ def transcode(http_options, message=None, **request_kwargs):
316
320
request ["method" ] = http_option ["method" ]
317
321
return request
318
322
323
+ bindings_description = [
324
+ '\n \t URI: "{}"'
325
+ "\n \t Required 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
+
319
337
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
+ "\n Some of the fields of the request message are either not initialized or "
340
+ "initialized with an invalid value."
341
+ "\n Please make sure your request matches at least one accepted HTTP binding."
342
+ "\n To 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 )
322
345
)
323
346
)
0 commit comments