Skip to content

Commit db1192e

Browse files
authored
Handle URL parse error (#155)
Signed-off-by: Pulak Kanti Bhowmick <[email protected]>
1 parent 43693eb commit db1192e

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

rest-request.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,20 @@ type StatusMessage struct {
5858
// NewClient initializes client for interacting with an instance of Grafana server;
5959
// apiKeyOrBasicAuth accepts either 'username:password' basic authentication credentials,
6060
// or a Grafana API key
61-
func NewClient(apiURL, apiKeyOrBasicAuth string, client *http.Client) *Client {
61+
func NewClient(apiURL, apiKeyOrBasicAuth string, client *http.Client) (*Client, error) {
6262
key := ""
6363
basicAuth := strings.Contains(apiKeyOrBasicAuth, ":")
64-
baseURL, _ := url.Parse(apiURL)
64+
baseURL, err := url.Parse(apiURL)
65+
if err != nil {
66+
return nil, err
67+
}
6568
if !basicAuth {
6669
key = fmt.Sprintf("Bearer %s", apiKeyOrBasicAuth)
6770
} else {
6871
parts := strings.Split(apiKeyOrBasicAuth, ":")
6972
baseURL.User = url.UserPassword(parts[0], parts[1])
7073
}
71-
return &Client{baseURL: baseURL.String(), basicAuth: basicAuth, key: key, client: client}
74+
return &Client{baseURL: baseURL.String(), basicAuth: basicAuth, key: key, client: client}, nil
7275
}
7376

7477
func (r *Client) get(ctx context.Context, query string, params url.Values) ([]byte, int, error) {

0 commit comments

Comments
 (0)