Skip to content

Commit

Permalink
netx/errorx: wrap context.Canceled
Browse files Browse the repository at this point in the history
Like for MK errors, use the same naming of the C++11 library.

This error will happen when the mobile app will interrupt specific
tests, so better to have a clear naming for it.

Part of #646.
  • Loading branch information
bassosimone committed Jun 9, 2020
1 parent 07ac3a9 commit bd09875
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion experiment/tor/tor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func TestUnitDefautFlexibleConnectDirPort(t *testing.T) {
if err == nil {
t.Fatal("expected an error here")
}
if !strings.HasSuffix(err.Error(), "context canceled") {
if !strings.HasSuffix(err.Error(), "interrupted") {
t.Fatal("not the error we expected")
}
if tk.HTTPRequests == nil {
Expand Down
8 changes: 4 additions & 4 deletions experiment/urlgetter/getter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestGetterWithCancelledContextVanilla(t *testing.T) {
if tk.FailedOperation == nil || *tk.FailedOperation != modelx.TopLevelOperation {
t.Fatal("not the FailedOperation we expected")
}
if tk.Failure == nil || !strings.HasSuffix(*tk.Failure, "context canceled") {
if tk.Failure == nil || !strings.HasSuffix(*tk.Failure, "interrupted") {
t.Fatal("not the Failure we expected")
}
if len(tk.NetworkEvents) != 3 {
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestGetterWithCancelledContextAndMethod(t *testing.T) {
if tk.FailedOperation == nil || *tk.FailedOperation != modelx.TopLevelOperation {
t.Fatal("not the FailedOperation we expected")
}
if tk.Failure == nil || !strings.HasSuffix(*tk.Failure, "context canceled") {
if tk.Failure == nil || !strings.HasSuffix(*tk.Failure, "interrupted") {
t.Fatal("not the Failure we expected")
}
if len(tk.NetworkEvents) != 3 {
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestGetterWithCancelledContextNoFollowRedirects(t *testing.T) {
if tk.FailedOperation == nil || *tk.FailedOperation != modelx.TopLevelOperation {
t.Fatal("not the FailedOperation we expected")
}
if tk.Failure == nil || !strings.HasSuffix(*tk.Failure, "context canceled") {
if tk.Failure == nil || !strings.HasSuffix(*tk.Failure, "interrupted") {
t.Fatal("not the Failure we expected")
}
if len(tk.NetworkEvents) != 3 {
Expand Down Expand Up @@ -300,7 +300,7 @@ func TestGetterWithCancelledContextWithTunnel(t *testing.T) {
if tk.FailedOperation == nil || *tk.FailedOperation != modelx.TopLevelOperation {
t.Fatal("not the FailedOperation we expected")
}
if tk.Failure == nil || !strings.HasSuffix(*tk.Failure, "context canceled") {
if tk.Failure == nil || !strings.HasSuffix(*tk.Failure, "interrupted") {
t.Fatal("not the Failure we expected")
}
if len(tk.NetworkEvents) != 3 {
Expand Down
4 changes: 4 additions & 0 deletions netx/errorx/errorx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package errorx

import (
"context"
"crypto/x509"
"errors"
"fmt"
Expand Down Expand Up @@ -57,6 +58,9 @@ func toFailureString(err error) string {
if errors.Is(err, modelx.ErrDNSBogon) {
return modelx.FailureDNSBogonError // not in MK
}
if errors.Is(err, context.Canceled) {
return modelx.FailureInterrupted
}

var x509HostnameError x509.HostnameError
if errors.As(err, &x509HostnameError) {
Expand Down
5 changes: 5 additions & 0 deletions netx/errorx/errorx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func TestToFailureString(t *testing.T) {
t.Fatal("unexpected result")
}
})
t.Run("for context.Canceled", func(t *testing.T) {
if toFailureString(context.Canceled) != modelx.FailureInterrupted {
t.Fatal("unexpected result")
}
})
t.Run("for x509.HostnameError", func(t *testing.T) {
var err x509.HostnameError
if toFailureString(err) != modelx.FailureSSLInvalidHostname {
Expand Down
3 changes: 3 additions & 0 deletions netx/modelx/modelx.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ const (
// FailureGenericTimeoutError means we got some timer has expired.
FailureGenericTimeoutError = "generic_timeout_error"

// FailureInterrupted means that the user interrupted us.
FailureInterrupted = "interrupted"

// FailureSSLInvalidHostname means we got certificate is not valid for SNI.
FailureSSLInvalidHostname = "ssl_invalid_hostname"

Expand Down

0 comments on commit bd09875

Please sign in to comment.