Skip to content

Commit

Permalink
Fix error source bug in ErrorResponseWithErrorSource (#1158)
Browse files Browse the repository at this point in the history
* Fix error source bug in ErrorResponseWithErrorSource

* Fix lint

* Add tests
  • Loading branch information
ivanahuckova authored Nov 28, 2024
1 parent 316ae3b commit 7ef35b2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion backend/error_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func ErrorResponseWithErrorSource(err error) DataResponse {
var e ErrorWithSource
if errors.As(err, &e) {
return DataResponse{
Error: e,
Error: err,
ErrorSource: e.ErrorSource(),
}
}
Expand Down
49 changes: 49 additions & 0 deletions backend/error_source_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package backend_test

import (
"errors"
"fmt"
"testing"

"github.com/grafana/grafana-plugin-sdk-go/backend"
Expand All @@ -16,3 +18,50 @@ func TestErrorSource(t *testing.T) {
require.True(t, backend.ErrorSourcePlugin.IsValid())
require.Equal(t, "plugin", backend.ErrorSourcePlugin.String())
}

func TestResponseWithOptions(t *testing.T) {
for _, tc := range []struct {
name string
err error
expErrorMessage string
expErrorSource backend.ErrorSource
}{
{
name: "plugin error",
err: backend.PluginError(errors.New("unknown")),
expErrorMessage: "unknown",
expErrorSource: backend.ErrorSourcePlugin,
},
{
name: "downstream error",
err: backend.DownstreamError(errors.New("bad gateway")),
expErrorMessage: "bad gateway",
expErrorSource: backend.ErrorSourceDownstream,
},
{
name: "wrapped downstream error",
err: fmt.Errorf("wrapped: %w", backend.DownstreamError(errors.New("inside error"))),
expErrorMessage: "wrapped: inside error",
expErrorSource: backend.ErrorSourceDownstream,
},
{
name: "wrapped plugin error",
err: fmt.Errorf("wrapped: %w", backend.PluginError(errors.New("inside error"))),
expErrorMessage: "wrapped: inside error",
expErrorSource: backend.ErrorSourcePlugin,
},
{
name: "non error source error",
err: errors.New("inside error"),
expErrorMessage: "inside error",
expErrorSource: "",
},
} {
t.Run(tc.name, func(t *testing.T) {
res := backend.ErrorResponseWithErrorSource(tc.err)
require.Error(t, res.Error)
require.Equal(t, tc.expErrorMessage, res.Error.Error())
require.Equal(t, tc.expErrorSource, res.ErrorSource)
})
}
}

0 comments on commit 7ef35b2

Please # to comment.