summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2024-02-06 14:09:45 +0100
committerJeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com>2024-02-19 14:15:07 +0100
commit2de1cd18c05a779b27f68adfb9d60a1277bb6d55 (patch)
tree4a75d91f430a4b0826ec0f12a503028deaf4f186
parent6f7e32b6d9dcf4732dd0acbd89be77b42805a5ca (diff)
i18nerr + HTTP: Properly convert timeout errors
-rw-r--r--i18nerr/i18nerr.go22
-rw-r--r--internal/http/http.go21
2 files changed, 31 insertions, 12 deletions
diff --git a/i18nerr/i18nerr.go b/i18nerr/i18nerr.go
index d513fdd..2dea504 100644
--- a/i18nerr/i18nerr.go
+++ b/i18nerr/i18nerr.go
@@ -7,15 +7,17 @@ import (
"fmt"
"sync"
+ "github.com/eduvpn/eduvpn-common/internal/http"
"github.com/eduvpn/eduvpn-common/internal/log"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
-var printers sync.Map
-var once sync.Once
-
+var (
+ printers sync.Map
+ once sync.Once
+)
// TranslatedInner defines errors that are used as inner causes but are still translated because they can happen frequently
func TranslatedInner(inner error) (string, bool) {
@@ -24,9 +26,10 @@ func TranslatedInner(inner error) (string, bool) {
unwrapped = errors.Unwrap(unwrapped)
}
+ var tErr *http.TimeoutError
switch {
- case errors.Is(inner, context.DeadlineExceeded):
- return printerOrNew(language.English).Sprintf("timeout reached"), false
+ case errors.As(inner, &tErr):
+ return printerOrNew(language.English).Sprintf("timeout reached for URL: '%s' and HTTP method: '%s'", tErr.URL, tErr.Method), false
case errors.Is(inner, context.Canceled):
return unwrapped.Error(), true
}
@@ -37,10 +40,10 @@ func TranslatedInner(inner error) (string, bool) {
// This translation key is later used to lookup translation
// The inner error always consists of the translation key and some formatting
type Error struct {
- key message.Reference
- args []interface{}
+ key message.Reference
+ args []interface{}
wrapped *Error
- Misc bool
+ Misc bool
}
func (e *Error) translated(t language.Tag) string {
@@ -81,7 +84,6 @@ func (e *Error) Translations() map[string]string {
return translations
}
-
// Unwrap returns the unwrapped error
// it does this by unwrapping the inner error
func (e *Error) Unwrap() error {
@@ -142,7 +144,7 @@ func NewInternal(disp string) *Error {
}
// NewInternalf creates an internal localised error from a display string and arguments
-func NewInternalf(disp string, args...interface{}) *Error {
+func NewInternalf(disp string, args ...interface{}) *Error {
return NewInternal(fmt.Sprintf(disp, args...))
}
diff --git a/internal/http/http.go b/internal/http/http.go
index 58e8f05..262309a 100644
--- a/internal/http/http.go
+++ b/internal/http/http.go
@@ -201,8 +201,10 @@ func (c *Client) Do(ctx context.Context, method string, urlStr string, opts *Opt
// Do request
res, err := c.Client.Do(req)
if err != nil {
- return nil, nil, errors.WrapPrefix(err,
- fmt.Sprintf("failed HTTP request with method %s and url %s", method, urlStr), 0)
+ if errors.Is(err, context.DeadlineExceeded) {
+ return nil, nil, &TimeoutError{URL: urlStr, Method: method}
+ }
+ return nil, nil, fmt.Errorf("failed HTTP request with method: '%s', url: '%s' and error: %w", method, urlStr, err)
}
// Request successful, make sure body is closed at the end
@@ -229,6 +231,21 @@ func (c *Client) Do(ctx context.Context, method string, urlStr string, opts *Opt
return res.Header, body, nil
}
+// TimeoutError indicates that we have gotten a timeout
+type TimeoutError struct {
+ URL string
+ Method string
+}
+
+// Error returns the TimeoutError as an error string.
+func (e *TimeoutError) Error() string {
+ return fmt.Sprintf(
+ "timeout in obtaining HTTP resource: '%s' with method: '%s'",
+ e.URL,
+ e.Method,
+ )
+}
+
// StatusError indicates that we have received a HTTP status error.
type StatusError struct {
URL string