1# Copyright 2015 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5"""Classes representing errors that can be raised by the monitoring library.""" 6 7 8class MonitoringError(Exception): 9 """Base class for exceptions raised by this module.""" 10 11 12class MonitoringDecreasingValueError(MonitoringError): 13 """Raised when setting a metric value that should increase but doesn't.""" 14 15 def __init__(self, metric, old_value, new_value): 16 self.metric = metric 17 self.old_value = old_value 18 self.new_value = new_value 19 20 def __str__(self): 21 return ('Monotonically increasing metric "%s" was given value "%s", which ' 22 'is not greater than or equal to "%s".' % ( 23 self.metric, self.new_value, self.old_value)) 24 25 26class MonitoringDuplicateRegistrationError(MonitoringError): 27 """Raised when trying to register a metric with the same name as another.""" 28 29 def __init__(self, metric): 30 self.metric = metric 31 32 def __str__(self): 33 return 'Different metrics with the same name "%s" were both registered.' % ( 34 self.metric) 35 36 37class MonitoringIncrementUnsetValueError(MonitoringError): 38 """Raised when trying to increment a metric which hasn't been set.""" 39 40 def __init__(self, metric): 41 self.metric = metric 42 43 def __str__(self): 44 return 'Metric "%s" was incremented without first setting a value.' % ( 45 self.metric) 46 47 48class MonitoringInvalidValueTypeError(MonitoringError): 49 """Raised when sending a metric value is not a valid type.""" 50 51 def __init__(self, metric, value): 52 self.metric = metric 53 self.value = value 54 55 def __str__(self): 56 return 'Metric "%s" was given invalid value "%s" (%s).' % ( 57 self.metric, self.value, type(self.value)) 58 59 60class MonitoringInvalidFieldTypeError(MonitoringError): 61 """Raised when sending a metric with a field value of an invalid type.""" 62 63 def __init__(self, metric, field, value): 64 self.metric = metric 65 self.field = field 66 self.value = value 67 68 def __str__(self): 69 return 'Metric "%s" was given field "%s" with invalid value "%s" (%s).' % ( 70 self.metric, self.field, self.value, type(self.value)) 71 72 73class MonitoringTooManyFieldsError(MonitoringError): 74 """Raised when sending a metric with more than 7 fields.""" 75 76 def __init__(self, metric, fields): 77 self.metric = metric 78 self.fields = fields 79 80 def __str__(self): 81 return 'Metric "%s" was given too many (%d > 7) fields: %s.' % ( 82 self.metric, len(self.fields), self.fields) 83 84 85class MonitoringNoConfiguredMonitorError(MonitoringError): 86 """Raised when sending a metric without configuring the global Monitor.""" 87 88 def __init__(self, metric): 89 self.metric = metric 90 91 def __str__(self): 92 if self.metric is not None: 93 return 'Metric "%s" was sent before initializing the global Monitor.' % ( 94 self.metric) 95 else: 96 return 'Metrics were sent before initializing the global Monitor.' 97 98 99class MonitoringNoConfiguredTargetError(MonitoringError): 100 """Raised when sending a metric with no global nor local Target.""" 101 102 def __init__(self, metric): 103 self.metric = metric 104 105 def __str__(self): 106 if self.metric is not None: 107 return 'Metric "%s" was sent with no Target configured.' % (self.metric) 108 else: 109 return 'Metrics were sent with no Target configured.' 110 111 112class MonitoringFailedToFlushAllMetricsError(MonitoringError): 113 """Raised when some error is encountered in flushing specific metrics.""" 114 115 def __init__(self, error_count): 116 self.error_count = error_count 117 118 def __str__(self): 119 return ('Failed to flush %d metrics. See tracebacks above' % 120 (self.error_count)) 121 122 123class MetricDefinitionError(MonitoringError): 124 """Raised when a metric was defined incorrectly.""" 125 126 127class WrongFieldsError(MonitoringError): 128 """Raised when a metric is given different fields to its definition.""" 129 130 def __init__(self, metric_name, got, expected): 131 self.metric_name = metric_name 132 self.got = got 133 self.expected = expected 134 135 def __str__(self): 136 return 'Metric "%s" is defined with %s fields but was given %s' % ( 137 self.metric_name, self.expected, self.got) 138