Skip to content

Commit 0d4c2db

Browse files
committed
Error handling when requesting to package registry
1 parent e92f4d5 commit 0d4c2db

File tree

4 files changed

+55
-16
lines changed

4 files changed

+55
-16
lines changed

cmd/bundler.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ type BundlerDoctor struct {
1313
}
1414

1515
func NewBundlerDoctor() *BundlerDoctor {
16-
t := http.DefaultTransport.(*http.Transport).Clone()
17-
t.MaxIdleConnsPerHost = -1
18-
client := &http.Client{Transport: t}
16+
client := &http.Client{}
1917
return &BundlerDoctor{HTTPClient: *client}
2018
}
2119

cmd/nodejs.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"io"
78
"net/http"
@@ -22,13 +23,26 @@ type Nodejs struct {
2223

2324
func (n *Nodejs) fetchURLFromRegistry(client http.Client) (string, error) {
2425
url := fmt.Sprintf(NODEJS_REGISTRY_API, n.name)
25-
req, _ := http.NewRequest(http.MethodGet, url, nil)
26-
resp, _ := client.Do(req)
27-
body, _ := io.ReadAll(resp.Body)
26+
req, err := http.NewRequest(http.MethodGet, url, nil)
27+
if err != nil {
28+
return "", err
29+
}
30+
31+
resp, err := client.Do(req)
32+
if err != nil {
33+
return "", nil
34+
}
35+
2836
defer resp.Body.Close()
37+
if resp.StatusCode < 200 || 299 < resp.StatusCode {
38+
m := fmt.Sprintf("Got status code: %d from %s", resp.StatusCode, RUBY_GEMS_REGISTRY_API)
39+
return "", errors.New(m)
40+
}
41+
42+
body, _ := io.ReadAll(resp.Body)
2943

3044
var NodejsRegistryResponse NodejsRegistryResponse
31-
err := json.Unmarshal(body, &NodejsRegistryResponse)
45+
err = json.Unmarshal(body, &NodejsRegistryResponse)
3246
if err != nil {
3347
return "", nil
3448
}

cmd/pypi.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"io"
78
"net/http"
@@ -25,13 +26,26 @@ type Pypi struct {
2526

2627
func (p *Pypi) fetchURLFromRepository(client http.Client) (string, error) {
2728
url := fmt.Sprintf(PYPI_REGISTRY_API, p.name)
28-
req, _ := http.NewRequest(http.MethodGet, url, nil)
29-
resp, _ := client.Do(req)
30-
body, _ := io.ReadAll(resp.Body)
29+
req, err := http.NewRequest(http.MethodGet, url, nil)
30+
if err != nil {
31+
return "", err
32+
}
33+
34+
resp, err := client.Do(req)
35+
if err != nil {
36+
return "", err
37+
}
38+
3139
defer resp.Body.Close()
40+
if resp.StatusCode < 200 || 299 < resp.StatusCode {
41+
m := fmt.Sprintf("Got status code: %d from %s", resp.StatusCode, RUBY_GEMS_REGISTRY_API)
42+
return "", errors.New(m)
43+
}
44+
45+
body, _ := io.ReadAll(resp.Body)
3246

3347
var PypiRegistryResponse PypiRegistryResponse
34-
err := json.Unmarshal(body, &PypiRegistryResponse)
48+
err = json.Unmarshal(body, &PypiRegistryResponse)
3549
if err != nil {
3650
return "", nil
3751
}

cmd/ruby_gems.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,28 @@ type RubyGems struct {
2323

2424
func (g *RubyGems) fetchURLFromRegistry(client http.Client) (string, error) {
2525
url := fmt.Sprintf(RUBY_GEMS_REGISTRY_API, g.name)
26-
req, _ := http.NewRequest(http.MethodGet, url, nil)
27-
resp, _ := client.Do(req)
28-
body, _ := io.ReadAll(resp.Body)
26+
req, err := http.NewRequest(http.MethodGet, url, nil)
27+
if err != nil {
28+
return "", err
29+
}
30+
31+
resp, err := client.Do(req)
32+
if err != nil {
33+
return "", err
34+
}
35+
2936
defer resp.Body.Close()
37+
if resp.StatusCode < 200 || 299 < resp.StatusCode {
38+
m := fmt.Sprintf("Got status code: %d from %s", resp.StatusCode, RUBY_GEMS_REGISTRY_API)
39+
return "", errors.New(m)
40+
}
41+
42+
body, _ := io.ReadAll(resp.Body)
3043

3144
var Gem RubyGemsRegistryResponse
32-
err := json.Unmarshal(body, &Gem)
45+
err = json.Unmarshal(body, &Gem)
3346
if err != nil {
34-
return "", errors.New("error: Unknown response")
47+
return "", err
3548
}
3649

3750
if Gem.SourceCodeUri != "" {

0 commit comments

Comments
 (0)