diff --git a/ably/error_test.go b/ably/error_test.go index 59bdcd6f..c5eb860d 100644 --- a/ably/error_test.go +++ b/ably/error_test.go @@ -7,6 +7,7 @@ import ( "context" "errors" "fmt" + "net" "net/http" "net/http/httptest" "net/url" @@ -146,3 +147,41 @@ func TestIssue_154(t *testing.T) { assert.Equal(t, http.StatusMethodNotAllowed, et.StatusCode, "expected %d got %d: %v", http.StatusMethodNotAllowed, et.StatusCode, err) } + +func Test_DNSOrTimeoutErr(t *testing.T) { + dnsErr := net.DNSError{ + Err: "Can't resolve host", + Name: "Host unresolvable", + Server: "rest.ably.com", + IsTimeout: false, + IsTemporary: false, + IsNotFound: false, + } + + WrappedDNSErr := fmt.Errorf("custom error occured %w", &dnsErr) + if !ably.IsTimeoutOrDnsErr(WrappedDNSErr) { + t.Fatalf("%v is a DNS error", WrappedDNSErr) + } + + urlErr := url.Error{ + URL: "rest.ably.io", + Err: errors.New("URL error occured"), + Op: "IO read OP", + } + + if ably.IsTimeoutOrDnsErr(&urlErr) { + t.Fatalf("%v is not a DNS or timeout error", urlErr) + } + + urlErr.Err = &dnsErr + + if !ably.IsTimeoutOrDnsErr(WrappedDNSErr) { + t.Fatalf("%v is a DNS error", urlErr) + } + + dnsErr.IsTimeout = true + + if !ably.IsTimeoutOrDnsErr(WrappedDNSErr) { + t.Fatalf("%v is a timeout error", urlErr) + } +} diff --git a/ably/export_test.go b/ably/export_test.go index 55aaef87..7fcbd64e 100644 --- a/ably/export_test.go +++ b/ably/export_test.go @@ -65,6 +65,10 @@ func UnwrapStatusCode(err error) int { return statusCode(err) } +func IsTimeoutOrDnsErr(err error) bool { + return isTimeoutOrDnsErr(err) +} + func (a *Auth) Timestamp(ctx context.Context, query bool) (time.Time, error) { return a.timestamp(ctx, query) }