Skip to content

Commit 374d202

Browse files
authored
Use tensorboard.errors in histograms and distributions (#2986)
Summary: We now raise `errors.NotFoundError` rather than manually propagating an error message and status code. The response code was previously 400 Bad Request, but 404 Not Found is more appropriate, and is consistent with the scalars plugin. Test Plan: Requesting `/data/plugin/histograms/histograms?run=foo&tag=bar` yields an error with the same message as before (but now with a “Not found:” prefix), and the histograms and distributions dashboards both work. wchargin-branch: histograms-notfound
1 parent 97a23b1 commit 374d202

File tree

6 files changed

+28
-23
lines changed

6 files changed

+28
-23
lines changed

tensorboard/plugins/distribution/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ py_test(
3131
deps = [
3232
":compressor",
3333
":distributions_plugin",
34+
"//tensorboard:errors",
3435
"//tensorboard:expect_tensorflow_installed",
3536
"//tensorboard/backend:application",
3637
"//tensorboard/backend/event_processing:event_accumulator",

tensorboard/plugins/distribution/distributions_plugin.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ def frontend_metadata(self):
7474
)
7575

7676
def distributions_impl(self, tag, run):
77-
"""Result of the form `(body, mime_type)`, or `ValueError`."""
77+
"""Result of the form `(body, mime_type)`.
78+
79+
Raises:
80+
tensorboard.errors.PublicError: On invalid request.
81+
"""
7882
(histograms, mime_type) = self._histograms_plugin.histograms_impl(
7983
tag, run, downsample_to=self.SAMPLE_SIZE)
8084
return ([self._compress(histogram) for histogram in histograms],
@@ -98,10 +102,5 @@ def distributions_route(self, request):
98102
"""Given a tag and single run, return an array of compressed histograms."""
99103
tag = request.args.get('tag')
100104
run = request.args.get('run')
101-
try:
102-
(body, mime_type) = self.distributions_impl(tag, run)
103-
code = 200
104-
except ValueError as e:
105-
(body, mime_type) = (str(e), 'text/plain')
106-
code = 400
107-
return http_util.Respond(request, body, mime_type, code=code)
105+
(body, mime_type) = self.distributions_impl(tag, run)
106+
return http_util.Respond(request, body, mime_type)

tensorboard/plugins/distribution/distributions_plugin_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
import collections
2323
import os.path
2424

25-
import six
2625
from six.moves import xrange # pylint: disable=redefined-builtin
2726
import tensorflow as tf
2827

28+
from tensorboard import errors
2929
from tensorboard.backend.event_processing import plugin_event_accumulator as event_accumulator # pylint: disable=line-too-long
3030
from tensorboard.backend.event_processing import plugin_event_multiplexer as event_multiplexer # pylint: disable=line-too-long
3131
from tensorboard.plugins import base_plugin
@@ -137,7 +137,7 @@ def _test_distributions(self, run_name, tag_name, should_work=True):
137137
(bps, _unused_icdfs) = zip(*bps_and_icdfs)
138138
self.assertEqual(bps, compressor.NORMAL_HISTOGRAM_BPS)
139139
else:
140-
with six.assertRaisesRegex(self, ValueError, 'No histogram tag'):
140+
with self.assertRaises(errors.NotFoundError):
141141
self.plugin.distributions_impl(self._DISTRIBUTION_TAG, run_name)
142142

143143
def test_distributions_with_scalars(self):

tensorboard/plugins/histogram/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ py_library(
1717
visibility = ["//visibility:public"],
1818
deps = [
1919
":metadata",
20+
"//tensorboard:errors",
2021
"//tensorboard:expect_numpy_installed",
2122
"//tensorboard:plugin_util",
2223
"//tensorboard/backend:http_util",
@@ -36,6 +37,7 @@ py_test(
3637
deps = [
3738
":histograms_plugin",
3839
":summary",
40+
"//tensorboard:errors",
3941
"//tensorboard:expect_tensorflow_installed",
4042
"//tensorboard/backend:application",
4143
"//tensorboard/backend/event_processing:event_accumulator",

tensorboard/plugins/histogram/histograms_plugin.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import six
3030
from werkzeug import wrappers
3131

32+
from tensorboard import errors
3233
from tensorboard import plugin_util
3334
from tensorboard.backend import http_util
3435
from tensorboard.compat import tf
@@ -128,10 +129,13 @@ def frontend_metadata(self):
128129
return base_plugin.FrontendMetadata(element_name='tf-histogram-dashboard')
129130

130131
def histograms_impl(self, tag, run, downsample_to=None):
131-
"""Result of the form `(body, mime_type)`, or `ValueError`.
132+
"""Result of the form `(body, mime_type)`.
132133
133134
At most `downsample_to` events will be returned. If this value is
134135
`None`, then no downsampling will be performed.
136+
137+
Raises:
138+
tensorboard.errors.PublicError: On invalid request.
135139
"""
136140
if self._db_connection_provider:
137141
# Serve data from the database.
@@ -152,7 +156,9 @@ def histograms_impl(self, tag, run, downsample_to=None):
152156
{'run': run, 'tag': tag, 'plugin': metadata.PLUGIN_NAME})
153157
row = cursor.fetchone()
154158
if not row:
155-
raise ValueError('No histogram tag %r for run %r' % (tag, run))
159+
raise errors.NotFoundError(
160+
'No histogram tag %r for run %r' % (tag, run)
161+
)
156162
(tag_id,) = row
157163
# Fetch tensor values, optionally with linear-spaced sampling by step.
158164
# For steps ranging from s_min to s_max and sample size k, this query
@@ -196,7 +202,9 @@ def histograms_impl(self, tag, run, downsample_to=None):
196202
try:
197203
tensor_events = self._multiplexer.Tensors(run, tag)
198204
except KeyError:
199-
raise ValueError('No histogram tag %r for run %r' % (tag, run))
205+
raise errors.NotFoundError(
206+
'No histogram tag %r for run %r' % (tag, run)
207+
)
200208
if downsample_to is not None and len(tensor_events) > downsample_to:
201209
rand_indices = random.Random(0).sample(
202210
six.moves.xrange(len(tensor_events)), downsample_to)
@@ -228,11 +236,6 @@ def histograms_route(self, request):
228236
"""Given a tag and single run, return array of histogram values."""
229237
tag = request.args.get('tag')
230238
run = request.args.get('run')
231-
try:
232-
(body, mime_type) = self.histograms_impl(
233-
tag, run, downsample_to=self.SAMPLE_SIZE)
234-
code = 200
235-
except ValueError as e:
236-
(body, mime_type) = (str(e), 'text/plain')
237-
code = 400
238-
return http_util.Respond(request, body, mime_type, code=code)
239+
(body, mime_type) = self.histograms_impl(
240+
tag, run, downsample_to=self.SAMPLE_SIZE)
241+
return http_util.Respond(request, body, mime_type)

tensorboard/plugins/histogram/histograms_plugin_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
import collections
2323
import os.path
2424

25-
import six
2625
from six.moves import xrange # pylint: disable=redefined-builtin
2726
import tensorflow as tf
2827

28+
from tensorboard import errors
2929
from tensorboard.backend.event_processing import plugin_event_accumulator as event_accumulator # pylint: disable=line-too-long
3030
from tensorboard.backend.event_processing import plugin_event_multiplexer as event_multiplexer # pylint: disable=line-too-long
3131
from tensorboard.plugins import base_plugin
@@ -130,7 +130,7 @@ def _test_histograms(self, run_name, tag_name, should_work=True):
130130
self._check_histograms_result(tag_name, run_name, downsample=False)
131131
self._check_histograms_result(tag_name, run_name, downsample=True)
132132
else:
133-
with six.assertRaisesRegex(self, ValueError, 'No histogram tag'):
133+
with self.assertRaises(errors.NotFoundError):
134134
self.plugin.histograms_impl(self._HISTOGRAM_TAG, run_name)
135135

136136
def _check_histograms_result(self, tag_name, run_name, downsample):

0 commit comments

Comments
 (0)