Skip to content

Commit 489f84c

Browse files
duricanikolicclaude
andcommitted
feat: expose Desc error through public Err() method
Adds a public Err() method to Desc type to allow users to check if an error occurred during descriptor construction. Previously, the error field was private and inaccessible. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 23c8ee1 commit 489f84c

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

prometheus/desc.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ func NewInvalidDesc(err error) *Desc {
182182
}
183183
}
184184

185+
// Err returns an error that occurred during construction, if any.
186+
func (d *Desc) Err() error {
187+
return d.err
188+
}
189+
185190
func (d *Desc) String() string {
186191
lpStrings := make([]string, 0, len(d.constLabelPairs))
187192
for _, lp := range d.constLabelPairs {

prometheus/desc_test.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,29 @@ import (
1717
"testing"
1818
)
1919

20-
func TestNewDescInvalidLabelValues(t *testing.T) {
20+
func TestNewDescInvalidConstLabelValues(t *testing.T) {
21+
labelValue := "\xFF"
2122
desc := NewDesc(
2223
"sample_label",
2324
"sample label",
2425
nil,
25-
Labels{"a": "\xFF"},
26+
Labels{"a": labelValue},
2627
)
27-
if desc.err == nil {
28-
t.Errorf("NewDesc: expected error because: %s", desc.err)
28+
if desc.Err() == nil {
29+
t.Errorf("NewDesc: expected error because const label value is invalid: %s", labelValue)
30+
}
31+
}
32+
33+
func TestNewDescInvalidVariableLabelName(t *testing.T) {
34+
labelValue := "__label__"
35+
desc := NewDesc(
36+
"sample_label",
37+
"sample label",
38+
[]string{labelValue},
39+
Labels{"a": "b"},
40+
)
41+
if desc.Err() == nil {
42+
t.Errorf("NewDesc: expected error because variable label name is invalid: %s", labelValue)
2943
}
3044
}
3145

@@ -36,8 +50,8 @@ func TestNewDescNilLabelValues(t *testing.T) {
3650
nil,
3751
nil,
3852
)
39-
if desc.err != nil {
40-
t.Errorf("NewDesc: unexpected error: %s", desc.err)
53+
if desc.Err() != nil {
54+
t.Errorf("NewDesc: unexpected error: %s", desc.Err())
4155
}
4256
}
4357

0 commit comments

Comments
 (0)