Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enrich errors with error source using grpc status package #1163

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion backend/data_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ func (a *dataSDKAdapter) QueryData(ctx context.Context, req *pluginv2.QueryDataR

// enrichWithErrorSourceInfo returns a gRPC status error with error source info as metadata.
func enrichWithErrorSourceInfo(err error) error {
errorSource := status.DefaultSource
var errorSource status.Source
if IsDownstreamError(err) {
errorSource = status.SourceDownstream
} else if IsPluginError(err) {
errorSource = status.SourcePlugin
}

// Unless the error is explicitly marked as a plugin or downstream error, we don't enrich it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if errorSource == "" {
return err
}

status := grpcstatus.New(codes.Unknown, err.Error())
status, innerErr := status.WithDetails(&errdetails.ErrorInfo{
Metadata: map[string]string{
Expand Down
72 changes: 72 additions & 0 deletions backend/data_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana-plugin-sdk-go/genproto/pluginv2"
Expand Down Expand Up @@ -147,6 +150,75 @@
})
require.NoError(t, err)
})

t.Run("Error source error from QueryData handler will be enriched with grpc status", func(t *testing.T) {
t.Run("When error is a downstream error", func(t *testing.T) {
adapter := newDataSDKAdapter(QueryDataHandlerFunc(
func(ctx context.Context, req *QueryDataRequest) (*QueryDataResponse, error) {

Check failure on line 157 in backend/data_adapter_test.go

View workflow job for this annotation

GitHub Actions / Lint, Build, and Test

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
return nil, DownstreamError(errors.New("oh no"))
},
))

_, err := adapter.QueryData(context.Background(), &pluginv2.QueryDataRequest{
PluginContext: &pluginv2.PluginContext{},
})
require.Error(t, err)

st := status.Convert(err)
require.NotNil(t, st)
require.NotEmpty(t, st.Details())
for _, detail := range st.Details() {
errorInfo, ok := detail.(*errdetails.ErrorInfo)
require.True(t, ok)
require.NotNil(t, errorInfo)
errorSource, exists := errorInfo.Metadata["errorSource"]
require.True(t, exists)
require.Equal(t, ErrorSourceDownstream.String(), errorSource)
}
})

t.Run("When error is a plugin error", func(t *testing.T) {
adapter := newDataSDKAdapter(QueryDataHandlerFunc(
func(ctx context.Context, req *QueryDataRequest) (*QueryDataResponse, error) {

Check failure on line 182 in backend/data_adapter_test.go

View workflow job for this annotation

GitHub Actions / Lint, Build, and Test

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
return nil, PluginError(errors.New("oh no"))
},
))

_, err := adapter.QueryData(context.Background(), &pluginv2.QueryDataRequest{
PluginContext: &pluginv2.PluginContext{},
})
require.Error(t, err)

st := status.Convert(err)
require.NotNil(t, st)
require.NotEmpty(t, st.Details())
for _, detail := range st.Details() {
errorInfo, ok := detail.(*errdetails.ErrorInfo)
require.True(t, ok)
require.NotNil(t, errorInfo)
errorSource, exists := errorInfo.Metadata["errorSource"]
require.True(t, exists)
require.Equal(t, ErrorSourcePlugin.String(), errorSource)
}
})

t.Run("When error is neither a downstream or plugin error", func(t *testing.T) {
adapter := newDataSDKAdapter(QueryDataHandlerFunc(
func(ctx context.Context, req *QueryDataRequest) (*QueryDataResponse, error) {

Check failure on line 207 in backend/data_adapter_test.go

View workflow job for this annotation

GitHub Actions / Lint, Build, and Test

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
return nil, errors.New("oh no")
},
))

_, err := adapter.QueryData(context.Background(), &pluginv2.QueryDataRequest{
PluginContext: &pluginv2.PluginContext{},
})
require.Error(t, err)

st := status.Convert(err)
require.NotNil(t, st)
require.Empty(t, st.Details())
})
})
}

var finalRoundTripper = httpclient.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
Expand Down
41 changes: 41 additions & 0 deletions experimental/status/status_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,47 @@ func TestIsDownstreamError(t *testing.T) {
}
}

func TestIsPluginError(t *testing.T) {
tcs := []struct {
name string
err error
expected bool
}{
{
name: "nil",
err: nil,
expected: false,
},
{
name: "plugin error",
err: backend.NewErrorWithSource(nil, backend.ErrorSourcePlugin),
expected: true,
},
{
name: "downstream error",
err: backend.NewErrorWithSource(nil, backend.ErrorSourceDownstream),
expected: false,
},
{
name: "other error",
err: fmt.Errorf("other error"),
expected: false,
},
{
name: "network error",
err: newFakeNetworkError(true, true),
expected: false,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
wrappedErr := fmt.Errorf("error: %w", tc.err)
assert.Equalf(t, tc.expected, status.IsPluginError(tc.err), "IsPluginError(%v)", tc.err)
assert.Equalf(t, tc.expected, status.IsPluginError(wrappedErr), "wrapped IsPluginError(%v)", wrappedErr)
})
}
}

func TestIsDownstreamHTTPError(t *testing.T) {
tcs := []struct {
name string
Expand Down
Loading