Skip to content

Commit

Permalink
Alerting: Add QueryError to expr package (grafana#41737)
Browse files Browse the repository at this point in the history
  • Loading branch information
grobinson-grafana authored Nov 16, 2021
1 parent 15d6da8 commit 543b1a7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/expr/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ var (
logger = log.New("expr")
)

type QueryError struct {
RefID string
Err error
}

func (e QueryError) Error() string {
return fmt.Sprintf("failed to execute query %s: %s", e.RefID, e.Err)
}

// baseNode includes common properties used across DPNodes.
type baseNode struct {
id int64
Expand Down Expand Up @@ -240,7 +249,7 @@ func (dn *DSNode) Execute(ctx context.Context, vars mathexp.Vars, s *Service) (m
vals := make([]mathexp.Value, 0)
for refID, qr := range resp.Responses {
if qr.Error != nil {
return mathexp.Results{}, fmt.Errorf("failed to execute query %v: %w", refID, qr.Error)
return mathexp.Results{}, QueryError{RefID: refID, Err: qr.Error}
}

if len(qr.Frames) == 1 {
Expand Down
16 changes: 16 additions & 0 deletions pkg/expr/nodes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package expr

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func TestQueryError(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")
}

0 comments on commit 543b1a7

Please sign in to comment.