Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix: the CLI collapses missing_token and bad_token into one 'Error 401: unauthorized', dropping the code the server sends (PIN-210)

Luigi Agostic74a7cd3b424
diff --git a/internal/cli/apiclient/apierror.go b/internal/cli/apiclient/apierror.go
index 1cab812c..5e34adb7 100644
--- a/internal/cli/apiclient/apierror.go
+++ b/internal/cli/apiclient/apierror.go
@@ -133,6 +133,24 @@ func clearCachedTabOnFailure(statusCode int, body []byte) string {
return " The cached current tab is now cleared, so that retry will target the server's current tab.\n"
}
+// genericErrorCode is the code httpx.Error stamps on every UNCODED refusal — the largest
+// error family in the product by a wide margin. It carries no information beyond the status
+// that is already printed, so rendering it appends a bare "(error)" to the most common error
+// shape and leaves a reader wondering what it denotes.
+//
+// Declared here rather than imported: the CLI is a client of the WIRE, not of the server's
+// error helper, so it should not take a runtime dependency on it. TestTheGenericSentinelStill
+// MatchesWhatTheServerSends drives httpx.Error and asserts the wire code is this value, which
+// pins the agreement more tightly than a shared symbol would — it compares the bytes.
+const genericErrorCode = "error"
+
+// codeAddsInformation is the rule the suffix exists for, stated once. A code repeats nothing
+// the reader already has: not the message beside it, and not the generic sentinel that every
+// uncoded refusal carries.
+func codeAddsInformation(code, message string) bool {
+ return code != "" && code != message && code != genericErrorCode
+}
+
func renderAPIErrorBody(statusCode int, body []byte) string {
var errResp struct {
Error string `json:"error"`
@@ -146,7 +164,7 @@ func renderAPIErrorBody(statusCode int, body []byte) string {
var b strings.Builder
switch {
- case errResp.Error != "" && errResp.Code != "" && errResp.Code != errResp.Error:
+ case errResp.Error != "" && codeAddsInformation(errResp.Code, errResp.Error):
fmt.Fprintf(&b, "Error %d: %s (%s)\n", statusCode, errResp.Error, errResp.Code)
case errResp.Error != "":
fmt.Fprintf(&b, "Error %d: %s\n", statusCode, errResp.Error)
diff --git a/internal/cli/apiclient/apierror_test.go b/internal/cli/apiclient/apierror_test.go
index 95a1f9a9..bdbc2530 100644
--- a/internal/cli/apiclient/apierror_test.go
+++ b/internal/cli/apiclient/apierror_test.go
@@ -1,6 +1,7 @@
package apiclient
import (
+ "encoding/json"
"errors"
"fmt"
"go/ast"
@@ -387,3 +388,60 @@ func TestARejectedTokenNamesItsSource(t *testing.T) {
t.Errorf("missing-token render = %q names a provenance for a credential that was never sent", missing)
}
}
+
+// The shape 424 call sites produce, and the one no fixture drove: httpx.Error stamps the
+// generic sentinel on every uncoded refusal, so a code suffix here would append a bare
+// "(error)" to the most common error in the product — noise on the largest error family,
+// added by a change written for the coded one. This fixture is also what stops the next
+// widening of the condition from re-introducing it.
+func TestTheGenericSentinelCodeRendersExactlyAsAnUncodedError(t *testing.T) {
+ const message = `element text extract: no element matches selector "#nope": selector matched no element`
+
+ withSentinel := renderAPIErrorBody(404, []byte(`{"code":"error","error":`+quoteJSON(message)+`}`))
+ withoutCode := renderAPIErrorBody(404, []byte(`{"error":`+quoteJSON(message)+`}`))
+
+ if withSentinel != withoutCode {
+ t.Errorf("a generic-sentinel error renders differently from an uncoded one:\n with %q\n without %q", withSentinel, withoutCode)
+ }
+ if strings.Contains(withSentinel, "(error)") {
+ t.Errorf("the render appends the sentinel, which adds nothing beyond the 404 already printed: %q", withSentinel)
+ }
+ if !strings.Contains(withSentinel, message) {
+ t.Errorf("suppressing the sentinel lost the message: %q", withSentinel)
+ }
+}
+
+// The sentinel is spelled here rather than imported, so the agreement is pinned against the
+// BYTES the server actually sends rather than against a shared symbol. Drives the real
+// httpx.Error, so a rename or a change of the stamped code reds here instead of quietly
+// putting "(some_new_sentinel)" back on 424 responses.
+func TestTheGenericSentinelStillMatchesWhatTheServerSends(t *testing.T) {
+ w := httptest.NewRecorder()
+ httpx.Error(w, 404, errors.New("no element matches selector"))
+
+ var wire struct {
+ Code string `json:"code"`
+ Error string `json:"error"`
+ }
+ if err := json.Unmarshal(w.Body.Bytes(), &wire); err != nil {
+ t.Fatalf("httpx.Error did not produce the error envelope: %v (%s)", err, w.Body.String())
+ }
+ if wire.Code == "" {
+ t.Fatal("httpx.Error stamped no code, so this parity check would compare against nothing")
+ }
+ if wire.Code != genericErrorCode {
+ t.Errorf("httpx.Error now stamps %q but the renderer suppresses %q, so every uncoded refusal would render with a meaningless suffix again — update genericErrorCode", wire.Code, genericErrorCode)
+ }
+ // The other half: a code that is NOT the sentinel must still reach the reader.
+ if !codeAddsInformation("tab_not_found", "unauthorized") {
+ t.Error("a real code was classified as adding nothing; the suppression is too wide")
+ }
+}
+
+func quoteJSON(s string) string {
+ encoded, err := json.Marshal(s)
+ if err != nil {
+ panic(err)
+ }
+ return string(encoded)
+}