mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-24 05:04:05 -08:00
Expose Error function for WrappedError (#9662)
This commit is contained in:
parent
8a4f659126
commit
52159840eb
|
@ -38,6 +38,16 @@ type Error struct {
|
|||
Err WrappedError
|
||||
}
|
||||
|
||||
// Error prints the error message in a formatted string.
|
||||
func (err *Error) Error() string {
|
||||
if err.Err.nodeAlt != nil {
|
||||
return errors.Wrapf(err.Err.err, "%d:%d: %d:%d: group %q, rule %d, %q", err.Err.node.Line, err.Err.node.Column, err.Err.nodeAlt.Line, err.Err.nodeAlt.Column, err.Group, err.Rule, err.RuleName).Error()
|
||||
} else if err.Err.node != nil {
|
||||
return errors.Wrapf(err.Err.err, "%d:%d: group %q, rule %d, %q", err.Err.node.Line, err.Err.node.Column, err.Group, err.Rule, err.RuleName).Error()
|
||||
}
|
||||
return errors.Wrapf(err.Err.err, "group %q, rule %d, %q", err.Group, err.Rule, err.RuleName).Error()
|
||||
}
|
||||
|
||||
// WrappedError wraps error with the yaml node which can be used to represent
|
||||
// the line and column numbers of the error.
|
||||
type WrappedError struct {
|
||||
|
@ -46,13 +56,14 @@ type WrappedError struct {
|
|||
nodeAlt *yaml.Node
|
||||
}
|
||||
|
||||
func (err *Error) Error() string {
|
||||
if err.Err.nodeAlt != nil {
|
||||
return errors.Wrapf(err.Err.err, "%d:%d: %d:%d: group %q, rule %d, %q", err.Err.node.Line, err.Err.node.Column, err.Err.nodeAlt.Line, err.Err.nodeAlt.Column, err.Group, err.Rule, err.RuleName).Error()
|
||||
} else if err.Err.node != nil {
|
||||
return errors.Wrapf(err.Err.err, "%d:%d: group %q, rule %d, %q", err.Err.node.Line, err.Err.node.Column, err.Group, err.Rule, err.RuleName).Error()
|
||||
// Error prints the error message in a formatted string.
|
||||
func (we *WrappedError) Error() string {
|
||||
if we.nodeAlt != nil {
|
||||
return errors.Wrapf(we.err, "%d:%d: %d:%d", we.node.Line, we.node.Column, we.nodeAlt.Line, we.nodeAlt.Column).Error()
|
||||
} else if we.node != nil {
|
||||
return errors.Wrapf(we.err, "%d:%d", we.node.Line, we.node.Column).Error()
|
||||
}
|
||||
return errors.Wrapf(err.Err.err, "group %q, rule %d, %q", err.Group, err.Rule, err.RuleName).Error()
|
||||
return we.err.Error()
|
||||
}
|
||||
|
||||
// RuleGroups is a set of rule groups that are typically exposed in a file.
|
||||
|
|
|
@ -14,10 +14,12 @@
|
|||
package rulefmt
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func TestParseFileSuccess(t *testing.T) {
|
||||
|
@ -184,3 +186,116 @@ groups:
|
|||
err1 := errs[1].(*Error).Err.node
|
||||
require.NotEqual(t, err0, err1, "Error nodes should not be the same")
|
||||
}
|
||||
|
||||
func TestError(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
error *Error
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "with alternative node provided in WrappedError",
|
||||
error: &Error{
|
||||
Group: "some group",
|
||||
Rule: 1,
|
||||
RuleName: "some rule name",
|
||||
Err: WrappedError{
|
||||
err: errors.New("some error"),
|
||||
node: &yaml.Node{
|
||||
Line: 10,
|
||||
Column: 20,
|
||||
},
|
||||
nodeAlt: &yaml.Node{
|
||||
Line: 11,
|
||||
Column: 21,
|
||||
},
|
||||
},
|
||||
},
|
||||
want: `10:20: 11:21: group "some group", rule 1, "some rule name": some error`,
|
||||
},
|
||||
{
|
||||
name: "with node provided in WrappedError",
|
||||
error: &Error{
|
||||
Group: "some group",
|
||||
Rule: 1,
|
||||
RuleName: "some rule name",
|
||||
Err: WrappedError{
|
||||
err: errors.New("some error"),
|
||||
node: &yaml.Node{
|
||||
Line: 10,
|
||||
Column: 20,
|
||||
},
|
||||
},
|
||||
},
|
||||
want: `10:20: group "some group", rule 1, "some rule name": some error`,
|
||||
},
|
||||
{
|
||||
name: "with only err provided in WrappedError",
|
||||
error: &Error{
|
||||
Group: "some group",
|
||||
Rule: 1,
|
||||
RuleName: "some rule name",
|
||||
Err: WrappedError{
|
||||
err: errors.New("some error"),
|
||||
},
|
||||
},
|
||||
want: `group "some group", rule 1, "some rule name": some error`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := tt.error.Error()
|
||||
require.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrappedError(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
wrappedError *WrappedError
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "with alternative node provided",
|
||||
wrappedError: &WrappedError{
|
||||
err: errors.New("some error"),
|
||||
node: &yaml.Node{
|
||||
Line: 10,
|
||||
Column: 20,
|
||||
},
|
||||
nodeAlt: &yaml.Node{
|
||||
Line: 11,
|
||||
Column: 21,
|
||||
},
|
||||
},
|
||||
want: `10:20: 11:21: some error`,
|
||||
},
|
||||
{
|
||||
name: "with node provided",
|
||||
wrappedError: &WrappedError{
|
||||
err: errors.New("some error"),
|
||||
node: &yaml.Node{
|
||||
Line: 10,
|
||||
Column: 20,
|
||||
},
|
||||
},
|
||||
want: `10:20: some error`,
|
||||
},
|
||||
{
|
||||
name: "with only err provided",
|
||||
wrappedError: &WrappedError{
|
||||
err: errors.New("some error"),
|
||||
},
|
||||
want: `some error`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := tt.wrappedError.Error()
|
||||
require.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue