From 702c9abf24c4b879b2620f68f633231816a77c31 Mon Sep 17 00:00:00 2001 From: johnnyfish Date: Sat, 9 May 2026 23:35:02 +0300 Subject: [PATCH] fix: replace raw network errors with user-friendly gateway messages --- internal/api/client.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/internal/api/client.go b/internal/api/client.go index 234c0f4..96221ca 100644 --- a/internal/api/client.go +++ b/internal/api/client.go @@ -6,11 +6,13 @@ import ( "crypto/tls" "crypto/x509" "encoding/json" + "errors" "fmt" "io" "net/http" "net/url" "os" + "syscall" "time" ) @@ -76,6 +78,21 @@ func (e *APIError) Error() string { return e.Message } +// networkError translates raw Go network errors into user-friendly messages. +func (c *Client) networkError(err error) error { + host := c.baseURL + if u, e := url.Parse(c.baseURL); e == nil && u.Host != "" { + host = u.Host + } + if errors.Is(err, syscall.ECONNREFUSED) { + return fmt.Errorf("could not connect to gateway at %s — is the OneCLI gateway running?", host) + } + if os.IsTimeout(err) { + return fmt.Errorf("request to gateway at %s timed out", host) + } + return fmt.Errorf("could not reach gateway at %s: %w", host, err) +} + // do executes an HTTP request and decodes the JSON response. // For 204 responses, result should be nil. func (c *Client) do(ctx context.Context, method, path string, body any, result any) error { @@ -102,7 +119,7 @@ func (c *Client) do(ctx context.Context, method, path string, body any, result a resp, err := c.httpClient.Do(req) if err != nil { - return fmt.Errorf("executing request: %w", err) + return c.networkError(err) } defer resp.Body.Close()