From f6175bcd1803327eda1f479db3d07648742579e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Tue, 22 Nov 2022 12:08:50 +0000 Subject: [PATCH] cmd/xurls: show parse, request, and dial errors These were being entirely ignored, which isn't right for a "fix" feature to ensure that URLs are good. --- cmd/xurls/main.go | 11 +++++++---- cmd/xurls/sequencer.go | 8 ++++---- cmd/xurls/testdata/script/fix.txtar | 9 ++++++--- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/cmd/xurls/main.go b/cmd/xurls/main.go index 88c362e..46edbac 100644 --- a/cmd/xurls/main.go +++ b/cmd/xurls/main.go @@ -97,6 +97,7 @@ func scanPath(re *regexp.Regexp, path string) error { match := line[pair[0]:pair[1]] origURL, err := url.Parse(match) if err != nil { + r.appendBroken(match, err.Error()) continue } fixed := origURL.String() @@ -128,15 +129,17 @@ func scanPath(re *regexp.Regexp, path string) error { } req, err := http.NewRequest(http.MethodHead, fixed, nil) if err != nil { + r.appendBroken(match, err.Error()) continue } req.Header.Set("User-Agent", userAgent) resp, err := client.Do(req) if err != nil { + r.appendBroken(match, err.Error()) continue } - if resp.StatusCode >= 400 { - r.appendBroken(match, resp.StatusCode) + if code := resp.StatusCode; code >= 400 { + r.appendBroken(match, fmt.Sprintf("%d %s", code, http.StatusText(code))) } resp.Body.Close() } @@ -148,7 +151,7 @@ func scanPath(re *regexp.Regexp, path string) error { atomic.AddUint32(&atomicFixedCount, 1) } } - io.WriteString(r, line) + io.WriteString(r, line) // add the fixed line to outBuf return nil }) if err := scanner.Err(); err != nil { @@ -173,7 +176,7 @@ func scanPath(re *regexp.Regexp, path string) error { var s strings.Builder fmt.Fprintf(&s, "found %d broken urls in %q:\n", len(state.brokenURLs), path) for _, broken := range state.brokenURLs { - fmt.Fprintf(&s, " * %d: %s\n", broken.statusCode, broken.url) + fmt.Fprintf(&s, " * %s - %s\n", broken.url, broken.reason) } return errors.New(s.String()) } diff --git a/cmd/xurls/sequencer.go b/cmd/xurls/sequencer.go index ae7ea00..9a5e4e1 100644 --- a/cmd/xurls/sequencer.go +++ b/cmd/xurls/sequencer.go @@ -114,8 +114,8 @@ type reporterState struct { } type brokenURL struct { - url string - statusCode int + url string + reason string } // getState blocks until any prior reporters are finished with the reporter @@ -135,9 +135,9 @@ func (r *reporter) Write(p []byte) (int, error) { return r.getState().out.Write(p) } -func (r *reporter) appendBroken(url string, statusCode int) { +func (r *reporter) appendBroken(url, reason string) { state := r.getState() - state.brokenURLs = append(state.brokenURLs, brokenURL{url, statusCode}) + state.brokenURLs = append(state.brokenURLs, brokenURL{url, reason}) } // Report emits a non-nil error to the reporter's error stream, diff --git a/cmd/xurls/testdata/script/fix.txtar b/cmd/xurls/testdata/script/fix.txtar index 90a6642..7b3874f 100644 --- a/cmd/xurls/testdata/script/fix.txtar +++ b/cmd/xurls/testdata/script/fix.txtar @@ -27,9 +27,10 @@ cmp redirects redirects.golden ! exec xurls -fix broken stdout -count=1 '^broken$' -stderr -count=1 '4 broken urls' -stderr -count=2 '/404' -stderr -count=2 '/500' +stderr -count=1 '5 broken urls' +stderr -count=2 '/404 - 404 Not Found' +stderr -count=2 '/500 - 500 Internal Server Error' +stderr -count=1 'totallydoesnotexist.localhost/ - Head .* dial tcp' cmp broken broken.golden -- nothing -- @@ -74,7 +75,9 @@ Temporary redirect codes: One redirect: ${SERVER}/redir-1 404 errors: ${SERVER}/404 ${SERVER}/404 500 errors: ${SERVER}/500 ${SERVER}/500 +Dial error: http://totallydoesnotexist.localhost/ -- broken.golden -- One redirect: ${SERVER}/plain 404 errors: ${SERVER}/404 ${SERVER}/404 500 errors: ${SERVER}/500 ${SERVER}/500 +Dial error: http://totallydoesnotexist.localhost/