Skip to content

Commit cbc6aed

Browse files
committed
Refactor error handling
Change error handling so it's more consistent (and easier to debug) with the rest of the project.
1 parent a6fe379 commit cbc6aed

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

upload/uploader.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
"io/ioutil"
1111
"log"
1212
"net/http"
13+
"net/url"
1314
"os"
1415
"strings"
1516
"time"
16-
"net/url"
1717

1818
"github.com/Sirupsen/logrus"
1919
"github.com/codeclimate/test-reporter/formatters"
@@ -88,25 +88,27 @@ func (u Uploader) Upload() error {
8888
return errors.WithStack(err)
8989
}
9090

91-
postBatchURL := u.TransformPostBatchURL(batchLinks.Links.PostBatch)
91+
postBatchURL, err := u.TransformPostBatchURL(batchLinks.Links.PostBatch)
92+
if err != nil {
93+
return errors.WithStack(err)
94+
}
9295

9396
return u.SendBatches(testReport, postBatchURL)
9497
}
9598

96-
func (u Uploader) TransformPostBatchURL(rawUrl string) string {
99+
func (u Uploader) TransformPostBatchURL(rawUrl string) (string, error) {
97100
parsed, err := url.Parse(rawUrl)
98-
99101
if err != nil {
100-
return rawUrl
102+
return "", err
101103
}
102104

103-
if (u.Insecure) {
105+
if u.Insecure {
104106
parsed.Scheme = "http"
105107
} else {
106108
parsed.Scheme = "https"
107109
}
108110

109-
return parsed.String()
111+
return parsed.String(), nil
110112
}
111113

112114
func (u Uploader) SendBatches(rep *TestReport, url string) error {

upload/uploader_test.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ func Test_TransformPostBatchURL_Secure(t *testing.T) {
1313
Insecure: false,
1414
}
1515

16-
url := "https://example.com/"
16+
rawURL := "https://example.com/"
17+
actualURL, err := uploader.TransformPostBatchURL(rawURL)
1718

18-
r.Equal("https://example.com/", uploader.TransformPostBatchURL(url))
19+
r.Equal("https://example.com/", actualURL)
20+
r.Nil(err)
1921
}
2022
func Test_TransformPostBatchURL_Insecure_Success(t *testing.T) {
2123
r := require.New(t)
@@ -24,9 +26,11 @@ func Test_TransformPostBatchURL_Insecure_Success(t *testing.T) {
2426
Insecure: true,
2527
}
2628

27-
url := "https://example.com/"
29+
rawURL := "https://example.com/"
30+
actualURL, err := uploader.TransformPostBatchURL(rawURL)
2831

29-
r.Equal("http://example.com/", uploader.TransformPostBatchURL(url))
32+
r.Equal("http://example.com/", actualURL)
33+
r.Nil(err)
3034
}
3135

3236
func Test_TransformPostBatchURL_Insecure_Error(t *testing.T) {
@@ -36,7 +40,9 @@ func Test_TransformPostBatchURL_Insecure_Error(t *testing.T) {
3640
Insecure: true,
3741
}
3842

39-
url := "://example.com/"
43+
rawURL := "://example.com/"
44+
actualURL, err := uploader.TransformPostBatchURL(rawURL)
4045

41-
r.Equal(url, uploader.TransformPostBatchURL(url))
46+
r.Equal("", actualURL)
47+
r.Equal("parse ://example.com/: missing protocol scheme", err.Error())
4248
}

0 commit comments

Comments
 (0)