|
| 1 | +package instant |
| 2 | + |
| 3 | +// apierror_envelope_test.go — registry contract for the canonical |
| 4 | +// instanode.dev error envelope (rule 18 style: iterate the registry, not a |
| 5 | +// hand-typed list). The API replies to every 4xx/5xx with |
| 6 | +// |
| 7 | +// {ok, error, error_code, message, agent_action, upgrade_url, |
| 8 | +// retry_after_seconds, request_id} |
| 9 | +// |
| 10 | +// and APIError must give every one of those keys a home — the original |
| 11 | +// struct captured only error+message, silently dropping agent_action, |
| 12 | +// error_code, upgrade_url, retry_after_seconds, and request_id, which |
| 13 | +// defeated the agent-native contract. These tests fail if a future envelope |
| 14 | +// key is added to APIErrorEnvelopeKeys without a tagged field, OR if a sample |
| 15 | +// full body fails to round-trip every field onto the struct. |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + "io" |
| 21 | + "net/http" |
| 22 | + "net/http/httptest" |
| 23 | + "reflect" |
| 24 | + "strings" |
| 25 | + "testing" |
| 26 | +) |
| 27 | + |
| 28 | +// jsonTagsOfAPIError reflects the JSON tag set declared on APIError, skipping |
| 29 | +// untagged fields (StatusCode), the unexported raw field, and json:"-". |
| 30 | +func jsonTagsOfAPIError(t *testing.T) map[string]string { |
| 31 | + t.Helper() |
| 32 | + tags := map[string]string{} |
| 33 | + rt := reflect.TypeOf(APIError{}) |
| 34 | + for i := 0; i < rt.NumField(); i++ { |
| 35 | + f := rt.Field(i) |
| 36 | + tag := f.Tag.Get("json") |
| 37 | + if tag == "" || tag == "-" { |
| 38 | + continue |
| 39 | + } |
| 40 | + name := strings.Split(tag, ",")[0] |
| 41 | + if name == "" { |
| 42 | + continue |
| 43 | + } |
| 44 | + tags[name] = f.Name |
| 45 | + } |
| 46 | + return tags |
| 47 | +} |
| 48 | + |
| 49 | +// TestAPIError_EnvelopeKeysAllHaveAHome asserts every envelope key the SDK |
| 50 | +// claims to map (APIErrorEnvelopeKeys) is backed by a tagged field on |
| 51 | +// APIError. A future API field added to the registry without a struct field |
| 52 | +// reds this test instead of silently dropping at runtime. |
| 53 | +func TestAPIError_EnvelopeKeysAllHaveAHome(t *testing.T) { |
| 54 | + tags := jsonTagsOfAPIError(t) |
| 55 | + for _, key := range APIErrorEnvelopeKeys { |
| 56 | + if _, ok := tags[key]; !ok { |
| 57 | + t.Errorf("envelope key %q in APIErrorEnvelopeKeys has no tagged field on APIError "+ |
| 58 | + "(add the field with `json:%q` so it can't drop)", key, key) |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// TestAPIError_EnvelopeKeysRegistryIsComplete guards the other direction: |
| 64 | +// every documented envelope key the API can emit (the canonical contract |
| 65 | +// list) must appear in APIErrorEnvelopeKeys. This is the registry-iterating |
| 66 | +// guard from rule 18 — if the API adds a key here-but-not-in-the-SDK the test |
| 67 | +// names exactly which one is missing. |
| 68 | +func TestAPIError_EnvelopeKeysRegistryIsComplete(t *testing.T) { |
| 69 | + // The canonical key set the api emits on its ErrorResponse envelope |
| 70 | + // (api/internal/handlers/helpers.go: ErrorResponse). claim_url is the |
| 71 | + // recycle-gate-only alias the SDK folds into upgrade_url; ok is the |
| 72 | + // success/failure flag the SDK reads via the typed predicates, not via |
| 73 | + // APIError — both are intentionally excluded and listed here so the |
| 74 | + // exclusion is explicit rather than an oversight. |
| 75 | + canonical := []string{ |
| 76 | + "error", |
| 77 | + "error_code", |
| 78 | + "message", |
| 79 | + "agent_action", |
| 80 | + "upgrade_url", |
| 81 | + "retry_after_seconds", |
| 82 | + "request_id", |
| 83 | + } |
| 84 | + have := map[string]bool{} |
| 85 | + for _, k := range APIErrorEnvelopeKeys { |
| 86 | + have[k] = true |
| 87 | + } |
| 88 | + for _, k := range canonical { |
| 89 | + if !have[k] { |
| 90 | + t.Errorf("canonical envelope key %q is NOT in APIErrorEnvelopeKeys — "+ |
| 91 | + "the SDK will drop it; add it to the registry and APIError", k) |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// TestAPIError_FullEnvelopeRoundTrips decodes a sample of the complete error |
| 97 | +// envelope and asserts EVERY field lands on the struct — the regression the |
| 98 | +// fix closes (pre-fix, only Code+Message survived). It also asserts the tag |
| 99 | +// mapping: Code holds the category "error", ErrorCode holds the canonical |
| 100 | +// "error_code", and CanonicalCode prefers error_code. |
| 101 | +func TestAPIError_FullEnvelopeRoundTrips(t *testing.T) { |
| 102 | + const retry = 30 |
| 103 | + body := `{ |
| 104 | + "ok": false, |
| 105 | + "error": "unauthorized", |
| 106 | + "error_code": "missing_credentials", |
| 107 | + "message": "No INSTANODE_TOKEN was provided.", |
| 108 | + "agent_action": "Have the user log in at https://instanode.dev/login.", |
| 109 | + "upgrade_url": "https://instanode.dev/login", |
| 110 | + "retry_after_seconds": 30, |
| 111 | + "request_id": "req_abc123" |
| 112 | + }` |
| 113 | + |
| 114 | + var e APIError |
| 115 | + if err := json.Unmarshal([]byte(body), &e); err != nil { |
| 116 | + t.Fatalf("unmarshal: %v", err) |
| 117 | + } |
| 118 | + |
| 119 | + if e.Code != "unauthorized" { |
| 120 | + t.Errorf("Code (json:\"error\", the category) = %q, want %q", e.Code, "unauthorized") |
| 121 | + } |
| 122 | + if e.ErrorCode != "missing_credentials" { |
| 123 | + t.Errorf("ErrorCode (json:\"error_code\") = %q, want %q", e.ErrorCode, "missing_credentials") |
| 124 | + } |
| 125 | + if e.Message != "No INSTANODE_TOKEN was provided." { |
| 126 | + t.Errorf("Message = %q", e.Message) |
| 127 | + } |
| 128 | + if !strings.Contains(e.AgentAction, "log in") { |
| 129 | + t.Errorf("AgentAction not captured: %q", e.AgentAction) |
| 130 | + } |
| 131 | + if e.UpgradeURL != "https://instanode.dev/login" { |
| 132 | + t.Errorf("UpgradeURL = %q", e.UpgradeURL) |
| 133 | + } |
| 134 | + if e.RetryAfterSeconds == nil { |
| 135 | + t.Fatal("RetryAfterSeconds = nil, want non-nil 30") |
| 136 | + } |
| 137 | + if *e.RetryAfterSeconds != retry { |
| 138 | + t.Errorf("RetryAfterSeconds = %d, want %d", *e.RetryAfterSeconds, retry) |
| 139 | + } |
| 140 | + if e.RequestID != "req_abc123" { |
| 141 | + t.Errorf("RequestID = %q", e.RequestID) |
| 142 | + } |
| 143 | + |
| 144 | + // CanonicalCode prefers the finer-grained error_code over the category. |
| 145 | + if got := e.CanonicalCode(); got != "missing_credentials" { |
| 146 | + t.Errorf("CanonicalCode() = %q, want the canonical machine code %q", got, "missing_credentials") |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +// TestAPIError_CanonicalCodeFallback — when the server omits error_code the |
| 151 | +// canonical code falls back to the category (Code / json:"error"). |
| 152 | +func TestAPIError_CanonicalCodeFallback(t *testing.T) { |
| 153 | + e := &APIError{Code: "not_found"} |
| 154 | + if got := e.CanonicalCode(); got != "not_found" { |
| 155 | + t.Errorf("CanonicalCode() with empty ErrorCode = %q, want fallback to Code %q", got, "not_found") |
| 156 | + } |
| 157 | + e2 := &APIError{Code: "unauthorized", ErrorCode: "missing_credentials"} |
| 158 | + if got := e2.CanonicalCode(); got != "missing_credentials" { |
| 159 | + t.Errorf("CanonicalCode() = %q, want %q", got, "missing_credentials") |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +// TestAPIError_ErrorStringFoldsAgentActionAndUpgrade — the Error() string must |
| 164 | +// surface agent_action + upgrade_url so logs are actionable, while staying |
| 165 | +// backward-compatible (no trailing " | ..." when neither is present). |
| 166 | +func TestAPIError_ErrorStringFoldsAgentActionAndUpgrade(t *testing.T) { |
| 167 | + full := &APIError{ |
| 168 | + StatusCode: 402, |
| 169 | + Code: "quota_exceeded", |
| 170 | + ErrorCode: "storage_limit_reached", |
| 171 | + Message: "storage limit reached", |
| 172 | + AgentAction: "Upgrade to Pro at https://instanode.dev/pricing.", |
| 173 | + UpgradeURL: "https://instanode.dev/pricing", |
| 174 | + } |
| 175 | + s := full.Error() |
| 176 | + // canonical code wins over category in the prefix |
| 177 | + if !strings.Contains(s, "(storage_limit_reached)") { |
| 178 | + t.Errorf("Error() should use canonical code: %q", s) |
| 179 | + } |
| 180 | + if !strings.Contains(s, "agent_action: Upgrade to Pro") { |
| 181 | + t.Errorf("Error() must fold in agent_action: %q", s) |
| 182 | + } |
| 183 | + if !strings.Contains(s, "upgrade_url: https://instanode.dev/pricing") { |
| 184 | + t.Errorf("Error() must fold in upgrade_url: %q", s) |
| 185 | + } |
| 186 | + |
| 187 | + // Backward compat: with neither agent_action nor upgrade_url, the string |
| 188 | + // is exactly the legacy shape (no trailing pipes). |
| 189 | + plain := &APIError{StatusCode: 404, Code: "not_found", Message: "Resource not found"} |
| 190 | + if got, want := plain.Error(), "instant.dev API error 404 (not_found): Resource not found"; got != want { |
| 191 | + t.Errorf("Error() backward-compat = %q, want %q", got, want) |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +// TestAPIError_FullEnvelopeOverHTTP wires the full envelope through the real |
| 196 | +// client error path (do → json.Unmarshal(raw, apiErr)) to prove the wire |
| 197 | +// decode — not just a direct unmarshal — populates every field. |
| 198 | +func TestAPIError_FullEnvelopeOverHTTP(t *testing.T) { |
| 199 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 200 | + w.WriteHeader(http.StatusTooManyRequests) |
| 201 | + _, _ = io.WriteString(w, `{"ok":false,"error":"rate_limited","error_code":"too_many_requests",`+ |
| 202 | + `"message":"slow down","agent_action":"Wait 60 seconds and retry.",`+ |
| 203 | + `"upgrade_url":"https://instanode.dev/pricing","retry_after_seconds":60,"request_id":"req_z9"}`) |
| 204 | + })) |
| 205 | + defer srv.Close() |
| 206 | + |
| 207 | + c := New(WithBaseURL(srv.URL)) |
| 208 | + var out map[string]any |
| 209 | + err := c.get(context.Background(), "/x", &out) |
| 210 | + if err == nil { |
| 211 | + t.Fatal("expected error") |
| 212 | + } |
| 213 | + apiErr, ok := err.(*APIError) |
| 214 | + if !ok { |
| 215 | + t.Fatalf("expected *APIError, got %T", err) |
| 216 | + } |
| 217 | + if apiErr.StatusCode != http.StatusTooManyRequests { |
| 218 | + t.Errorf("StatusCode = %d", apiErr.StatusCode) |
| 219 | + } |
| 220 | + if apiErr.ErrorCode != "too_many_requests" { |
| 221 | + t.Errorf("ErrorCode = %q (dropped on the wire path?)", apiErr.ErrorCode) |
| 222 | + } |
| 223 | + if apiErr.AgentAction == "" || apiErr.UpgradeURL == "" || apiErr.RequestID == "" { |
| 224 | + t.Errorf("agent-native fields dropped on the wire path: action=%q upgrade=%q rid=%q", |
| 225 | + apiErr.AgentAction, apiErr.UpgradeURL, apiErr.RequestID) |
| 226 | + } |
| 227 | + if apiErr.RetryAfterSeconds == nil || *apiErr.RetryAfterSeconds != 60 { |
| 228 | + t.Errorf("RetryAfterSeconds not captured: %v", apiErr.RetryAfterSeconds) |
| 229 | + } |
| 230 | + if !IsRateLimited(err) { |
| 231 | + t.Error("IsRateLimited should match the 429") |
| 232 | + } |
| 233 | +} |
0 commit comments