Browse Source

Curl better errors

Volodymyr Tkach 2 years ago
parent
commit
49b21fb916
1 changed files with 17 additions and 6 deletions
  1. 17 6
      utils/http/helpers/curl.go

+ 17 - 6
utils/http/helpers/curl.go

@@ -8,12 +8,23 @@ import (
 	"time"
 )
 
-type CurlGetStatusError struct {
+var (
+	ErrCurlGetStatus = errCurlGetStatus(0, 0)
+)
+
+func errCurlGetStatus(e, r int) error {
+	return &curlGetStatusError{
+		expected: e,
+		received: r,
+	}
+}
+
+type curlGetStatusError struct {
 	expected int
 	received int
 }
 
-func (c *CurlGetStatusError) Error() string {
+func (c *curlGetStatusError) Error() string {
 	return fmt.Sprintf("CurlGet: expected %d, received %d", c.received, c.expected)
 }
 
@@ -37,10 +48,10 @@ func CurlGet(ctx context.Context, url string, timeout time.Duration) ([]byte, er
 	defer resp.Body.Close()
 
 	if resp.StatusCode != http.StatusOK {
-		return b, &CurlGetStatusError{
-			expected: http.StatusOK,
-			received: resp.StatusCode,
-		}
+		return b, errCurlGetStatus(
+			http.StatusOK,
+			resp.StatusCode,
+		)
 	}
 
 	b, err = io.ReadAll(resp.Body)