Skip to content

Commit

Permalink
Migrate from experimental/errorsource to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
zoltanbedi committed Dec 5, 2024
1 parent 5ace464 commit 2dee082
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 66 deletions.
31 changes: 15 additions & 16 deletions pkg/infinity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/proxy"
"github.com/grafana/grafana-plugin-sdk-go/backend/tracing"
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
"github.com/icholy/digest"
"golang.org/x/oauth2"
)
Expand Down Expand Up @@ -198,15 +197,15 @@ func (client *Client) req(ctx context.Context, url string, body io.Reader, setti
defer span.End()
req, err := GetRequest(ctx, settings, body, query, requestHeaders, true)
if err != nil {
return nil, http.StatusInternalServerError, 0, errorsource.DownstreamError(fmt.Errorf("error preparing request. %w", err), false)
return nil, http.StatusInternalServerError, 0, backend.DownstreamError(fmt.Errorf("error preparing request. %w", err))
}
if req == nil {
return nil, http.StatusInternalServerError, 0, errorsource.DownstreamError(errors.New("error preparing request. invalid request constructed"), false)
return nil, http.StatusInternalServerError, 0, backend.DownstreamError(errors.New("error preparing request. invalid request constructed"))
}
startTime := time.Now()
if !CanAllowURL(req.URL.String(), settings.AllowedHosts) {
logger.Debug("url is not in the allowed list. make sure to match the base URL with the settings", "url", req.URL.String())
return nil, http.StatusUnauthorized, 0, errorsource.DownstreamError(errors.New("requested URL is not allowed. To allow this URL, update the datasource config Security -> Allowed Hosts section"), false)
return nil, http.StatusUnauthorized, 0, backend.DownstreamError(errors.New("requested URL is not allowed. To allow this URL, update the datasource config Security -> Allowed Hosts section"))
}
logger.Debug("requesting URL", "host", req.URL.Hostname(), "url_path", req.URL.Path, "method", req.Method, "type", query.Type)
res, err := client.HttpClient.Do(req)
Expand All @@ -220,37 +219,37 @@ func (client *Client) req(ctx context.Context, url string, body io.Reader, setti
logger.Debug("error getting response from server", "url", url, "method", req.Method, "error", err.Error(), "status code", res.StatusCode)
// Infinity can query anything and users are responsible for ensuring that endpoint/auth is correct
// therefore any incoming error is considered downstream
return nil, res.StatusCode, duration, errorsource.DownstreamError(fmt.Errorf("error getting response from %s", url), false)
return nil, res.StatusCode, duration, backend.DownstreamError(fmt.Errorf("error getting response from %s", url))
}
if errors.Is(err, context.Canceled) {
logger.Debug("request cancelled", "url", url, "method", req.Method)
return nil, http.StatusInternalServerError, duration, errorsource.DownstreamError(err, false)
return nil, http.StatusInternalServerError, duration, backend.DownstreamError(err)
}
logger.Debug("error getting response from server. no response received", "url", url, "error", err.Error())
return nil, http.StatusInternalServerError, duration, errorsource.DownstreamError(fmt.Errorf("error getting response from url %s. no response received. Error: %w", url, err), false)
return nil, http.StatusInternalServerError, duration, backend.DownstreamError(fmt.Errorf("error getting response from url %s. no response received. Error: %w", url, err))
}
if res == nil {
logger.Debug("invalid response from server and also no error", "url", url, "method", req.Method)
return nil, http.StatusInternalServerError, duration, errorsource.DownstreamError(fmt.Errorf("invalid response received for the URL %s", url), false)
return nil, http.StatusInternalServerError, duration, backend.DownstreamError(fmt.Errorf("invalid response received for the URL %s", url))
}
if res.StatusCode >= http.StatusBadRequest {
err = fmt.Errorf("%w. %s", ErrUnsuccessfulHTTPResponseStatus, res.Status)
// Infinity can query anything and users are responsible for ensuring that endpoint/auth is correct
// therefore any incoming error is considered downstream
return nil, res.StatusCode, duration, errorsource.DownstreamError(err, false)
return nil, res.StatusCode, duration, backend.DownstreamError(err)
}
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
logger.Debug("error reading response body", "url", url, "error", err.Error())
return nil, res.StatusCode, duration, errorsource.DownstreamError(err, false)
return nil, res.StatusCode, duration, backend.DownstreamError(err)
}
bodyBytes = removeBOMContent(bodyBytes)
if CanParseAsJSON(query.Type, res.Header) {
var out any
err := json.Unmarshal(bodyBytes, &out)
if err != nil {
err = fmt.Errorf("%w. %w", ErrParsingResponseBodyAsJson, err)
err = errorsource.DownstreamError(err, false)
err = backend.DownstreamError(err)
logger.Debug("error un-marshaling JSON response", "url", url, "error", err.Error())
}
return out, res.StatusCode, duration, err
Expand All @@ -267,27 +266,27 @@ func (client *Client) GetResults(ctx context.Context, query models.Query, reques
logger := backend.Logger.FromContext(ctx)
if query.Source == "azure-blob" {
if strings.TrimSpace(query.AzBlobContainerName) == "" || strings.TrimSpace(query.AzBlobName) == "" {
return nil, http.StatusBadRequest, 0, errorsource.DownstreamError(errors.New("invalid/empty container name/blob name"), false)
return nil, http.StatusBadRequest, 0, backend.DownstreamError(errors.New("invalid/empty container name/blob name"))
}
if client.AzureBlobClient == nil {
return nil, http.StatusInternalServerError, 0, errorsource.PluginError(errors.New("invalid azure blob client"), false)
return nil, http.StatusInternalServerError, 0, backend.PluginError(errors.New("invalid azure blob client"))
}
blobDownloadResponse, err := client.AzureBlobClient.DownloadStream(ctx, strings.TrimSpace(query.AzBlobContainerName), strings.TrimSpace(query.AzBlobName), nil)
if err != nil {
return nil, http.StatusInternalServerError, 0, errorsource.DownstreamError(err, false)
return nil, http.StatusInternalServerError, 0, backend.DownstreamError(err)
}
reader := blobDownloadResponse.Body
bodyBytes, err := io.ReadAll(reader)
if err != nil {
return nil, http.StatusInternalServerError, 0, errorsource.PluginError(fmt.Errorf("error reading blob content. %w", err), false)
return nil, http.StatusInternalServerError, 0, backend.PluginError(fmt.Errorf("error reading blob content. %w", err))
}
bodyBytes = removeBOMContent(bodyBytes)
if CanParseAsJSON(query.Type, http.Header{}) {
var out any
err := json.Unmarshal(bodyBytes, &out)
if err != nil {
logger.Error("error un-marshaling blob content", "error", err.Error())
err = errorsource.PluginError(err, false)
err = backend.PluginError(err)
}
return out, http.StatusOK, duration, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/infinity/csvBackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"errors"

"github.com/grafana/grafana-infinity-datasource/pkg/models"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/tracing"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
"github.com/grafana/infinity-libs/lib/go/csvframer"
"github.com/grafana/infinity-libs/lib/go/gframer"
)
Expand Down Expand Up @@ -48,9 +48,9 @@ func GetCSVBackendResponse(ctx context.Context, responseString string, query mod
}
if err != nil {
if errors.Is(err, csvframer.ErrEmptyCsv) || errors.Is(err, csvframer.ErrReadingCsvResponse) {
err = errorsource.DownstreamError(err, false)
err = backend.DownstreamError(err)
} else {
err = errorsource.PluginError(err , false)
err = backend.PluginError(err)
}
}
return frame, err
Expand Down
5 changes: 2 additions & 3 deletions pkg/infinity/googleSheets.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/grafana/grafana-infinity-datasource/pkg/models"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
"github.com/grafana/infinity-libs/lib/go/gframer"
)

Expand Down Expand Up @@ -49,7 +48,7 @@ func GetGoogleSheetsResponse(ctx context.Context, urlResponseObject any, query m
Query: query,
Error: "invalid response received from google sheets",
}
return frame, errorsource.DownstreamError(errors.New("invalid response received from google sheets"), false)
return frame, backend.DownstreamError(errors.New("invalid response received from google sheets"))
}
sheet := &Spreadsheet{}
if err := json.Unmarshal([]byte(sheetsString), &sheet); err != nil {
Expand All @@ -58,7 +57,7 @@ func GetGoogleSheetsResponse(ctx context.Context, urlResponseObject any, query m
Query: query,
Error: "invalid response received from google sheets",
}
return frame, errorsource.DownstreamError(errors.New("invalid response received from google sheets"), false)
return frame, backend.DownstreamError(errors.New("invalid response received from google sheets"))
}
if sheet != nil && len(sheet.Sheets) > 0 && len(sheet.Sheets[0].Data) > 0 {
parsedCSV := [][]string{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/infinity/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"fmt"

"github.com/grafana/grafana-infinity-datasource/pkg/models"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
"github.com/grafana/infinity-libs/lib/go/jsonframer"
)

Expand Down Expand Up @@ -49,7 +49,7 @@ func GetFrameForInlineSources(ctx context.Context, query models.Query) (*data.Fr
})
if err != nil {
if errors.Is(err, jsonframer.ErrInvalidRootSelector) || errors.Is(err, jsonframer.ErrInvalidJSONContent) || errors.Is(err, jsonframer.ErrEvaluatingJSONata) {
return frame, errorsource.DownstreamError(fmt.Errorf("error converting json data to frame: %w", err), false)
return frame, backend.DownstreamError(fmt.Errorf("error converting json data to frame: %w", err))
}
return frame, err
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/infinity/jsonBackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/tracing"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
"github.com/grafana/infinity-libs/lib/go/jsonframer"
)

Expand All @@ -23,7 +22,7 @@ func GetJSONBackendResponse(ctx context.Context, urlResponseObject any, query mo
if err != nil {
logger.Error("error json parsing root data", "error", err.Error())
frame.Meta.Custom = &CustomMeta{Query: query, Error: err.Error()}
return frame, errorsource.PluginError(fmt.Errorf("error parsing json root data"), false)
return frame, backend.PluginError(fmt.Errorf("error parsing json root data"))
}
columns := []jsonframer.ColumnSelector{}
for _, c := range query.Columns {
Expand All @@ -42,9 +41,9 @@ func GetJSONBackendResponse(ctx context.Context, urlResponseObject any, query mo

if err != nil {
if errors.Is(err, jsonframer.ErrInvalidRootSelector) || errors.Is(err, jsonframer.ErrInvalidJSONContent) || errors.Is(err, jsonframer.ErrEvaluatingJSONata) {
return frame, errorsource.DownstreamError(fmt.Errorf("error converting json data to frame: %w", err), false)
return frame, backend.DownstreamError(fmt.Errorf("error converting json data to frame: %w", err))
}
return frame, errorsource.PluginError(fmt.Errorf("error converting json data to frame: %w", err), false)
return frame, backend.PluginError(fmt.Errorf("error converting json data to frame: %w", err))
}
if newFrame != nil {
frame.Fields = append(frame.Fields, newFrame.Fields...)
Expand Down
4 changes: 2 additions & 2 deletions pkg/infinity/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"time"

"github.com/grafana/grafana-infinity-datasource/pkg/models"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/tracing"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
)

type CustomMeta struct {
Expand Down Expand Up @@ -47,7 +47,7 @@ func WrapMetaForInlineQuery(ctx context.Context, frame *data.Frame, err error, q
customMeta := &CustomMeta{Query: query, Data: query.Data, ResponseCodeFromServer: 0}
if err != nil {
customMeta.Error = err.Error()
err = errorsource.PluginError(err, false)
err = backend.PluginError(err)
}
frame.Meta = &data.FrameMeta{
ExecutedQueryString: "This feature is not available for this type of query yet",
Expand Down
7 changes: 3 additions & 4 deletions pkg/infinity/postprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/tracing"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
"github.com/grafana/infinity-libs/lib/go/framesql"
t "github.com/grafana/infinity-libs/lib/go/transformations"
)
Expand All @@ -27,7 +26,7 @@ func PostProcessFrame(ctx context.Context, frame *data.Frame, query models.Query
if err != nil {
logger.Error("error getting computed column", "error", err.Error())
frame.Meta.Custom = &CustomMeta{Query: query, Error: err.Error()}
return frame, errorsource.PluginError(err, false)
return frame, backend.PluginError(err)
}
frame, err = t.ApplyFilter(frame, query.FilterExpression)
if err != nil {
Expand Down Expand Up @@ -78,8 +77,8 @@ func addErrorSourceToTransformError(err error) error {

for _, e := range downstreamErrors {
if errors.Is(err, e) {
return errorsource.DownstreamError(err, false)
return backend.DownstreamError(err)
}
}
return errorsource.PluginError(err, false)
return backend.PluginError(err)
}
4 changes: 2 additions & 2 deletions pkg/infinity/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/grafana/grafana-infinity-datasource/pkg/models"
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
"github.com/grafana/grafana-plugin-sdk-go/backend"
)

func UpdateQueryWithReferenceData(ctx context.Context, query models.Query, settings models.InfinitySettings) (models.Query, error) {
Expand All @@ -18,7 +18,7 @@ func UpdateQueryWithReferenceData(ctx context.Context, query models.Query, setti
return query, nil
}
}
return query, errorsource.DownstreamError(errors.New("error getting reference data. Either empty or not defined"), false)
return query, backend.DownstreamError(errors.New("error getting reference data. Either empty or not defined"))
}
return query, nil
}
5 changes: 2 additions & 3 deletions pkg/infinity/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/tracing"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
"github.com/grafana/infinity-libs/lib/go/jsonframer"
"github.com/grafana/infinity-libs/lib/go/transformations"
)
Expand Down Expand Up @@ -214,11 +213,11 @@ func GetFrameForURLSourcesWithPostProcessing(ctx context.Context, query models.Q
if query.PageMode == models.PaginationModeCursor && strings.TrimSpace(query.PageParamCursorFieldExtractionPath) != "" {
body, err := json.Marshal(urlResponseObject)
if err != nil {
return frame, cursor, errorsource.PluginError(errors.New("error while finding the cursor value"), false)
return frame, cursor, backend.PluginError(errors.New("error while finding the cursor value"))
}
cursor, err = jsonframer.GetRootData(string(body), query.PageParamCursorFieldExtractionPath)
if err != nil {
return frame, cursor, errorsource.PluginError(errors.New("error while extracting the cursor value"), false)
return frame, cursor, backend.PluginError(errors.New("error while extracting the cursor value"))
}
}
return frame, cursor, nil
Expand Down
3 changes: 1 addition & 2 deletions pkg/infinity/transformations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/grafana/grafana-infinity-datasource/pkg/models"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
"github.com/grafana/infinity-libs/lib/go/transformations"
)

Expand All @@ -28,7 +27,7 @@ func ApplyTransformations(query models.Query, input *backend.QueryDataResponse)
}
response, err = ApplyTransformation(query, t, response)
if err != nil {
return response, errorsource.PluginError(err, false)
return response, backend.PluginError(err)
}
}
for k := range response.Responses {
Expand Down
4 changes: 2 additions & 2 deletions pkg/infinity/xmlBackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"

"github.com/grafana/grafana-infinity-datasource/pkg/models"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/tracing"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
"github.com/grafana/infinity-libs/lib/go/jsonframer"
"github.com/grafana/infinity-libs/lib/go/xmlframer"
)
Expand All @@ -33,7 +33,7 @@ func GetXMLBackendResponse(ctx context.Context, inputString string, query models
frame.Fields = append(frame.Fields, newFrame.Fields...)
}
if err != nil {
err = errorsource.PluginError(err, false)
err = backend.PluginError(err)
}
return frame, err
}
13 changes: 6 additions & 7 deletions pkg/models/macros.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
m "github.com/grafana/infinity-libs/lib/go/macros"
)

Expand All @@ -19,7 +18,7 @@ func getMatches(macroName, input string) ([][]string, error) {
macroRegex := fmt.Sprintf("\\$__%s\\b(?:\\((.*?)\\))?", macroName)
rgx, err := regexp.Compile(macroRegex)
if err != nil {
return nil, errorsource.PluginError(err, false)
return nil, backend.PluginError(err)
}
return rgx.FindAllStringSubmatch(input, -1), nil
}
Expand All @@ -38,7 +37,7 @@ func InterPolateMacros(queryString string, timeRange backend.TimeRange, pluginCo
macros := map[string]macroFunc{
"combineValues": func(query string, args []string) (string, error) {
if len(args) <= 3 {
return query, errorsource.DownstreamError(errors.New("insufficient arguments to combineValues macro"), false)
return query, backend.DownstreamError(errors.New("insufficient arguments to combineValues macro"))
}
if len(args) == 4 && args[3] == "*" {
return "", nil
Expand All @@ -57,7 +56,7 @@ func InterPolateMacros(queryString string, timeRange backend.TimeRange, pluginCo
},
"customInterval": func(query string, args []string) (string, error) {
if len(args) == 0 {
return query, errorsource.DownstreamError(errors.New("insufficient arguments to customInterval macro"), false)
return query, backend.DownstreamError(errors.New("insufficient arguments to customInterval macro"))
}
for argI := range args {
if argI == len(args)-1 {
Expand All @@ -66,7 +65,7 @@ func InterPolateMacros(queryString string, timeRange backend.TimeRange, pluginCo
if argI%2 != 0 {
duration, err := gtime.ParseDuration(args[argI-1])
if err != nil {
return query, errorsource.DownstreamError(errors.New("invalid customInterval macro"), false)
return query, backend.DownstreamError(errors.New("invalid customInterval macro"))
}
if timeRangeInMilliSeconds <= duration.Milliseconds() {
return args[argI], nil
Expand All @@ -79,7 +78,7 @@ func InterPolateMacros(queryString string, timeRange backend.TimeRange, pluginCo
for key, macro := range macros {
matches, err := getMatches(key, queryString)
if err != nil {
return queryString, errorsource.PluginError(err, false)
return queryString, err
}
for _, match := range matches {
if len(match) == 0 {
Expand All @@ -91,7 +90,7 @@ func InterPolateMacros(queryString string, timeRange backend.TimeRange, pluginCo
}
res, err := macro(queryString, args)
if err != nil {
return queryString, errorsource.PluginError(err, false)
return queryString, err
}
queryString = strings.Replace(queryString, match[0], res, -1)
}
Expand Down
Loading

0 comments on commit 2dee082

Please sign in to comment.