Elasticsearch: Invalid URL and unsupported protocol should be downstream errors (#100886)

* Invalid URL and unsupported protocol should be downstream errors

* Fix lint

* Change from errors.Is to errors.As
This commit is contained in:
Ivana Huckova
2025-02-20 10:04:48 +01:00
committed by GitHub
parent f69e81facf
commit 30aa676724
2 changed files with 11 additions and 1 deletions
+1 -1
View File
@@ -135,7 +135,7 @@ func (c *baseClientImpl) executeRequest(method, uriPath, uriQuery string, body [
c.logger.Debug("Sending request to Elasticsearch", "url", c.ds.URL)
u, err := url.Parse(c.ds.URL)
if err != nil {
return nil, err
return nil, backend.DownstreamError(fmt.Errorf("URL could not be parsed: %w", err))
}
u.Path = path.Join(u.Path, uriPath)
u.RawQuery = uriQuery
+10
View File
@@ -3,10 +3,13 @@ package elasticsearch
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"regexp"
"slices"
"strconv"
"strings"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
@@ -82,6 +85,13 @@ func (e *elasticsearchDataQuery) execute() (*backend.QueryDataResponse, error) {
if backend.IsDownstreamHTTPError(err) {
err = backend.DownstreamError(err)
}
var urlErr *url.Error
if errors.As(err, &urlErr) {
// Unsupported protocol scheme is a common error when the URL is not valid and should be treated as a downstream error
if urlErr.Err != nil && strings.HasPrefix(urlErr.Err.Error(), "unsupported protocol scheme") {
err = backend.DownstreamError(err)
}
}
response.Responses[e.dataQueries[0].RefID] = backend.ErrorResponseWithErrorSource(err)
return response, nil
}