From b779218c5527ca6228abdd90f8a6900dad686233 Mon Sep 17 00:00:00 2001 From: bashbunni Date: Wed, 4 Dec 2024 16:57:19 -0800 Subject: [PATCH] fix: handle unexpected http responses --- github.go | 2 +- gitlab.go | 2 +- glow_test.go | 16 ++++++++-------- main.go | 7 +++++++ 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/github.go b/github.go index 6b169fa6..6b94bb9e 100644 --- a/github.go +++ b/github.go @@ -53,5 +53,5 @@ func findGitHubREADME(u *url.URL) (*source, error) { } } - return nil, errors.New("can't find README in GitHub repository") + return nil, errors.New(fmt.Sprintf("Can't find README in GitHub repository. Response status: %d %s", res.StatusCode, res.Status)) } diff --git a/gitlab.go b/gitlab.go index 05e12392..a69c59ae 100644 --- a/gitlab.go +++ b/gitlab.go @@ -57,5 +57,5 @@ func findGitLabREADME(u *url.URL) (*source, error) { } } - return nil, errors.New("can't find README in GitLab repository") + return nil, errors.New(fmt.Sprintf("Can't find README in GitLab repository. Response status: %d %s", res.StatusCode, res.Status)) } diff --git a/glow_test.go b/glow_test.go index 263177a2..08d90c7d 100644 --- a/glow_test.go +++ b/glow_test.go @@ -2,7 +2,8 @@ package main import ( "bytes" - "strings" + "errors" + "net" "testing" ) @@ -17,16 +18,15 @@ func TestGlowSources(t *testing.T) { for _, v := range tt { t.Run(v, func(t *testing.T) { - // Start by checking for network issues. - _, nerr := readmeURL(v) - if nerr != nil && strings.Contains(nerr.Error(), "no such host") { - t.Logf("Error during execution (args: %s):\n%v", v, nerr) - t.Skip("Test uses network. Are you connected to the Internet?") - } - buf := &bytes.Buffer{} err := executeArg(rootCmd, v, buf) if err != nil { + // Check for network issues. + var netErr *net.DNSError + if errors.As(err, &netErr) { + t.Logf("Error during execution (args: %s): %v", v, err) + t.Skip("Test uses network. Are you connected to the Internet?") + } t.Errorf("Error during execution (args: %s): %v", v, err) } if buf.Len() == 0 { diff --git a/main.go b/main.go index fb3ce57e..f1075251 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "io/fs" + "net" "net/http" "net/url" "os" @@ -76,6 +77,12 @@ func sourceFromArg(arg string) (*source, error) { // a GitHub or GitLab URL (even without the protocol): src, err := readmeURL(arg) + // Return error if it's a network issue. + var dnsErr *net.DNSError + if errors.As(err, &dnsErr) { + return src, err + } + if src != nil && err == nil { // if there's an error, try next methods... return src, nil