Skip to content

Commit 89e3728

Browse files
author
Dzmitry Kishylau
authored
[fix] Display better error messages when checking Retool version (#32)
1 parent 706ad04 commit 89e3728

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

internal/provider/provider.go

+18-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package provider
44
import (
55
"context"
66
"encoding/json"
7+
"fmt"
78
"io"
89
"net/http"
910
"os"
@@ -106,12 +107,12 @@ type healthCheckResponse struct {
106107
Version string `json:"version"`
107108
}
108109

109-
func checkMinimalVersion(ctx context.Context, host string, scheme string) bool {
110+
func checkMinimalVersion(ctx context.Context, host string, scheme string) (bool, error) {
110111
// Create HTTP client, make GET /api/checkHealth request, parse the version field out of the JSON response.
111112
httpResponse, err := http.Get(scheme + "://" + host + "/api/checkHealth")
112113
if err != nil {
113-
tflog.Error(ctx, "Failed to check Retool version", map[string]any{"error": err})
114-
return false
114+
tflog.Error(ctx, "Request to /api/checkHealth failed", map[string]any{"error": err})
115+
return false, fmt.Errorf("request to /api/checkHealth failed: %w", err)
115116
}
116117
defer func() {
117118
err := httpResponse.Body.Close()
@@ -123,20 +124,20 @@ func checkMinimalVersion(ctx context.Context, host string, scheme string) bool {
123124
// Read the response body.
124125
body, err := io.ReadAll(httpResponse.Body)
125126
if err != nil {
126-
tflog.Error(ctx, "Failed to read response body", map[string]any{"error": err})
127-
return false
127+
tflog.Error(ctx, "Failed to read healthcheck response body", map[string]any{"error": err})
128+
return false, fmt.Errorf("failed to read healthcheck response body: %w", err)
128129
}
129130

130131
// Parse the JSON response.
131132
var healthCheck healthCheckResponse
132133
err = json.Unmarshal(body, &healthCheck)
133134
if err != nil {
134135
tflog.Error(ctx, "Failed to parse JSON response from healthcheck", map[string]any{"error": err, "body": string(body)})
135-
return false
136+
return false, fmt.Errorf("failed to parse JSON response from healthcheck: %w", err)
136137
}
137138
tflog.Info(ctx, "Retool version", map[string]any{"version": healthCheck.Version})
138139

139-
return semver.Compare("v"+healthCheck.Version, minimumRetoolVersion) >= 0
140+
return semver.Compare("v"+healthCheck.Version, minimumRetoolVersion) >= 0, nil
140141
}
141142

142143
// Configure prepares a Retool API client for data sources and resources.
@@ -152,7 +153,6 @@ func (p *retoolProvider) Configure(ctx context.Context, req provider.ConfigureRe
152153

153154
// If practitioner provided a configuration value for any of the
154155
// attributes, it must be a known value.
155-
156156
if config.Host.IsUnknown() {
157157
resp.Diagnostics.AddAttributeError(
158158
path.Root("host"),
@@ -232,9 +232,16 @@ func (p *retoolProvider) Configure(ctx context.Context, req provider.ConfigureRe
232232

233233
// We only check the minimum version if there's no HTTP client override
234234
// This is a hacky way to avoid doing the check when running acceptance tests in "record" or "replay" mode.
235-
if p.httpClient == nil && !checkMinimalVersion(ctx, host, scheme) {
236-
resp.Diagnostics.AddError("Incompatible Retool version", "The Retool instance version is not supported. Minimum version required is "+minimumRetoolVersion)
237-
return
235+
if p.httpClient == nil {
236+
versionIsCompatible, err := checkMinimalVersion(ctx, host, scheme)
237+
if err != nil {
238+
resp.Diagnostics.AddError("Failed to check Retool version", err.Error())
239+
return
240+
}
241+
if !versionIsCompatible {
242+
resp.Diagnostics.AddError("Incompatible Retool version", "The Retool instance version is not supported. Minimum version required is "+minimumRetoolVersion)
243+
return
244+
}
238245
}
239246

240247
clientConfig := api.NewConfiguration()

0 commit comments

Comments
 (0)