Skip to content

Commit

Permalink
Simplified exception system
Browse files Browse the repository at this point in the history
Signed-off-by: Rick van Hattem <[email protected]>
  • Loading branch information
wolph committed Jan 8, 2019
1 parent a3fa450 commit 6b4592e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 61 deletions.
35 changes: 0 additions & 35 deletions prometheus_client/exceptions.py

This file was deleted.

6 changes: 3 additions & 3 deletions prometheus_client/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
45 changes: 22 additions & 23 deletions tests/test_metrics_core.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest

from prometheus_client import core
from prometheus_client import exceptions
from prometheus_client import exposition


Expand All @@ -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

Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand Down

0 comments on commit 6b4592e

Please sign in to comment.