summaryrefslogtreecommitdiff
path: root/internal/http
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2024-02-06 14:43:06 +0100
committerJeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com>2024-02-19 14:15:07 +0100
commit3fd29f3e1c963196cac69fcbb9d68116f7ea80ec (patch)
tree7cb586d304167e4198166ff17dc79d33122a75e5 /internal/http
parent2337dcde60a710d2f65d3fe1107811202e34c633 (diff)
All: Prepare to get rid of go-errors/errors lib
Diffstat (limited to 'internal/http')
-rw-r--r--internal/http/http.go22
1 files changed, 10 insertions, 12 deletions
diff --git a/internal/http/http.go b/internal/http/http.go
index b5250b6..536a9c3 100644
--- a/internal/http/http.go
+++ b/internal/http/http.go
@@ -3,6 +3,7 @@ package http
import (
"context"
+ "errors"
"fmt"
"io"
"net/http"
@@ -11,7 +12,7 @@ import (
"strings"
"time"
- "github.com/go-errors/errors"
+ "github.com/eduvpn/eduvpn-common/internal/version"
)
// UserAgent is the user agent that is used for requests
@@ -53,7 +54,7 @@ func cleanPath(u *url.URL, trailing bool) string {
func EnsureValidURL(s string, trailing bool) (string, error) {
u, err := url.Parse(s)
if err != nil {
- return "", errors.WrapPrefix(err, "failed parsing url", 0)
+ return "", fmt.Errorf("failed parsing url with error: %w", err)
}
// Make sure the scheme is always https
@@ -67,11 +68,11 @@ func EnsureValidURL(s string, trailing bool) (string, error) {
func JoinURLPath(u string, p string) (string, error) {
pu, err := url.Parse(u)
if err != nil {
- return "", errors.WrapPrefix(err, "failed to parse url for joining paths", 0)
+ return "", fmt.Errorf("failed to parse url for joining paths with error: %w", err)
}
pp, err := url.Parse(p)
if err != nil {
- return "", errors.WrapPrefix(err, "failed to parse path for joining paths", 0)
+ return "", fmt.Errorf("failed to parse path for joining paths with error: %w", err)
}
fp := pu.ResolveReference(pp)
@@ -95,8 +96,7 @@ func ConstructURL(u *url.URL, params URLParameters) (string, error) {
func optionalURL(urlStr string, opts *OptionalParams) (string, error) {
u, err := url.Parse(urlStr)
if err != nil {
- return "", errors.WrapPrefix(err,
- fmt.Sprintf("failed to construct parse url '%s'", urlStr), 0)
+ return "", fmt.Errorf("failed to construct parse url '%s' with error: %w", urlStr, err)
}
// Make sure the scheme is always set to HTTPS
if u.Scheme != "https" {
@@ -190,8 +190,7 @@ func (c *Client) Do(ctx context.Context, method string, urlStr string, opts *Opt
// Create request object with the body reader generated from the optional arguments
req, err := http.NewRequestWithContext(ctx, method, urlStr, optionalBodyReader(opts))
if err != nil {
- return nil, nil, errors.WrapPrefix(err,
- fmt.Sprintf("failed HTTP request with method %s and url %s", method, urlStr), 0)
+ return nil, nil, fmt.Errorf("failed HTTP request with method: '%s', url: '%s' and error: %w", method, urlStr, err)
}
if UserAgent != "" {
req.Header.Add("User-Agent", UserAgent)
@@ -222,11 +221,10 @@ func (c *Client) Do(ctx context.Context, method string, urlStr string, opts *Opt
r := http.MaxBytesReader(nil, res.Body, c.ReadLimit)
body, err := io.ReadAll(r)
if err != nil {
- return res.Header, nil, errors.WrapPrefix(err,
- fmt.Sprintf("failed HTTP request with method: %s, url: %s and max bytes size: %v", method, urlStr, c.ReadLimit), 0)
+ return res.Header, nil, fmt.Errorf("failed HTTP request with method: '%s', url: '%s', max bytes size: '%v' and error: %w", method, urlStr, c.ReadLimit, err)
}
if res.StatusCode < 200 || res.StatusCode > 299 {
- return res.Header, body, errors.Wrap(&StatusError{URL: urlStr, Body: string(body), Status: res.StatusCode}, 0)
+ return res.Header, body, fmt.Errorf("failed HTTP request with method: '%s' due to a status error: %w", method, &StatusError{URL: urlStr, Body: string(body), Status: res.StatusCode})
}
// Return the body in bytes and signal the status error if there was one
@@ -258,7 +256,7 @@ type StatusError struct {
// Error returns the StatusError as an error string.
func (e *StatusError) Error() string {
return fmt.Sprintf(
- "failed obtaining HTTP resource: %s as it gave an unsuccessful status code: %d. Body: %s",
+ "failed obtaining HTTP resource: '%s' as it gave an unsuccessful status code: '%d'. Body: '%s'",
e.URL,
e.Status,
e.Body,