mirror of
https://github.com/prometheus/prometheus.git
synced 2025-09-20 21:31:02 +02:00
refactor error creation and tests
Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
This commit is contained in:
parent
b99378f2c4
commit
5b39b79f5a
@ -830,7 +830,7 @@ func (h *FloatHistogram) Validate() error {
|
||||
return errors.New("histogram with exponential schema must not have custom bounds")
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("schema %d: %w", h.Schema, ErrHistogramsInvalidSchema)
|
||||
return InvalidSchemaError(h.Schema)
|
||||
}
|
||||
err := checkHistogramBuckets(h.PositiveBuckets, &pCount, false)
|
||||
if err != nil {
|
||||
|
@ -42,6 +42,10 @@ var (
|
||||
ErrHistogramsInvalidSchema = fmt.Errorf("histogram has an invalid schema, which must be between %d and %d for exponential buckets, or %d for custom buckets", ExponentialSchemaMin, ExponentialSchemaMax, CustomBucketsSchema)
|
||||
)
|
||||
|
||||
func InvalidSchemaError(s int32) error {
|
||||
return fmt.Errorf("%w, got schema %d", ErrHistogramsInvalidSchema, s)
|
||||
}
|
||||
|
||||
func IsCustomBucketsSchema(s int32) bool {
|
||||
return s == CustomBucketsSchema
|
||||
}
|
||||
|
@ -457,7 +457,7 @@ func (h *Histogram) Validate() error {
|
||||
return errors.New("histogram with exponential schema must not have custom bounds")
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("schema %d: %w", h.Schema, ErrHistogramsInvalidSchema)
|
||||
return InvalidSchemaError(h.Schema)
|
||||
}
|
||||
err := checkHistogramBuckets(h.PositiveBuckets, &pCount, true)
|
||||
if err != nil {
|
||||
|
@ -1569,13 +1569,13 @@ func TestHistogramValidation(t *testing.T) {
|
||||
h: &Histogram{
|
||||
Schema: 10,
|
||||
},
|
||||
errMsg: `schema 10: histogram has an invalid schema, which must be between -4 and 8 for exponential buckets, or -53 for custom buckets`,
|
||||
errMsg: `histogram has an invalid schema, which must be between -4 and 8 for exponential buckets, or -53 for custom buckets, got schema 10`,
|
||||
},
|
||||
"schema too low": {
|
||||
h: &Histogram{
|
||||
Schema: -10,
|
||||
},
|
||||
errMsg: `schema -10: histogram has an invalid schema, which must be between -4 and 8 for exponential buckets, or -53 for custom buckets`,
|
||||
errMsg: `histogram has an invalid schema, which must be between -4 and 8 for exponential buckets, or -53 for custom buckets, got schema -10`,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -475,7 +475,7 @@ func validateHistogramSchema(h *prompb.Histogram) error {
|
||||
if histogram.IsValidSchema(h.Schema) {
|
||||
return nil
|
||||
}
|
||||
return return fmt.Errorf("%w, got schema %d", h.Schema, histogram.ErrHistogramsInvalidSchema)
|
||||
return histogram.InvalidSchemaError(h.Schema)
|
||||
}
|
||||
|
||||
func getHistogramValType(h *prompb.Histogram) chunkenc.ValueType {
|
||||
|
@ -956,7 +956,7 @@ func (it *floatHistogramIterator) Next() ValueType {
|
||||
}
|
||||
|
||||
if !histogram.IsValidSchema(schema) {
|
||||
it.err = fmt.Errorf("invalid histogram schema %d", schema)
|
||||
it.err = histogram.InvalidSchemaError(schema)
|
||||
return ValNone
|
||||
}
|
||||
|
||||
|
@ -1488,7 +1488,7 @@ func TestFloatHistogramIteratorFailIfSchemaInValid(t *testing.T) {
|
||||
|
||||
it := c.Iterator(nil)
|
||||
require.Equal(t, ValNone, it.Next())
|
||||
require.EqualError(t, it.Err(), fmt.Sprintf("invalid histogram schema %d", schema))
|
||||
require.ErrorIs(t, it.Err(), histogram.ErrHistogramsInvalidSchema)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1079,7 +1079,7 @@ func (it *histogramIterator) Next() ValueType {
|
||||
}
|
||||
|
||||
if !histogram.IsValidSchema(schema) {
|
||||
it.err = fmt.Errorf("invalid histogram schema %d", schema)
|
||||
it.err = histogram.InvalidSchemaError(schema)
|
||||
return ValNone
|
||||
}
|
||||
|
||||
|
@ -1844,7 +1844,7 @@ func TestHistogramIteratorFailIfSchemaInValid(t *testing.T) {
|
||||
|
||||
it := c.Iterator(nil)
|
||||
require.Equal(t, ValNone, it.Next())
|
||||
require.EqualError(t, it.Err(), fmt.Sprintf("invalid histogram schema %d", schema))
|
||||
require.ErrorIs(t, it.Err(), histogram.ErrHistogramsInvalidSchema)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user