From 6b4592e693ae23685be08e877eaa5dc1a292078d Mon Sep 17 00:00:00 2001 From: Rick van Hattem Date: Tue, 8 Jan 2019 15:15:00 +0100 Subject: [PATCH] Simplified exception system Signed-off-by: Rick van Hattem --- prometheus_client/exceptions.py | 35 ------------------------- prometheus_client/exposition.py | 6 ++--- tests/test_metrics_core.py | 45 ++++++++++++++++----------------- 3 files changed, 25 insertions(+), 61 deletions(-) delete mode 100644 prometheus_client/exceptions.py diff --git a/prometheus_client/exceptions.py b/prometheus_client/exceptions.py deleted file mode 100644 index 857392d3..00000000 --- a/prometheus_client/exceptions.py +++ /dev/null @@ -1,35 +0,0 @@ - - -class MetricErrorBase(BaseException): - - def __init__(self, metric, *args): - self.metric = metric - super(MetricErrorBase, self).__init__(*args) - - -class MetricTypeError(MetricErrorBase, TypeError): - pass - - -class MetricValueError(MetricErrorBase, ValueError): - pass - - -class MetricAttributeError(MetricErrorBase, AttributeError): - pass - - -Exceptions = { - TypeError: MetricTypeError, - ValueError: MetricValueError, - AttributeError: MetricAttributeError, -} - - -def from_exception(metric, exception): - # Fetch the exception class from the dictionary - type_ = Exceptions[type(exception)] - # Init the exception and add the metric - instance = type_(*exception.args) - instance.metric = metric - return instance diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py index fe9f0147..efa53a20 100644 --- a/prometheus_client/exposition.py +++ b/prometheus_client/exposition.py @@ -13,7 +13,6 @@ from .openmetrics import exposition as openmetrics from .registry import REGISTRY from .utils import floatToGoString -from . import exceptions try: from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer @@ -119,8 +118,9 @@ def sample_line(s): break else: output.append(sample_line(s)) - except tuple(exceptions.Exceptions) as exception: - raise exceptions.from_exception(metric, exception) + except Exception as exception: + exception.args = (exception.args or ('',)) + (metric,) + raise for suffix, lines in sorted(om_samples.items()): output.append('# TYPE {0}{1} gauge\n'.format(metric.name, suffix)) diff --git a/tests/test_metrics_core.py b/tests/test_metrics_core.py index f1c7882d..a3c2c9ba 100644 --- a/tests/test_metrics_core.py +++ b/tests/test_metrics_core.py @@ -1,7 +1,6 @@ import pytest from prometheus_client import core -from prometheus_client import exceptions from prometheus_client import exposition @@ -24,7 +23,7 @@ def _expect_metric_exception(registry, expected_error): try: exposition.generate_latest(registry) except expected_error as exception: - assert hasattr(exception, 'metric') + assert isinstance(exception.args[-1], core.Metric) # Got a valid error as expected, return quietly return @@ -36,11 +35,11 @@ def _expect_metric_exception(registry, expected_error): core.GaugeMetricFamily, ]) @pytest.mark.parametrize('value,error', [ - (None, exceptions.MetricTypeError), - ('', exceptions.MetricValueError), - ('x', exceptions.MetricValueError), - ([], exceptions.MetricTypeError), - ({}, exceptions.MetricTypeError), + (None, TypeError), + ('', ValueError), + ('x', ValueError), + ([], TypeError), + ({}, TypeError), ]) def test_basic_metric_families(registry, MetricFamily, value, error): metric_family = MetricFamily(MetricFamily.__name__, 'help') @@ -49,14 +48,14 @@ def test_basic_metric_families(registry, MetricFamily, value, error): @pytest.mark.parametrize('count_value,sum_value,error', [ - (None, 0, exceptions.MetricTypeError), - (0, None, exceptions.MetricTypeError), - ('', 0, exceptions.MetricValueError), - (0, '', exceptions.MetricValueError), - ([], 0, exceptions.MetricTypeError), - (0, [], exceptions.MetricTypeError), - ({}, 0, exceptions.MetricTypeError), - (0, {}, exceptions.MetricTypeError), + (None, 0, TypeError), + (0, None, TypeError), + ('', 0, ValueError), + (0, '', ValueError), + ([], 0, TypeError), + (0, [], TypeError), + ({}, 0, TypeError), + (0, {}, TypeError), ]) def test_summary_metric_family(registry, count_value, sum_value, error): metric_family = core.SummaryMetricFamily('summary', 'help') @@ -69,14 +68,14 @@ def test_summary_metric_family(registry, count_value, sum_value, error): core.GaugeHistogramMetricFamily, ]) @pytest.mark.parametrize('buckets,sum_value,error', [ - ([('spam', 0), ('eggs', 0)], None, exceptions.MetricTypeError), - ([('spam', 0), ('eggs', None)], 0, exceptions.MetricTypeError), - ([('spam', 0), (None, 0)], 0, exceptions.MetricAttributeError), - ([('spam', None), ('eggs', 0)], 0, exceptions.MetricTypeError), - ([(None, 0), ('eggs', 0)], 0, exceptions.MetricAttributeError), - ([('spam', 0), ('eggs', 0)], '', exceptions.MetricValueError), - ([('spam', 0), ('eggs', '')], 0, exceptions.MetricValueError), - ([('spam', ''), ('eggs', 0)], 0, exceptions.MetricValueError), + ([('spam', 0), ('eggs', 0)], None, TypeError), + ([('spam', 0), ('eggs', None)], 0, TypeError), + ([('spam', 0), (None, 0)], 0, AttributeError), + ([('spam', None), ('eggs', 0)], 0, TypeError), + ([(None, 0), ('eggs', 0)], 0, AttributeError), + ([('spam', 0), ('eggs', 0)], '', ValueError), + ([('spam', 0), ('eggs', '')], 0, ValueError), + ([('spam', ''), ('eggs', 0)], 0, ValueError), ]) def test_histogram_metric_families(MetricFamily, registry, buckets, sum_value, error): metric_family = MetricFamily(MetricFamily.__name__, 'help')