refactor error creation and tests

Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
This commit is contained in:
György Krajcsovits 2025-09-19 09:26:34 +02:00
parent b99378f2c4
commit 5b39b79f5a
No known key found for this signature in database
GPG Key ID: 47A8F9CE80FD7C7F
9 changed files with 13 additions and 9 deletions

View File

@ -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 {

View File

@ -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
}

View File

@ -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 {

View File

@ -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`,
},
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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)
})
}
}

View File

@ -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
}

View File

@ -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)
})
}
}