Alerting: Support Unwrap for QueryError in expr package (#41743)

This commit is contained in:
George Robinson 2021-11-17 10:07:24 +00:00 committed by GitHub
parent 2319c52c85
commit 708bdc80cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -28,6 +28,10 @@ func (e QueryError) Error() string {
return fmt.Sprintf("failed to execute query %s: %s", e.RefID, e.Err)
}
func (e QueryError) Unwrap() error {
return e.Err
}
// baseNode includes common properties used across DPNodes.
type baseNode struct {
id int64

View File

@ -7,10 +7,36 @@ import (
"github.com/stretchr/testify/assert"
)
func TestQueryError(t *testing.T) {
type expectedError struct{}
func (e expectedError) Error() string {
return "expected"
}
func TestQueryError_Error(t *testing.T) {
e := QueryError{
RefID: "A",
Err: errors.New("this is an error message"),
}
assert.EqualError(t, e, "failed to execute query A: this is an error message")
}
func TestQueryError_Unwrap(t *testing.T) {
t.Run("errors.Is", func(t *testing.T) {
expectedIsErr := errors.New("expected")
e := QueryError{
RefID: "A",
Err: expectedIsErr,
}
assert.True(t, errors.Is(e, expectedIsErr))
})
t.Run("errors.As", func(t *testing.T) {
e := QueryError{
RefID: "A",
Err: expectedError{},
}
var expectedAsError expectedError
assert.True(t, errors.As(e, &expectedAsError))
})
}