Skip to content

Commit

Permalink
cmd/xurls: show parse, request, and dial errors
Browse files Browse the repository at this point in the history
These were being entirely ignored, which isn't right for a "fix" feature
to ensure that URLs are good.
  • Loading branch information
mvdan committed Nov 22, 2022
1 parent 94f9df8 commit f6175bc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
11 changes: 7 additions & 4 deletions cmd/xurls/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
}
Expand All @@ -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 {
Expand All @@ -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())
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/xurls/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions cmd/xurls/testdata/script/fix.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -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 --
Expand Down Expand Up @@ -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/

0 comments on commit f6175bc

Please sign in to comment.