-
Notifications
You must be signed in to change notification settings - Fork 21
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
Add errorsource to errors #449
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |
"github.com/grafana/grafana-plugin-sdk-go/backend" | ||
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt" | ||
"github.com/grafana/grafana-plugin-sdk-go/backend/log" | ||
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource" | ||
"github.com/grafana/opensearch-datasource/pkg/opensearch/client" | ||
) | ||
|
||
|
@@ -60,25 +61,25 @@ func (ds *OpenSearchDatasource) QueryData(ctx context.Context, req *backend.Quer | |
return nil, err | ||
} | ||
|
||
errRefID, err := handleServiceMapPrefetch(ctx, osClient, req) | ||
if err != nil { | ||
return wrapServiceMapPrefetchError(errRefID, err) | ||
response := handleServiceMapPrefetch(ctx, osClient, req) | ||
if response != nil { | ||
return response, nil | ||
} | ||
|
||
query := newQueryRequest(osClient, req.Queries, req.PluginContext.DataSourceInstanceSettings) | ||
response, err := wrapError(query.execute(ctx)) | ||
response, err = wrapError(query.execute(ctx)) | ||
return response, err | ||
} | ||
|
||
// handleServiceMapPrefetch inspects the given request, and, if it wants a serviceMap, creates and | ||
// calls the Prefetch query to get the services and operations lists that are required for | ||
// the associated Stats query. It then adds these parameters to the originating query so | ||
// the Stats query can be created later. | ||
func handleServiceMapPrefetch(ctx context.Context, osClient client.Client, req *backend.QueryDataRequest) (string, error) { | ||
// the Stats query can be created later. Returns a response with an error if the request fails. | ||
func handleServiceMapPrefetch(ctx context.Context, osClient client.Client, req *backend.QueryDataRequest) *backend.QueryDataResponse { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There wasn't really a better way to handle the fact that |
||
for i, query := range req.Queries { | ||
model, err := simplejson.NewJson(query.JSON) | ||
if err != nil { | ||
return "", err | ||
return wrapServiceMapPrefetchError(query.RefID, err) | ||
} | ||
queryType := model.Get("queryType").MustString() | ||
luceneQueryType := model.Get("luceneQueryType").MustString() | ||
|
@@ -88,7 +89,9 @@ func handleServiceMapPrefetch(ctx context.Context, osClient client.Client, req * | |
q := newQueryRequest(osClient, []backend.DataQuery{prefetchQuery}, req.PluginContext.DataSourceInstanceSettings) | ||
response, err := q.execute(ctx) | ||
if err != nil { | ||
return query.RefID, err | ||
return wrapServiceMapPrefetchError(query.RefID, err) | ||
} else if response.Responses[query.RefID].Error != nil { | ||
return wrapServiceMapPrefetchError(query.RefID, response.Responses[query.RefID].Error) | ||
} | ||
services, operations := extractParametersFromServiceMapFrames(response) | ||
|
||
|
@@ -99,41 +102,33 @@ func handleServiceMapPrefetch(ctx context.Context, osClient client.Client, req * | |
// An error here _should_ be impossible but since services and operations are coming from outside, | ||
// handle it just in case | ||
if err != nil { | ||
return query.RefID, err | ||
return wrapServiceMapPrefetchError(query.RefID, err) | ||
} | ||
req.Queries[i].JSON = newJson | ||
return "", nil | ||
return nil | ||
} | ||
} | ||
return "", nil | ||
return nil | ||
} | ||
|
||
func wrapServiceMapPrefetchError(refId string, err error) (*backend.QueryDataResponse, error) { | ||
if refId != "" { | ||
return &backend.QueryDataResponse{ | ||
Responses: map[string]backend.DataResponse{ | ||
refId: { | ||
Error: fmt.Errorf(`Error fetching service map info: %w`, err), | ||
}}, | ||
}, nil | ||
func wrapServiceMapPrefetchError(refId string, err error) *backend.QueryDataResponse { | ||
if err != nil { | ||
var sourceError errorsource.Error | ||
source := backend.ErrorSourcePlugin | ||
if errors.As(err, &sourceError) && sourceError.Source() == backend.ErrorSourceDownstream { | ||
source = backend.ErrorSourceDownstream | ||
} | ||
response := backend.NewQueryDataResponse() | ||
err = errorsource.SourceError(source, fmt.Errorf(`Error fetching service map info: %w`, err), true) | ||
iwysiu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return errorsource.AddErrorToResponse(refId, response, err) | ||
} | ||
return nil, err | ||
return nil | ||
} | ||
|
||
func wrapError(response *backend.QueryDataResponse, err error) (*backend.QueryDataResponse, error) { | ||
var invalidQueryTypeError invalidQueryTypeError | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now the error gets wrapped earlier, so we don't need this |
||
if errors.As(err, &invalidQueryTypeError) { | ||
return &backend.QueryDataResponse{ | ||
Responses: map[string]backend.DataResponse{ | ||
invalidQueryTypeError.refId: { | ||
Error: fmt.Errorf(`%w, expected Lucene or PPL`, err), | ||
}}, | ||
}, nil | ||
} | ||
if err != nil { | ||
return response, fmt.Errorf("OpenSearch data source error: %w", err) | ||
} | ||
|
||
return response, err | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is context of removing the
:=
to=
? (I am a bit of a Go noob btw)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also should we keep this version of
wrapError
around vs using the newer wrap for this error? or in this case are we confident that this is an error owned by usThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The difference is that
:=
initializes and sets a variable and=
sets an existing variable. Since response and err have now both been already initialized in this function, we only need to set it (and the compiler will yell at us if we do otherwise). It was able to use:=
before because whileerr
had been initialized, response hadn't.This is the same
wrapError
as it was before (line 128), it just used to also handleinvalidQueryTypeError
which we're now handling earlier.