carlos / platform Public

console: legal fetch reports a cancelled fetch as cancelled

context.Canceled read as "connect failed"; it is now its own category.
The request is built from the already-parsed URL, so the unreachable
"invalid url" branch after a successful parse is gone.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 62a6464bf7e0fcc80ec962aac8524ef21b4fcc3e parent 9553905
2 files changed, +51 −5
  • internal/console/legalfetch.go +6 −5
  • internal/console/legalfetch_test.go +45 −0

Intent

diff --git a/internal/console/legalfetch.go b/internal/console/legalfetch.go
index c4c43825d..93676870a 100644
--- a/internal/console/legalfetch.go
+++ b/internal/console/legalfetch.go
@@ -47,11 +47,9 @@ func fetchLegalText(ctx context.Context, c *http.Client, rawURL string) ([]byte,
if err != nil || u.Scheme != "https" || u.Opaque != "" || u.Host == "" {
return nil, errors.New("not https")
}
- req, err := http.NewRequestWithContext(ctx, http.MethodGet, "", nil)
- if err != nil {
- return nil, errors.New("invalid url")
- }
- req.URL = u
+ // Built from the parsed URL, so nothing is parsed twice and nothing past
+ // the check above can fail. A nil ctx panics in WithContext: a caller's bug.
+ req := (&http.Request{Method: http.MethodGet, URL: u, Header: http.Header{}}).WithContext(ctx)
resp, err := c.Do(req)
if err != nil {
return nil, legalFetchErr(err)
@@ -76,6 +74,9 @@ func legalFetchErr(err error) error {
switch {
case errors.Is(err, errRefusedAddr):
return errors.New("refused address")
+ case errors.Is(err, context.Canceled):
+ // The operator's request went away; nothing was wrong with the site.
+ return errors.New("cancelled")
case errors.Is(err, context.DeadlineExceeded), errors.As(err, &netErr) && netErr.Timeout():
return errors.New("timeout")
}
diff --git a/internal/console/legalfetch_test.go b/internal/console/legalfetch_test.go
index f4a092b4d..a3e435e97 100644
--- a/internal/console/legalfetch_test.go
+++ b/internal/console/legalfetch_test.go
@@ -4,8 +4,10 @@ import (
"context"
"crypto/tls"
"crypto/x509"
+ "errors"
"net/http"
"net/http/httptest"
+ "net/url"
"strings"
"testing"
"time"
@@ -157,3 +159,46 @@ func TestCanonicalAgreementRefuses(t *testing.T) {
}
}
}
+
+// A fetch cancelled by its caller (the operator's request went away) is its
+// own category, not a connection failure.
+func TestFetchLegalTextCancelled(t *testing.T) {
+ srv, _ := countingServer(t, func(w http.ResponseWriter, r *http.Request) { <-r.Context().Done() })
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+ _, err := fetchLegalText(ctx, legalTestClient(srv), srv.URL)
+ if err == nil || err.Error() != "cancelled" {
+ t.Fatalf("err = %v, want cancelled", err)
+ }
+
+ // Cancelled mid-request, once the server has the request.
+ arrived := make(chan struct{})
+ srv2, _ := countingServer(t, func(w http.ResponseWriter, r *http.Request) {
+ close(arrived)
+ <-r.Context().Done()
+ })
+ ctx2, cancel2 := context.WithCancel(context.Background())
+ defer cancel2()
+ go func() { <-arrived; cancel2() }()
+ _, err = fetchLegalText(ctx2, legalTestClient(srv2), srv2.URL)
+ if err == nil || err.Error() != "cancelled" {
+ t.Fatalf("mid-request err = %v, want cancelled", err)
+ }
+}
+
+// legalFetchErr's categories, for the errors a client returns.
+func TestFetchLegalErrCategories(t *testing.T) {
+ for _, tc := range []struct {
+ err error
+ want string
+ }{
+ {&url.Error{Op: "Get", URL: "https://x", Err: context.Canceled}, "cancelled"},
+ {&url.Error{Op: "Get", URL: "https://x", Err: context.DeadlineExceeded}, "timeout"},
+ {&url.Error{Op: "Get", URL: "https://x", Err: errRefusedAddr}, "refused address"},
+ {&url.Error{Op: "Get", URL: "https://x", Err: errors.New("connection reset")}, "connect failed"},
+ } {
+ if got := legalFetchErr(tc.err).Error(); got != tc.want {
+ t.Errorf("%v: %q, want %q", tc.err, got, tc.want)
+ }
+ }
+}