Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/next-release/bugfix-client-4qrbt6n2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "bugfix",
"category": "client",
"description": "Surface nested API error bodies ({\"error\":{\"message\":...}}) instead of a bare HTTP status, and make the --debug flag log request/response"
}
20 changes: 20 additions & 0 deletions go/internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"io"
"net/http"
"net/url"
"os"
"strings"
"time"

"github.com/vngcloud/greennode-cli/internal/auth"
Expand Down Expand Up @@ -191,6 +193,14 @@ func (c *GreenodeClient) requestRaw(method, path string, params map[string]strin
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")

if c.debug {
fmt.Fprintf(os.Stderr, "[debug] %s %s\n", method, fullURL)
if body != nil {
jsonBody, _ := json.Marshal(body)
fmt.Fprintf(os.Stderr, "[debug] request body: %s\n", string(jsonBody))
}
}

resp, err := c.httpClient.Do(req)
if err != nil {
lastErr = err
Expand All @@ -205,6 +215,10 @@ func (c *GreenodeClient) requestRaw(method, path string, params map[string]strin
respBody, _ := io.ReadAll(resp.Body)
resp.Body.Close()

if c.debug {
fmt.Fprintf(os.Stderr, "[debug] response %d: %s\n", resp.StatusCode, string(respBody))
}

// 401 — refresh token and retry once
if resp.StatusCode == http.StatusUnauthorized {
token, err = c.tokenManager.RefreshToken()
Expand Down Expand Up @@ -266,6 +280,12 @@ func formatError(statusCode int, body []byte) string {
}
}
}
// Fallback: the error payload didn't use a known string field (e.g. the
// VKS API returns {"error": {...}} as a nested object). Surface the raw
// JSON body so the server's message isn't silently dropped.
if detail == "" && len(body) > 0 {
detail = strings.TrimSpace(string(body))
}
} else {
detail = string(body)
}
Expand Down
19 changes: 19 additions & 0 deletions go/internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -39,3 +40,21 @@ func TestPatchSendsPatchMethodAndBody(t *testing.T) {
t.Errorf("body = %q, want enableAutoHealing payload", gotBody)
}
}

func TestFormatErrorSurfacesNestedErrorObject(t *testing.T) {
// VKS returns errors as {"error": {"message": ...}} — a nested object, not a
// string. The detail must still reach the user instead of being dropped.
body := []byte(`{"error":{"message":"KubeConfig can only be requested when the cluster is ACTIVE."}}`)
got := formatError(http.StatusBadRequest, body)
if !strings.Contains(got, "cluster is ACTIVE") {
t.Errorf("formatError = %q, want it to contain the nested error message", got)
}
}

func TestFormatErrorUsesPlainStringMessage(t *testing.T) {
body := []byte(`{"message":"boom"}`)
got := formatError(http.StatusBadRequest, body)
if !strings.Contains(got, "boom") {
t.Errorf("formatError = %q, want it to contain %q", got, "boom")
}
}
Loading