mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-09 23:24:05 -08:00
Implement Unwrap() on errors returned from rulefmt
I'd like to unwrap errors returned from rulefmt but both Error and WrappedError types are missing Unwrap() method. Signed-off-by: Łukasz Mierzwa <l.mierzwa@gmail.com>
This commit is contained in:
parent
c511d26dd0
commit
648b12d8c5
|
@ -48,6 +48,11 @@ func (err *Error) Error() string {
|
|||
return errors.Wrapf(err.Err.err, "group %q, rule %d, %q", err.Group, err.Rule, err.RuleName).Error()
|
||||
}
|
||||
|
||||
// Unwrap unpacks wrapped error for use in errors.Is & errors.As.
|
||||
func (err *Error) Unwrap() error {
|
||||
return &err.Err
|
||||
}
|
||||
|
||||
// WrappedError wraps error with the yaml node which can be used to represent
|
||||
// the line and column numbers of the error.
|
||||
type WrappedError struct {
|
||||
|
@ -66,6 +71,11 @@ func (we *WrappedError) Error() string {
|
|||
return we.err.Error()
|
||||
}
|
||||
|
||||
// Unwrap unpacks wrapped error for use in errors.Is & errors.As.
|
||||
func (we *WrappedError) Unwrap() error {
|
||||
return we.err
|
||||
}
|
||||
|
||||
// RuleGroups is a set of rule groups that are typically exposed in a file.
|
||||
type RuleGroups struct {
|
||||
Groups []RuleGroup `yaml:"groups"`
|
||||
|
|
|
@ -15,6 +15,7 @@ package rulefmt
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
|
@ -299,3 +300,27 @@ func TestWrappedError(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorUnwrap(t *testing.T) {
|
||||
err1 := errors.New("test error")
|
||||
|
||||
tests := []struct {
|
||||
wrappedError *Error
|
||||
unwrappedError error
|
||||
}{
|
||||
{
|
||||
wrappedError: &Error{Err: WrappedError{err: err1}},
|
||||
unwrappedError: err1,
|
||||
},
|
||||
{
|
||||
wrappedError: &Error{Err: WrappedError{err: io.ErrClosedPipe}},
|
||||
unwrappedError: io.ErrClosedPipe,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.wrappedError.Error(), func(t *testing.T) {
|
||||
require.ErrorIs(t, tt.wrappedError, tt.unwrappedError)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue