Skip to content

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

fix: explain route 404s from older instances instead of bare error

Luigi Agosti0894a587f047
diff --git a/internal/cli/apiclient/apierror.go b/internal/cli/apiclient/apierror.go
index 6d7b7a46..290e68dc 100644
--- a/internal/cli/apiclient/apierror.go
+++ b/internal/cli/apiclient/apierror.go
@@ -4,7 +4,9 @@ import (
"encoding/json"
"fmt"
"net/http"
+ "net/url"
"os"
+ "strings"
)
// mustRequest is doRequest with the common fatal-on-transport-error policy.
@@ -16,19 +18,54 @@ func mustRequest(client *http.Client, token string, r request) (int, []byte) {
return status, body
}
-func exitOnAPIError(status int, body []byte) {
+func exitOnAPIError(r request, status int, body []byte) {
if status >= 400 {
- handleAPIError(status, body)
+ fmt.Fprint(os.Stderr, renderAPIError(r, status, body))
os.Exit(1)
}
}
+// renderAPIError formats an HTTP error for the terminal. A route-level 404
+// (the mux's plain-text "404 page not found", as opposed to a JSON
+// application error) means the running instance predates the requested
+// endpoint — say so explicitly instead of a bare 404 that reads as if the
+// user's target site failed.
+func renderAPIError(r request, statusCode int, body []byte) string {
+ if isRouteNotFound(statusCode, body) {
+ return routeNotFoundMessage(r.method, r.url)
+ }
+ return renderAPIErrorBody(statusCode, body)
+}
+
+// isRouteNotFound reports whether a 404 came from the HTTP mux (no such
+// route) rather than an application handler, which always answers in JSON.
+func isRouteNotFound(statusCode int, body []byte) bool {
+ if statusCode != http.StatusNotFound {
+ return false
+ }
+ var probe any
+ return json.Unmarshal(body, &probe) != nil
+}
+
+func routeNotFoundMessage(method, rawURL string) string {
+ addr, path := rawURL, ""
+ if u, err := url.Parse(rawURL); err == nil && u.Host != "" {
+ addr, path = u.Host, u.Path
+ }
+ return fmt.Sprintf("Error: the running pinchtab instance at %s does not support %s %s (it is likely an older version). Restart it with the current binary and retry.\n",
+ addr, method, path)
+}
+
func ExitWithAPIError(statusCode int, body []byte) {
handleAPIError(statusCode, body)
os.Exit(1)
}
func handleAPIError(statusCode int, body []byte) {
+ fmt.Fprint(os.Stderr, renderAPIErrorBody(statusCode, body))
+}
+
+func renderAPIErrorBody(statusCode int, body []byte) string {
var errResp struct {
Error string `json:"error"`
Code string `json:"code"`
@@ -36,24 +73,25 @@ func handleAPIError(statusCode int, body []byte) {
}
if err := json.Unmarshal(body, &errResp); err != nil {
- fmt.Fprintf(os.Stderr, "Error %d: %s\n", statusCode, string(body))
- return
+ return fmt.Sprintf("Error %d: %s\n", statusCode, string(body))
}
+ var b strings.Builder
if errResp.Error != "" {
- fmt.Fprintf(os.Stderr, "Error %d: %s\n", statusCode, errResp.Error)
+ fmt.Fprintf(&b, "Error %d: %s\n", statusCode, errResp.Error)
} else {
- fmt.Fprintf(os.Stderr, "Error %d: %s\n", statusCode, string(body))
+ fmt.Fprintf(&b, "Error %d: %s\n", statusCode, string(body))
}
if errResp.Details != nil {
if hint, ok := errResp.Details["hint"].(string); ok && hint != "" {
- fmt.Fprintf(os.Stderr, "\n💡 %s\n", hint)
+ fmt.Fprintf(&b, "\n💡 %s\n", hint)
}
if remedy, ok := errResp.Details["remedy"].(string); ok && remedy != "" {
- fmt.Fprintf(os.Stderr, " Remedy: %s\n", remedy)
+ fmt.Fprintf(&b, " Remedy: %s\n", remedy)
}
}
+ return b.String()
}
func fatal(format string, args ...any) {
diff --git a/internal/cli/apiclient/apierror_test.go b/internal/cli/apiclient/apierror_test.go
new file mode 100644
index 00000000..69836789
--- /dev/null
+++ b/internal/cli/apiclient/apierror_test.go
@@ -0,0 +1,78 @@
+package apiclient
+
+import (
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+)
+
+func TestIsRouteNotFound(t *testing.T) {
+ cases := []struct {
+ name string
+ status int
+ body string
+ want bool
+ }{
+ {"mux plain-text 404", 404, "404 page not found\n", true},
+ {"application JSON 404", 404, `{"error":"tab not found"}`, false},
+ {"JSON body without error field", 404, `{"status":"missing"}`, false},
+ {"plain-text 500", 500, "boom", false},
+ {"ok", 200, "404 page not found", false},
+ }
+ for _, tc := range cases {
+ if got := isRouteNotFound(tc.status, []byte(tc.body)); got != tc.want {
+ t.Errorf("%s: isRouteNotFound = %v, want %v", tc.name, got, tc.want)
+ }
+ }
+}
+
+// An older instance without the /audit route answers with the mux's
+// plain-text 404; the rendered error must blame the instance, not the
+// audited target site.
+func TestRenderAPIErrorForFaked404Instance(t *testing.T) {
+ srv := httptest.NewServer(http.NotFoundHandler())
+ defer srv.Close()
+
+ r := request{method: "POST", url: srv.URL + "/audit", body: map[string]any{"urls": []string{"https://example.com"}}}
+ resp, err := http.Post(r.url, "application/json", strings.NewReader("{}"))
+ if err != nil {
+ t.Fatalf("post: %v", err)
+ }
+ body, _ := io.ReadAll(resp.Body)
+ _ = resp.Body.Close()
+ if resp.StatusCode != http.StatusNotFound {
+ t.Fatalf("status = %d, want 404", resp.StatusCode)
+ }
+
+ msg := renderAPIError(r, resp.StatusCode, body)
+ host := strings.TrimPrefix(srv.URL, "http://")
+ for _, want := range []string{host, "POST /audit", "older version", "Restart it with the current binary"} {
+ if !strings.Contains(msg, want) {
+ t.Errorf("message missing %q:\n%s", want, msg)
+ }
+ }
+ if strings.Contains(msg, "example.com") {
+ t.Errorf("message must not implicate the target site:\n%s", msg)
+ }
+ if strings.Contains(msg, "404 page not found") {
+ t.Errorf("message should replace the bare mux text:\n%s", msg)
+ }
+}
+
+func TestRenderAPIErrorKeepsApplicationErrors(t *testing.T) {
+ r := request{method: "GET", url: "http://127.0.0.1:9867/console"}
+ msg := renderAPIError(r, 404, []byte(`{"error":"tab not found: nonexistent_xyz"}`))
+ if !strings.Contains(msg, "Error 404: tab not found: nonexistent_xyz") {
+ t.Errorf("application 404 rendering changed:\n%s", msg)
+ }
+ if strings.Contains(msg, "older version") {
+ t.Errorf("application 404 must not be treated as a missing route:\n%s", msg)
+ }
+
+ msg = renderAPIError(r, 403, []byte(`{"error":"blocked","details":{"hint":"allow the domain"}}`))
+ if !strings.Contains(msg, "Error 403: blocked") || !strings.Contains(msg, "allow the domain") {
+ t.Errorf("hint rendering changed:\n%s", msg)
+ }
+}
diff --git a/internal/cli/apiclient/client.go b/internal/cli/apiclient/client.go
index 48d8c370..56d3b319 100644
--- a/internal/cli/apiclient/client.go
+++ b/internal/cli/apiclient/client.go
@@ -13,7 +13,7 @@ import (
// exit-on-HTTP-error policy, then pretty-prints and decodes the body.
func doAndRender(client *http.Client, token string, r request) map[string]any {
status, body := mustRequest(client, token, r)
- exitOnAPIError(status, body)
+ exitOnAPIError(r, status, body)
return printAndDecode(body)
}
@@ -22,11 +22,9 @@ func DoGet(client *http.Client, base, token, path string, params url.Values) map
}
func DoGetRaw(client *http.Client, base, token, path string, params url.Values) []byte {
- status, body := mustRequest(client, token, request{method: "GET", url: buildURL(base, path, params)})
- if status >= 400 {
- fmt.Fprintf(os.Stderr, "Error %d: %s\n", status, string(body))
- os.Exit(1)
- }
+ r := request{method: "GET", url: buildURL(base, path, params)}
+ status, body := mustRequest(client, token, r)
+ exitOnAPIError(r, status, body)
return body
}
@@ -60,10 +58,7 @@ func DoPostQuiet(client *http.Client, base, token, path string, body map[string]
// Exits on HTTP errors.
func DoPostRaw(client *http.Client, base, token, path string, body map[string]any) []byte {
statusCode, respBody, _ := doPostQuietWithStatus(client, base, token, path, body, nil)
- if statusCode >= 400 {
- handleAPIError(statusCode, respBody)
- os.Exit(1)
- }
+ exitOnAPIError(request{method: "POST", url: base + path, body: body}, statusCode, respBody)
return respBody
}
@@ -74,10 +69,7 @@ func DoPostQuietWithStatus(client *http.Client, base, token, path string, body m
// DoPostQuietWithHeaders is like DoPostQuiet but allows custom headers.
func DoPostQuietWithHeaders(client *http.Client, base, token, path string, body map[string]any, headers map[string]string) map[string]any {
statusCode, respBody, result := doPostQuietWithStatus(client, base, token, path, body, headers)
- if statusCode >= 400 {
- handleAPIError(statusCode, respBody)
- os.Exit(1)
- }
+ exitOnAPIError(request{method: "POST", url: base + path, body: body, headers: headers}, statusCode, respBody)
return result
}