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 all commits
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
34 changes: 33 additions & 1 deletion backend/data_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
"context"
"errors"

"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
grpcstatus "google.golang.org/grpc/status"

"github.com/grafana/grafana-plugin-sdk-go/experimental/status"
"github.com/grafana/grafana-plugin-sdk-go/genproto/pluginv2"
)

Expand All @@ -22,7 +27,7 @@ func (a *dataSDKAdapter) QueryData(ctx context.Context, req *pluginv2.QueryDataR
parsedReq := FromProto().QueryDataRequest(req)
resp, err := a.queryDataHandler.QueryData(ctx, parsedReq)
if err != nil {
return nil, err
return nil, enrichWithErrorSourceInfo(err)
}

if resp == nil {
Expand All @@ -31,3 +36,30 @@ func (a *dataSDKAdapter) QueryData(ctx context.Context, req *pluginv2.QueryDataR

return ToProto().QueryDataResponse(resp)
}

// enrichWithErrorSourceInfo returns a gRPC status error with error source info as metadata.
func enrichWithErrorSourceInfo(err error) error {
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{
"errorSource": errorSource.String(),
},
})
if innerErr != nil {
return err
}

return status.Err()
}
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 @@ package backend
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 @@ func TestQueryData(t *testing.T) {
})
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(_ context.Context, _ *QueryDataRequest) (*QueryDataResponse, error) {
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(_ context.Context, _ *QueryDataRequest) (*QueryDataResponse, error) {
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(_ context.Context, _ *QueryDataRequest) (*QueryDataResponse, error) {
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
4 changes: 4 additions & 0 deletions backend/error_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func ErrorSourceFromHTTPStatus(statusCode int) ErrorSource {
return status.SourceFromHTTPStatus(statusCode)
}

func IsPluginError(err error) bool {
return status.IsPluginError(err)
}

// IsDownstreamError return true if provided error is an error with downstream source or
// a timeout error or a cancelled error.
func IsDownstreamError(err error) bool {
Expand Down
7 changes: 7 additions & 0 deletions experimental/status/status_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ func (e ErrorWithSource) Unwrap() error {
return e.err
}

func IsPluginError(err error) bool {
e := ErrorWithSource{
source: SourcePlugin,
}
return errors.Is(err, e)
}

// IsDownstreamError return true if provided error is an error with downstream source or
// a timeout error or a cancelled error.
func IsDownstreamError(err error) bool {
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