Skip to content

Commit a818804

Browse files
authored
Handle the JSON parse error in HParams plugin (#5970)
## Motivation for features / changes Currently, feeding badly formatted request data to `tb.corp/experiment/<id>/data/plugin/hparams/experiment` endpoint can result in 500s (see [b/250791957](https://b.corp.google.com/issues/250791957)): ``` ... raise ParseError('Failed to load JSON: {0}.'.format(str(e))) google3.net.proto2.python.public.json_format.ParseError: Failed to load JSON: Expecting value: line 1 column 1 (char 0). ``` The 500s are confusing to the end users because they will see an `Internal Server Error` page without any useful debugging information. It also adds unnecessary noise to the logging/monitoring system which makes it hard to found the real alerts. ## Technical description of changes This CL catches the JSON ParseError, logs the request data for debugging, and throws 400s. ## Screenshots of UI changes <img width="1105" alt="image" src="https://user-images.githubusercontent.com/88216042/195377616-d06b4bd8-0532-4b37-9f09-d6e7b8dc5885.png"> ## Detailed steps to verify changes work correctly (as executed by you) Open the browser and enter the hparams plugin endpoint of an experiment that feeds the badly-formatted request data, e.g `/experiment/1811564056169732114/data/plugin/hparams/experiment?request=blah` and you should see the UI changes above rather than an `Internal Server Error` page.
1 parent 0ba10f7 commit a818804

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

tensorboard/plugins/hparams/hparams_plugin.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,18 @@ def _get_scalars_plugin(self):
191191

192192

193193
def _parse_request_argument(request, proto_class):
194-
if request.method == "POST":
195-
return json_format.Parse(request.data, proto_class())
196-
197-
# args.get() returns the request URI-unescaped.
198-
request_json = request.args.get("request")
199-
if request_json is None:
194+
request_json = (
195+
request.data
196+
if request.method == "POST"
197+
else request.args.get("request")
198+
)
199+
try:
200+
return json_format.Parse(request_json, proto_class())
201+
# if request_json is None, json_format.Parse will throw an AttributeError:
202+
# 'NoneType' object has no attribute 'decode'.
203+
except (AttributeError, json_format.ParseError) as e:
200204
raise error.HParamsError(
201-
"Expected a JSON-formatted 'request' arg of type: %s" % proto_class
202-
)
203-
return json_format.Parse(request_json, proto_class())
205+
"Expected a JSON-formatted request data of type: {}, but got {} ".format(
206+
proto_class, request_json
207+
)
208+
) from e

0 commit comments

Comments
 (0)