Skip to content

Commit

Permalink
fix: handle unexpected http responses
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Dec 5, 2024
1 parent 3e612da commit b779218
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
2 changes: 1 addition & 1 deletion gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
16 changes: 8 additions & 8 deletions glow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package main

import (
"bytes"
"strings"
"errors"
"net"
"testing"
)

Expand All @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/fs"
"net"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b779218

Please sign in to comment.