Skip to content

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

feat(nav): add remediation hints for binary file navigation errors

Datab8eb93cf618d
diff --git a/internal/cli/apiclient/client.go b/internal/cli/apiclient/client.go
index d820d37e..dea9001f 100644
--- a/internal/cli/apiclient/client.go
+++ b/internal/cli/apiclient/client.go
@@ -31,7 +31,7 @@ func DoGet(client *http.Client, base, token, path string, params url.Values) map
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode >= 400 {
- fmt.Fprintf(os.Stderr, "Error %d: %s\n", resp.StatusCode, string(body))
+ handleAPIError(resp.StatusCode, body)
os.Exit(1)
}
@@ -87,7 +87,7 @@ func DoPost(client *http.Client, base, token, path string, body map[string]any)
respBody, _ := io.ReadAll(resp.Body)
if resp.StatusCode >= 400 {
- fmt.Fprintf(os.Stderr, "Error %d: %s\n", resp.StatusCode, string(respBody))
+ handleAPIError(resp.StatusCode, respBody)
os.Exit(1)
}
@@ -135,6 +135,38 @@ func setAuthHeader(req *http.Request, token string) {
}
}
+// handleAPIError parses and displays API error responses with hints
+func handleAPIError(statusCode int, body []byte) {
+ var errResp struct {
+ Error string `json:"error"`
+ Code string `json:"code"`
+ Details map[string]any `json:"details"`
+ }
+
+ if err := json.Unmarshal(body, &errResp); err != nil {
+ // Fallback to raw output if not valid JSON
+ fmt.Fprintf(os.Stderr, "Error %d: %s\n", statusCode, string(body))
+ return
+ }
+
+ // Print main error
+ if errResp.Error != "" {
+ fmt.Fprintf(os.Stderr, "Error %d: %s\n", statusCode, errResp.Error)
+ } else {
+ fmt.Fprintf(os.Stderr, "Error %d: %s\n", statusCode, string(body))
+ }
+
+ // Print hint and remedy if present
+ if errResp.Details != nil {
+ if hint, ok := errResp.Details["hint"].(string); ok && hint != "" {
+ fmt.Fprintf(os.Stderr, "\n💡 %s\n", hint)
+ }
+ if remedy, ok := errResp.Details["remedy"].(string); ok && remedy != "" {
+ fmt.Fprintf(os.Stderr, " Remedy: %s\n", remedy)
+ }
+ }
+}
+
func fatal(format string, args ...any) {
fmt.Fprintf(os.Stderr, format+"\n", args...)
os.Exit(1)
diff --git a/internal/handlers/navigation.go b/internal/handlers/navigation.go
index 19f77224..973fd60f 100644
--- a/internal/handlers/navigation.go
+++ b/internal/handlers/navigation.go
@@ -210,7 +210,7 @@ func (h *Handlers) HandleNavigate(w http.ResponseWriter, r *http.Request) {
} else if strings.Contains(errMsg, "invalid URL") || strings.Contains(errMsg, "Cannot navigate to invalid URL") || strings.Contains(errMsg, "ERR_INVALID_URL") {
code = 400
}
- httpx.Error(w, code, fmt.Errorf("navigate: %w", err))
+ navigateErrorWithHint(w, code, err, req.URL)
return
}
@@ -219,13 +219,13 @@ func (h *Handlers) HandleNavigate(w http.ResponseWriter, r *http.Request) {
return
}
- var url string
- _ = chromedp.Run(tCtx, chromedp.Location(&url))
+ var navURL string
+ _ = chromedp.Run(tCtx, chromedp.Location(&navURL))
title, _ := bridge.WaitForTitle(tCtx, titleWait)
h.recordResolvedTab(r, newTabID)
- h.recordResolvedURL(r, url)
+ h.recordResolvedURL(r, navURL)
- httpx.JSON(w, 200, map[string]any{"tabId": newTabID, "url": url, "title": title})
+ httpx.JSON(w, 200, map[string]any{"tabId": newTabID, "url": navURL, "title": title})
return
}
@@ -264,7 +264,7 @@ func (h *Handlers) HandleNavigate(w http.ResponseWriter, r *http.Request) {
} else if strings.Contains(errMsg, "invalid URL") || strings.Contains(errMsg, "Cannot navigate to invalid URL") || strings.Contains(errMsg, "ERR_INVALID_URL") {
code = 400
}
- httpx.Error(w, code, fmt.Errorf("navigate: %w", err))
+ navigateErrorWithHint(w, code, err, req.URL)
return
}
@@ -275,12 +275,12 @@ func (h *Handlers) HandleNavigate(w http.ResponseWriter, r *http.Request) {
return
}
- var url string
- _ = chromedp.Run(tCtx, chromedp.Location(&url))
+ var navURL string
+ _ = chromedp.Run(tCtx, chromedp.Location(&navURL))
title, _ := bridge.WaitForTitle(tCtx, titleWait)
- h.recordResolvedURL(r, url)
+ h.recordResolvedURL(r, navURL)
- httpx.JSON(w, 200, map[string]any{"tabId": resolvedTabID, "url": url, "title": title})
+ httpx.JSON(w, 200, map[string]any{"tabId": resolvedTabID, "url": navURL, "title": title})
}
// HandleTabNavigate navigates an existing tab identified by path ID.
@@ -365,7 +365,7 @@ func (h *Handlers) HandleTab(w http.ResponseWriter, r *http.Request) {
if errors.Is(err, bridge.ErrTooManyRedirects) {
code = 422
}
- httpx.Error(w, code, fmt.Errorf("navigate: %w", err))
+ navigateErrorWithHint(w, code, err, req.URL)
return
}
}
@@ -497,6 +497,39 @@ func (h *Handlers) HandleForward(w http.ResponseWriter, r *http.Request) {
httpx.JSON(w, 200, map[string]any{"tabId": resolvedID, "url": curURL})
}
+// binaryFileExtensions are extensions that Chrome cannot render and will abort on
+var binaryFileExtensions = []string{".gz", ".zip", ".tar", ".rar", ".7z", ".bz2", ".xz", ".pdf", ".exe", ".bin", ".dmg", ".iso"}
+
+// isNavigateAbortedOnBinary checks if a navigation error is ERR_ABORTED on a likely binary URL
+func isNavigateAbortedOnBinary(err error, url string) bool {
+ if err == nil {
+ return false
+ }
+ errMsg := err.Error()
+ if !strings.Contains(errMsg, "ERR_ABORTED") {
+ return false
+ }
+ lowerURL := strings.ToLower(url)
+ for _, ext := range binaryFileExtensions {
+ if strings.HasSuffix(lowerURL, ext) || strings.Contains(lowerURL, ext+"?") {
+ return true
+ }
+ }
+ return false
+}
+
+// navigateErrorWithHint returns an error response with remedy hints for binary content
+func navigateErrorWithHint(w http.ResponseWriter, code int, err error, url string) {
+ if isNavigateAbortedOnBinary(err, url) {
+ httpx.ErrorCode(w, 502, "nav_binary_aborted", fmt.Sprintf("navigate: %s", err.Error()), false, map[string]any{
+ "remedy": "download",
+ "hint": fmt.Sprintf("Chrome cannot render binary/compressed files. Use: pinchtab download %q", url),
+ })
+ return
+ }
+ httpx.Error(w, code, fmt.Errorf("navigate: %w", err))
+}
+
// HandleReload reloads the current (or specified) tab.
func (h *Handlers) HandleReload(w http.ResponseWriter, r *http.Request) {
tabID := r.URL.Query().Get("tabId")
diff --git a/internal/handlers/navigation_test.go b/internal/handlers/navigation_test.go
index 1b8a4f94..1e5997b7 100644
--- a/internal/handlers/navigation_test.go
+++ b/internal/handlers/navigation_test.go
@@ -3,6 +3,7 @@ package handlers
import (
"bytes"
"context"
+ "fmt"
"net"
"net/http/httptest"
"strings"
@@ -285,3 +286,31 @@ func TestHandleTab_CloseMissingID(t *testing.T) {
t.Errorf("expected 400, got %d", w.Code)
}
}
+
+func TestIsNavigateAbortedOnBinary(t *testing.T) {
+ tests := []struct {
+ name string
+ err error
+ url string
+ expect bool
+ }{
+ {"gz file with ERR_ABORTED", fmt.Errorf("net::ERR_ABORTED"), "https://example.com/file.gz", true},
+ {"xml.gz file with ERR_ABORTED", fmt.Errorf("net::ERR_ABORTED"), "https://example.com/sitemap.xml.gz", true},
+ {"zip file with ERR_ABORTED", fmt.Errorf("net::ERR_ABORTED"), "https://example.com/archive.zip", true},
+ {"pdf file with ERR_ABORTED", fmt.Errorf("net::ERR_ABORTED"), "https://example.com/doc.pdf", true},
+ {"gz with query param", fmt.Errorf("net::ERR_ABORTED"), "https://example.com/file.gz?token=abc", true},
+ {"html file with ERR_ABORTED", fmt.Errorf("net::ERR_ABORTED"), "https://example.com/page.html", false},
+ {"gz file without ERR_ABORTED", fmt.Errorf("net::ERR_CONNECTION_REFUSED"), "https://example.com/file.gz", false},
+ {"html file different error", fmt.Errorf("timeout"), "https://example.com/page.html", false},
+ {"nil error", nil, "https://example.com/file.gz", false},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ result := isNavigateAbortedOnBinary(tt.err, tt.url)
+ if result != tt.expect {
+ t.Errorf("isNavigateAbortedOnBinary(%v, %q) = %v, want %v", tt.err, tt.url, result, tt.expect)
+ }
+ })
+ }
+}