refactor(errorsx): prepare for splitting the package (#476)

We will move the sane part of this package to i/netxlite/errorsx
and we will move the rest to i/e/legacy/errorsx.

What is the sane part? The sane part is error classifiers plus
the definition of ErrWrapper. The rest, including the rules
on how to decide whether an operation is major, are tricky and
we should consider them legacy and replace them with rules
that are more easy to understand and reason on.

Part of https://github.com/ooni/probe/issues/1591
This commit is contained in:
Simone Basso 2021-09-07 15:46:32 +02:00 committed by GitHub
parent cef801fa23
commit ccb3a644e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 96 additions and 90 deletions

View File

@ -23,7 +23,7 @@ func (d *ErrorWrapperDialer) DialContext(ctx context.Context, network, address s
conn, err := d.Dialer.DialContext(ctx, network, address) conn, err := d.Dialer.DialContext(ctx, network, address)
if err != nil { if err != nil {
return nil, &ErrWrapper{ return nil, &ErrWrapper{
Failure: toFailureString(err), Failure: ClassifyGenericError(err),
Operation: ConnectOperation, Operation: ConnectOperation,
WrappedErr: err, WrappedErr: err,
} }
@ -42,7 +42,7 @@ func (c *errorWrapperConn) Read(b []byte) (int, error) {
count, err := c.Conn.Read(b) count, err := c.Conn.Read(b)
if err != nil { if err != nil {
return 0, &ErrWrapper{ return 0, &ErrWrapper{
Failure: toFailureString(err), Failure: ClassifyGenericError(err),
Operation: ReadOperation, Operation: ReadOperation,
WrappedErr: err, WrappedErr: err,
} }
@ -55,7 +55,7 @@ func (c *errorWrapperConn) Write(b []byte) (int, error) {
count, err := c.Conn.Write(b) count, err := c.Conn.Write(b)
if err != nil { if err != nil {
return 0, &ErrWrapper{ return 0, &ErrWrapper{
Failure: toFailureString(err), Failure: ClassifyGenericError(err),
Operation: WriteOperation, Operation: WriteOperation,
WrappedErr: err, WrappedErr: err,
} }
@ -68,7 +68,7 @@ func (c *errorWrapperConn) Close() error {
err := c.Conn.Close() err := c.Conn.Close()
if err != nil { if err != nil {
return &ErrWrapper{ return &ErrWrapper{
Failure: toFailureString(err), Failure: ClassifyGenericError(err),
Operation: CloseOperation, Operation: CloseOperation,
WrappedErr: err, WrappedErr: err,
} }

View File

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT. // Code generated by go generate; DO NOT EDIT.
// Generated: 2021-09-07 14:56:43.206193 +0200 CEST m=+0.141131209 // Generated: 2021-09-07 15:15:03.350386 +0200 CEST m=+0.135456751
package errorsx package errorsx
@ -60,10 +60,10 @@ const (
FailureJSONParseError = "json_parse_error" FailureJSONParseError = "json_parse_error"
) )
// toSyscallErr converts a syscall error to the // classifySyscallError converts a syscall error to the
// proper OONI error. Returns the OONI error string // proper OONI error. Returns the OONI error string
// on success, an empty string otherwise. // on success, an empty string otherwise.
func toSyscallErr(err error) string { func classifySyscallError(err error) string {
// filter out system errors: necessary to detect all windows errors // filter out system errors: necessary to detect all windows errors
// https://github.com/ooni/probe/issues/1526 describes the problem // https://github.com/ooni/probe/issues/1526 describes the problem
// of mapping localized windows errors. // of mapping localized windows errors.

View File

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT. // Code generated by go generate; DO NOT EDIT.
// Generated: 2021-09-07 14:56:43.25305 +0200 CEST m=+0.187989417 // Generated: 2021-09-07 15:15:03.398087 +0200 CEST m=+0.183158793
package errorsx package errorsx
@ -10,94 +10,94 @@ import (
) )
func TestToSyscallErr(t *testing.T) { func TestToSyscallErr(t *testing.T) {
if v := toSyscallErr(io.EOF); v != "" { if v := classifySyscallError(io.EOF); v != "" {
t.Fatalf("expected empty string, got '%s'", v) t.Fatalf("expected empty string, got '%s'", v)
} }
if v := toSyscallErr(ECANCELED); v != FailureOperationCanceled { if v := classifySyscallError(ECANCELED); v != FailureOperationCanceled {
t.Fatalf("expected '%s', got '%s'", FailureOperationCanceled, v) t.Fatalf("expected '%s', got '%s'", FailureOperationCanceled, v)
} }
if v := toSyscallErr(ECONNREFUSED); v != FailureConnectionRefused { if v := classifySyscallError(ECONNREFUSED); v != FailureConnectionRefused {
t.Fatalf("expected '%s', got '%s'", FailureConnectionRefused, v) t.Fatalf("expected '%s', got '%s'", FailureConnectionRefused, v)
} }
if v := toSyscallErr(ECONNRESET); v != FailureConnectionReset { if v := classifySyscallError(ECONNRESET); v != FailureConnectionReset {
t.Fatalf("expected '%s', got '%s'", FailureConnectionReset, v) t.Fatalf("expected '%s', got '%s'", FailureConnectionReset, v)
} }
if v := toSyscallErr(EHOSTUNREACH); v != FailureHostUnreachable { if v := classifySyscallError(EHOSTUNREACH); v != FailureHostUnreachable {
t.Fatalf("expected '%s', got '%s'", FailureHostUnreachable, v) t.Fatalf("expected '%s', got '%s'", FailureHostUnreachable, v)
} }
if v := toSyscallErr(ETIMEDOUT); v != FailureTimedOut { if v := classifySyscallError(ETIMEDOUT); v != FailureTimedOut {
t.Fatalf("expected '%s', got '%s'", FailureTimedOut, v) t.Fatalf("expected '%s', got '%s'", FailureTimedOut, v)
} }
if v := toSyscallErr(EAFNOSUPPORT); v != FailureAddressFamilyNotSupported { if v := classifySyscallError(EAFNOSUPPORT); v != FailureAddressFamilyNotSupported {
t.Fatalf("expected '%s', got '%s'", FailureAddressFamilyNotSupported, v) t.Fatalf("expected '%s', got '%s'", FailureAddressFamilyNotSupported, v)
} }
if v := toSyscallErr(EADDRINUSE); v != FailureAddressInUse { if v := classifySyscallError(EADDRINUSE); v != FailureAddressInUse {
t.Fatalf("expected '%s', got '%s'", FailureAddressInUse, v) t.Fatalf("expected '%s', got '%s'", FailureAddressInUse, v)
} }
if v := toSyscallErr(EADDRNOTAVAIL); v != FailureAddressNotAvailable { if v := classifySyscallError(EADDRNOTAVAIL); v != FailureAddressNotAvailable {
t.Fatalf("expected '%s', got '%s'", FailureAddressNotAvailable, v) t.Fatalf("expected '%s', got '%s'", FailureAddressNotAvailable, v)
} }
if v := toSyscallErr(EISCONN); v != FailureAlreadyConnected { if v := classifySyscallError(EISCONN); v != FailureAlreadyConnected {
t.Fatalf("expected '%s', got '%s'", FailureAlreadyConnected, v) t.Fatalf("expected '%s', got '%s'", FailureAlreadyConnected, v)
} }
if v := toSyscallErr(EFAULT); v != FailureBadAddress { if v := classifySyscallError(EFAULT); v != FailureBadAddress {
t.Fatalf("expected '%s', got '%s'", FailureBadAddress, v) t.Fatalf("expected '%s', got '%s'", FailureBadAddress, v)
} }
if v := toSyscallErr(EBADF); v != FailureBadFileDescriptor { if v := classifySyscallError(EBADF); v != FailureBadFileDescriptor {
t.Fatalf("expected '%s', got '%s'", FailureBadFileDescriptor, v) t.Fatalf("expected '%s', got '%s'", FailureBadFileDescriptor, v)
} }
if v := toSyscallErr(ECONNABORTED); v != FailureConnectionAborted { if v := classifySyscallError(ECONNABORTED); v != FailureConnectionAborted {
t.Fatalf("expected '%s', got '%s'", FailureConnectionAborted, v) t.Fatalf("expected '%s', got '%s'", FailureConnectionAborted, v)
} }
if v := toSyscallErr(EALREADY); v != FailureConnectionAlreadyInProgress { if v := classifySyscallError(EALREADY); v != FailureConnectionAlreadyInProgress {
t.Fatalf("expected '%s', got '%s'", FailureConnectionAlreadyInProgress, v) t.Fatalf("expected '%s', got '%s'", FailureConnectionAlreadyInProgress, v)
} }
if v := toSyscallErr(EDESTADDRREQ); v != FailureDestinationAddressRequired { if v := classifySyscallError(EDESTADDRREQ); v != FailureDestinationAddressRequired {
t.Fatalf("expected '%s', got '%s'", FailureDestinationAddressRequired, v) t.Fatalf("expected '%s', got '%s'", FailureDestinationAddressRequired, v)
} }
if v := toSyscallErr(EINTR); v != FailureInterrupted { if v := classifySyscallError(EINTR); v != FailureInterrupted {
t.Fatalf("expected '%s', got '%s'", FailureInterrupted, v) t.Fatalf("expected '%s', got '%s'", FailureInterrupted, v)
} }
if v := toSyscallErr(EINVAL); v != FailureInvalidArgument { if v := classifySyscallError(EINVAL); v != FailureInvalidArgument {
t.Fatalf("expected '%s', got '%s'", FailureInvalidArgument, v) t.Fatalf("expected '%s', got '%s'", FailureInvalidArgument, v)
} }
if v := toSyscallErr(EMSGSIZE); v != FailureMessageSize { if v := classifySyscallError(EMSGSIZE); v != FailureMessageSize {
t.Fatalf("expected '%s', got '%s'", FailureMessageSize, v) t.Fatalf("expected '%s', got '%s'", FailureMessageSize, v)
} }
if v := toSyscallErr(ENETDOWN); v != FailureNetworkDown { if v := classifySyscallError(ENETDOWN); v != FailureNetworkDown {
t.Fatalf("expected '%s', got '%s'", FailureNetworkDown, v) t.Fatalf("expected '%s', got '%s'", FailureNetworkDown, v)
} }
if v := toSyscallErr(ENETRESET); v != FailureNetworkReset { if v := classifySyscallError(ENETRESET); v != FailureNetworkReset {
t.Fatalf("expected '%s', got '%s'", FailureNetworkReset, v) t.Fatalf("expected '%s', got '%s'", FailureNetworkReset, v)
} }
if v := toSyscallErr(ENETUNREACH); v != FailureNetworkUnreachable { if v := classifySyscallError(ENETUNREACH); v != FailureNetworkUnreachable {
t.Fatalf("expected '%s', got '%s'", FailureNetworkUnreachable, v) t.Fatalf("expected '%s', got '%s'", FailureNetworkUnreachable, v)
} }
if v := toSyscallErr(ENOBUFS); v != FailureNoBufferSpace { if v := classifySyscallError(ENOBUFS); v != FailureNoBufferSpace {
t.Fatalf("expected '%s', got '%s'", FailureNoBufferSpace, v) t.Fatalf("expected '%s', got '%s'", FailureNoBufferSpace, v)
} }
if v := toSyscallErr(ENOPROTOOPT); v != FailureNoProtocolOption { if v := classifySyscallError(ENOPROTOOPT); v != FailureNoProtocolOption {
t.Fatalf("expected '%s', got '%s'", FailureNoProtocolOption, v) t.Fatalf("expected '%s', got '%s'", FailureNoProtocolOption, v)
} }
if v := toSyscallErr(ENOTSOCK); v != FailureNotASocket { if v := classifySyscallError(ENOTSOCK); v != FailureNotASocket {
t.Fatalf("expected '%s', got '%s'", FailureNotASocket, v) t.Fatalf("expected '%s', got '%s'", FailureNotASocket, v)
} }
if v := toSyscallErr(ENOTCONN); v != FailureNotConnected { if v := classifySyscallError(ENOTCONN); v != FailureNotConnected {
t.Fatalf("expected '%s', got '%s'", FailureNotConnected, v) t.Fatalf("expected '%s', got '%s'", FailureNotConnected, v)
} }
if v := toSyscallErr(EWOULDBLOCK); v != FailureOperationWouldBlock { if v := classifySyscallError(EWOULDBLOCK); v != FailureOperationWouldBlock {
t.Fatalf("expected '%s', got '%s'", FailureOperationWouldBlock, v) t.Fatalf("expected '%s', got '%s'", FailureOperationWouldBlock, v)
} }
if v := toSyscallErr(EACCES); v != FailurePermissionDenied { if v := classifySyscallError(EACCES); v != FailurePermissionDenied {
t.Fatalf("expected '%s', got '%s'", FailurePermissionDenied, v) t.Fatalf("expected '%s', got '%s'", FailurePermissionDenied, v)
} }
if v := toSyscallErr(EPROTONOSUPPORT); v != FailureProtocolNotSupported { if v := classifySyscallError(EPROTONOSUPPORT); v != FailureProtocolNotSupported {
t.Fatalf("expected '%s', got '%s'", FailureProtocolNotSupported, v) t.Fatalf("expected '%s', got '%s'", FailureProtocolNotSupported, v)
} }
if v := toSyscallErr(EPROTOTYPE); v != FailureWrongProtocolType { if v := classifySyscallError(EPROTOTYPE); v != FailureWrongProtocolType {
t.Fatalf("expected '%s', got '%s'", FailureWrongProtocolType, v) t.Fatalf("expected '%s', got '%s'", FailureWrongProtocolType, v)
} }
if v := toSyscallErr(syscall.Errno(0)); v != "" { if v := classifySyscallError(syscall.Errno(0)); v != "" {
t.Fatalf("expected empty string, got '%s'", v) t.Fatalf("expected empty string, got '%s'", v)
} }
} }

View File

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT. // Code generated by go generate; DO NOT EDIT.
// Generated: 2021-09-07 14:56:43.065762 +0200 CEST m=+0.000698667 // Generated: 2021-09-07 15:15:03.215384 +0200 CEST m=+0.000452543
package errorsx package errorsx

View File

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT. // Code generated by go generate; DO NOT EDIT.
// Generated: 2021-09-07 14:56:43.179707 +0200 CEST m=+0.114644626 // Generated: 2021-09-07 15:15:03.324258 +0200 CEST m=+0.109328501
package errorsx package errorsx

View File

@ -85,7 +85,7 @@ func (b SafeErrWrapperBuilder) MaybeBuild() (err error) {
if b.Error != nil { if b.Error != nil {
classifier := b.Classifier classifier := b.Classifier
if classifier == nil { if classifier == nil {
classifier = toFailureString classifier = ClassifyGenericError
} }
err = &ErrWrapper{ err = &ErrWrapper{
Failure: classifier(b.Error), Failure: classifier(b.Error),
@ -100,7 +100,10 @@ func (b SafeErrWrapperBuilder) MaybeBuild() (err error) {
// Use errors.Is / errors.As more often, when possible, in this classifier. // Use errors.Is / errors.As more often, when possible, in this classifier.
// These methods are more robust to library changes than strings. // These methods are more robust to library changes than strings.
// errors.Is / errors.As can only be used when the error is exported. // errors.Is / errors.As can only be used when the error is exported.
func toFailureString(err error) string {
// ClassifyGenericError is the generic classifier mapping an error
// occurred during an operation to an OONI failure string.
func ClassifyGenericError(err error) string {
// The list returned here matches the values used by MK unless // The list returned here matches the values used by MK unless
// explicitly noted otherwise with a comment. // explicitly noted otherwise with a comment.
@ -111,7 +114,7 @@ func toFailureString(err error) string {
return errwrapper.Error() // we've already wrapped it return errwrapper.Error() // we've already wrapped it
} }
if failure := toSyscallErr(err); failure != "" { if failure := classifySyscallError(err); failure != "" {
return failure return failure
} }

View File

@ -34,47 +34,47 @@ func TestMaybeBuildFactory(t *testing.T) {
func TestToFailureString(t *testing.T) { func TestToFailureString(t *testing.T) {
t.Run("for already wrapped error", func(t *testing.T) { t.Run("for already wrapped error", func(t *testing.T) {
err := SafeErrWrapperBuilder{Error: io.EOF}.MaybeBuild() err := SafeErrWrapperBuilder{Error: io.EOF}.MaybeBuild()
if toFailureString(err) != FailureEOFError { if ClassifyGenericError(err) != FailureEOFError {
t.Fatal("unexpected result") t.Fatal("unexpected result")
} }
}) })
t.Run("for context.Canceled", func(t *testing.T) { t.Run("for context.Canceled", func(t *testing.T) {
if toFailureString(context.Canceled) != FailureInterrupted { if ClassifyGenericError(context.Canceled) != FailureInterrupted {
t.Fatal("unexpected result") t.Fatal("unexpected result")
} }
}) })
t.Run("for operation was canceled error", func(t *testing.T) { t.Run("for operation was canceled error", func(t *testing.T) {
if toFailureString(errors.New("operation was canceled")) != FailureInterrupted { if ClassifyGenericError(errors.New("operation was canceled")) != FailureInterrupted {
t.Fatal("unexpected result") t.Fatal("unexpected result")
} }
}) })
t.Run("for EOF", func(t *testing.T) { t.Run("for EOF", func(t *testing.T) {
if toFailureString(io.EOF) != FailureEOFError { if ClassifyGenericError(io.EOF) != FailureEOFError {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
t.Run("for canceled", func(t *testing.T) { t.Run("for canceled", func(t *testing.T) {
if toFailureString(syscall.ECANCELED) != FailureOperationCanceled { if ClassifyGenericError(syscall.ECANCELED) != FailureOperationCanceled {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
t.Run("for connection_refused", func(t *testing.T) { t.Run("for connection_refused", func(t *testing.T) {
if toFailureString(syscall.ECONNREFUSED) != FailureConnectionRefused { if ClassifyGenericError(syscall.ECONNREFUSED) != FailureConnectionRefused {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
t.Run("for connection_reset", func(t *testing.T) { t.Run("for connection_reset", func(t *testing.T) {
if toFailureString(syscall.ECONNRESET) != FailureConnectionReset { if ClassifyGenericError(syscall.ECONNRESET) != FailureConnectionReset {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
t.Run("for host_unreachable", func(t *testing.T) { t.Run("for host_unreachable", func(t *testing.T) {
if toFailureString(syscall.EHOSTUNREACH) != FailureHostUnreachable { if ClassifyGenericError(syscall.EHOSTUNREACH) != FailureHostUnreachable {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
t.Run("for system timeout", func(t *testing.T) { t.Run("for system timeout", func(t *testing.T) {
if toFailureString(syscall.ETIMEDOUT) != FailureTimedOut { if ClassifyGenericError(syscall.ETIMEDOUT) != FailureTimedOut {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
@ -82,12 +82,12 @@ func TestToFailureString(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1) ctx, cancel := context.WithTimeout(context.Background(), 1)
defer cancel() defer cancel()
<-ctx.Done() <-ctx.Done()
if toFailureString(ctx.Err()) != FailureGenericTimeoutError { if ClassifyGenericError(ctx.Err()) != FailureGenericTimeoutError {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
t.Run("for stun's transaction is timed out", func(t *testing.T) { t.Run("for stun's transaction is timed out", func(t *testing.T) {
if toFailureString(stun.ErrTransactionTimeOut) != FailureGenericTimeoutError { if ClassifyGenericError(stun.ErrTransactionTimeOut) != FailureGenericTimeoutError {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
@ -101,18 +101,18 @@ func TestToFailureString(t *testing.T) {
if conn != nil { if conn != nil {
t.Fatal("expected nil connection here") t.Fatal("expected nil connection here")
} }
if toFailureString(err) != FailureGenericTimeoutError { if ClassifyGenericError(err) != FailureGenericTimeoutError {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
t.Run("for TLS handshake timeout error", func(t *testing.T) { t.Run("for TLS handshake timeout error", func(t *testing.T) {
err := errors.New("net/http: TLS handshake timeout") err := errors.New("net/http: TLS handshake timeout")
if toFailureString(err) != FailureGenericTimeoutError { if ClassifyGenericError(err) != FailureGenericTimeoutError {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
t.Run("for no such host", func(t *testing.T) { t.Run("for no such host", func(t *testing.T) {
if toFailureString(&net.DNSError{ if ClassifyGenericError(&net.DNSError{
Err: "no such host", Err: "no such host",
}) != FailureDNSNXDOMAINError { }) != FailureDNSNXDOMAINError {
t.Fatal("unexpected results") t.Fatal("unexpected results")
@ -121,7 +121,7 @@ func TestToFailureString(t *testing.T) {
t.Run("for errors including IPv4 address", func(t *testing.T) { t.Run("for errors including IPv4 address", func(t *testing.T) {
input := errors.New("read tcp 10.0.2.15:56948->93.184.216.34:443: use of closed network connection") input := errors.New("read tcp 10.0.2.15:56948->93.184.216.34:443: use of closed network connection")
expected := "unknown_failure: read tcp [scrubbed]->[scrubbed]: use of closed network connection" expected := "unknown_failure: read tcp [scrubbed]->[scrubbed]: use of closed network connection"
out := toFailureString(input) out := ClassifyGenericError(input)
if out != expected { if out != expected {
t.Fatal(cmp.Diff(expected, out)) t.Fatal(cmp.Diff(expected, out))
} }
@ -129,7 +129,7 @@ func TestToFailureString(t *testing.T) {
t.Run("for errors including IPv6 address", func(t *testing.T) { t.Run("for errors including IPv6 address", func(t *testing.T) {
input := errors.New("read tcp [::1]:56948->[::1]:443: use of closed network connection") input := errors.New("read tcp [::1]:56948->[::1]:443: use of closed network connection")
expected := "unknown_failure: read tcp [scrubbed]->[scrubbed]: use of closed network connection" expected := "unknown_failure: read tcp [scrubbed]->[scrubbed]: use of closed network connection"
out := toFailureString(input) out := ClassifyGenericError(input)
if out != expected { if out != expected {
t.Fatal(cmp.Diff(expected, out)) t.Fatal(cmp.Diff(expected, out))
} }
@ -149,7 +149,7 @@ func TestToFailureString(t *testing.T) {
if sess != nil { if sess != nil {
t.Fatal("expected nil session here") t.Fatal("expected nil session here")
} }
if toFailureString(err) != FailureGenericTimeoutError { if ClassifyGenericError(err) != FailureGenericTimeoutError {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
@ -157,51 +157,51 @@ func TestToFailureString(t *testing.T) {
func TestClassifyQUICFailure(t *testing.T) { func TestClassifyQUICFailure(t *testing.T) {
t.Run("for connection_reset", func(t *testing.T) { t.Run("for connection_reset", func(t *testing.T) {
if classifyQUICFailure(&quic.StatelessResetError{}) != FailureConnectionReset { if ClassifyQUICHandshakeError(&quic.StatelessResetError{}) != FailureConnectionReset {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
t.Run("for incompatible quic version", func(t *testing.T) { t.Run("for incompatible quic version", func(t *testing.T) {
if classifyQUICFailure(&quic.VersionNegotiationError{}) != FailureQUICIncompatibleVersion { if ClassifyQUICHandshakeError(&quic.VersionNegotiationError{}) != FailureQUICIncompatibleVersion {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
t.Run("for quic connection refused", func(t *testing.T) { t.Run("for quic connection refused", func(t *testing.T) {
if classifyQUICFailure(&quic.TransportError{ErrorCode: quic.ConnectionRefused}) != FailureConnectionRefused { if ClassifyQUICHandshakeError(&quic.TransportError{ErrorCode: quic.ConnectionRefused}) != FailureConnectionRefused {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
t.Run("for quic handshake timeout", func(t *testing.T) { t.Run("for quic handshake timeout", func(t *testing.T) {
if classifyQUICFailure(&quic.HandshakeTimeoutError{}) != FailureGenericTimeoutError { if ClassifyQUICHandshakeError(&quic.HandshakeTimeoutError{}) != FailureGenericTimeoutError {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
t.Run("for QUIC idle connection timeout", func(t *testing.T) { t.Run("for QUIC idle connection timeout", func(t *testing.T) {
if classifyQUICFailure(&quic.IdleTimeoutError{}) != FailureGenericTimeoutError { if ClassifyQUICHandshakeError(&quic.IdleTimeoutError{}) != FailureGenericTimeoutError {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
t.Run("for QUIC CRYPTO Handshake", func(t *testing.T) { t.Run("for QUIC CRYPTO Handshake", func(t *testing.T) {
var err quic.TransportErrorCode = quicTLSAlertHandshakeFailure var err quic.TransportErrorCode = quicTLSAlertHandshakeFailure
if classifyQUICFailure(&quic.TransportError{ErrorCode: err}) != FailureSSLFailedHandshake { if ClassifyQUICHandshakeError(&quic.TransportError{ErrorCode: err}) != FailureSSLFailedHandshake {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
t.Run("for QUIC CRYPTO Invalid Certificate", func(t *testing.T) { t.Run("for QUIC CRYPTO Invalid Certificate", func(t *testing.T) {
var err quic.TransportErrorCode = quicTLSAlertBadCertificate var err quic.TransportErrorCode = quicTLSAlertBadCertificate
if classifyQUICFailure(&quic.TransportError{ErrorCode: err}) != FailureSSLInvalidCertificate { if ClassifyQUICHandshakeError(&quic.TransportError{ErrorCode: err}) != FailureSSLInvalidCertificate {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
t.Run("for QUIC CRYPTO Unknown CA", func(t *testing.T) { t.Run("for QUIC CRYPTO Unknown CA", func(t *testing.T) {
var err quic.TransportErrorCode = quicTLSAlertUnknownCA var err quic.TransportErrorCode = quicTLSAlertUnknownCA
if classifyQUICFailure(&quic.TransportError{ErrorCode: err}) != FailureSSLUnknownAuthority { if ClassifyQUICHandshakeError(&quic.TransportError{ErrorCode: err}) != FailureSSLUnknownAuthority {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
t.Run("for QUIC CRYPTO Bad Hostname", func(t *testing.T) { t.Run("for QUIC CRYPTO Bad Hostname", func(t *testing.T) {
var err quic.TransportErrorCode = quicTLSUnrecognizedName var err quic.TransportErrorCode = quicTLSUnrecognizedName
if classifyQUICFailure(&quic.TransportError{ErrorCode: err}) != FailureSSLInvalidHostname { if ClassifyQUICHandshakeError(&quic.TransportError{ErrorCode: err}) != FailureSSLInvalidHostname {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
@ -210,7 +210,7 @@ func TestClassifyQUICFailure(t *testing.T) {
func TestClassifyResolveFailure(t *testing.T) { func TestClassifyResolveFailure(t *testing.T) {
t.Run("for ErrDNSBogon", func(t *testing.T) { t.Run("for ErrDNSBogon", func(t *testing.T) {
if classifyResolveFailure(ErrDNSBogon) != FailureDNSBogonError { if ClassifyResolverError(ErrDNSBogon) != FailureDNSBogonError {
t.Fatal("unexpected result") t.Fatal("unexpected result")
} }
}) })
@ -219,19 +219,19 @@ func TestClassifyResolveFailure(t *testing.T) {
func TestClassifyTLSFailure(t *testing.T) { func TestClassifyTLSFailure(t *testing.T) {
t.Run("for x509.HostnameError", func(t *testing.T) { t.Run("for x509.HostnameError", func(t *testing.T) {
var err x509.HostnameError var err x509.HostnameError
if classifyTLSFailure(err) != FailureSSLInvalidHostname { if ClassifyTLSHandshakeError(err) != FailureSSLInvalidHostname {
t.Fatal("unexpected result") t.Fatal("unexpected result")
} }
}) })
t.Run("for x509.UnknownAuthorityError", func(t *testing.T) { t.Run("for x509.UnknownAuthorityError", func(t *testing.T) {
var err x509.UnknownAuthorityError var err x509.UnknownAuthorityError
if classifyTLSFailure(err) != FailureSSLUnknownAuthority { if ClassifyTLSHandshakeError(err) != FailureSSLUnknownAuthority {
t.Fatal("unexpected result") t.Fatal("unexpected result")
} }
}) })
t.Run("for x509.CertificateInvalidError", func(t *testing.T) { t.Run("for x509.CertificateInvalidError", func(t *testing.T) {
var err x509.CertificateInvalidError var err x509.CertificateInvalidError
if classifyTLSFailure(err) != FailureSSLInvalidCertificate { if ClassifyTLSHandshakeError(err) != FailureSSLInvalidCertificate {
t.Fatal("unexpected result") t.Fatal("unexpected result")
} }
}) })

View File

@ -194,10 +194,10 @@ func writeGenericFile() {
} }
fileWrite(filep, ")\n\n") fileWrite(filep, ")\n\n")
fileWrite(filep, "// toSyscallErr converts a syscall error to the\n") fileWrite(filep, "// classifySyscallError converts a syscall error to the\n")
fileWrite(filep, "// proper OONI error. Returns the OONI error string\n") fileWrite(filep, "// proper OONI error. Returns the OONI error string\n")
fileWrite(filep, "// on success, an empty string otherwise.\n") fileWrite(filep, "// on success, an empty string otherwise.\n")
fileWrite(filep, "func toSyscallErr(err error) string {\n") fileWrite(filep, "func classifySyscallError(err error) string {\n")
fileWrite(filep, "\t// filter out system errors: necessary to detect all windows errors\n") fileWrite(filep, "\t// filter out system errors: necessary to detect all windows errors\n")
fileWrite(filep, "\t// https://github.com/ooni/probe/issues/1526 describes the problem\n") fileWrite(filep, "\t// https://github.com/ooni/probe/issues/1526 describes the problem\n")
fileWrite(filep, "\t// of mapping localized windows errors.\n") fileWrite(filep, "\t// of mapping localized windows errors.\n")
@ -235,7 +235,7 @@ func writeGenericTestFile() {
fileWrite(filep, ")\n\n") fileWrite(filep, ")\n\n")
fileWrite(filep, "func TestToSyscallErr(t *testing.T) {\n") fileWrite(filep, "func TestToSyscallErr(t *testing.T) {\n")
fileWrite(filep, "\tif v := toSyscallErr(io.EOF); v != \"\" {\n") fileWrite(filep, "\tif v := classifySyscallError(io.EOF); v != \"\" {\n")
fileWrite(filep, "\t\tt.Fatalf(\"expected empty string, got '%s'\", v)\n") fileWrite(filep, "\t\tt.Fatalf(\"expected empty string, got '%s'\", v)\n")
fileWrite(filep, "\t}\n") fileWrite(filep, "\t}\n")
@ -243,14 +243,14 @@ func writeGenericTestFile() {
if !spec.IsSystemError() { if !spec.IsSystemError() {
continue continue
} }
filePrintf(filep, "\tif v := toSyscallErr(%s); v != %s {\n", filePrintf(filep, "\tif v := classifySyscallError(%s); v != %s {\n",
spec.AsErrnoName(), spec.AsFailureVar()) spec.AsErrnoName(), spec.AsFailureVar())
filePrintf(filep, "\t\tt.Fatalf(\"expected '%%s', got '%%s'\", %s, v)\n", filePrintf(filep, "\t\tt.Fatalf(\"expected '%%s', got '%%s'\", %s, v)\n",
spec.AsFailureVar()) spec.AsFailureVar())
fileWrite(filep, "\t}\n") fileWrite(filep, "\t}\n")
} }
fileWrite(filep, "\tif v := toSyscallErr(syscall.Errno(0)); v != \"\" {\n") fileWrite(filep, "\tif v := classifySyscallError(syscall.Errno(0)); v != \"\" {\n")
fileWrite(filep, "\t\tt.Fatalf(\"expected empty string, got '%s'\", v)\n") fileWrite(filep, "\t\tt.Fatalf(\"expected empty string, got '%s'\", v)\n")
fileWrite(filep, "\t}\n") fileWrite(filep, "\t}\n")
fileWrite(filep, "}\n") fileWrite(filep, "}\n")

View File

@ -88,7 +88,7 @@ func (d *ErrorWrapperQUICDialer) DialContext(
tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlySession, error) { tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlySession, error) {
sess, err := d.Dialer.DialContext(ctx, network, host, tlsCfg, cfg) sess, err := d.Dialer.DialContext(ctx, network, host, tlsCfg, cfg)
err = SafeErrWrapperBuilder{ err = SafeErrWrapperBuilder{
Classifier: classifyQUICFailure, Classifier: ClassifyQUICHandshakeError,
Error: err, Error: err,
Operation: QUICHandshakeOperation, Operation: QUICHandshakeOperation,
}.MaybeBuild() }.MaybeBuild()
@ -98,8 +98,9 @@ func (d *ErrorWrapperQUICDialer) DialContext(
return sess, nil return sess, nil
} }
// classifyQUICFailure is a classifier to translate QUIC errors to OONI error strings. // ClassifyQUICHandshakeError maps an error occurred during the QUIC
func classifyQUICFailure(err error) string { // handshake to an OONI failure string.
func ClassifyQUICHandshakeError(err error) string {
var versionNegotiation *quic.VersionNegotiationError var versionNegotiation *quic.VersionNegotiationError
var statelessReset *quic.StatelessResetError var statelessReset *quic.StatelessResetError
var handshakeTimeout *quic.HandshakeTimeoutError var handshakeTimeout *quic.HandshakeTimeoutError
@ -139,7 +140,7 @@ func classifyQUICFailure(err error) string {
return FailureSSLInvalidHostname return FailureSSLInvalidHostname
} }
} }
return toFailureString(err) return ClassifyGenericError(err)
} }
// TLS alert protocol as defined in RFC8446 // TLS alert protocol as defined in RFC8446

View File

@ -23,19 +23,20 @@ var _ Resolver = &ErrorWrapperResolver{}
func (r *ErrorWrapperResolver) LookupHost(ctx context.Context, hostname string) ([]string, error) { func (r *ErrorWrapperResolver) LookupHost(ctx context.Context, hostname string) ([]string, error) {
addrs, err := r.Resolver.LookupHost(ctx, hostname) addrs, err := r.Resolver.LookupHost(ctx, hostname)
err = SafeErrWrapperBuilder{ err = SafeErrWrapperBuilder{
Classifier: classifyResolveFailure, Classifier: ClassifyResolverError,
Error: err, Error: err,
Operation: ResolveOperation, Operation: ResolveOperation,
}.MaybeBuild() }.MaybeBuild()
return addrs, err return addrs, err
} }
// classifyResolveFailure is a classifier to translate DNS resolving errors to OONI error strings. // ClassifyResolverError maps an error occurred during a domain name
func classifyResolveFailure(err error) string { // resolution to the corresponding OONI failure string.
func ClassifyResolverError(err error) string {
if errors.Is(err, ErrDNSBogon) { if errors.Is(err, ErrDNSBogon) {
return FailureDNSBogonError // not in MK return FailureDNSBogonError // not in MK
} }
return toFailureString(err) return ClassifyGenericError(err)
} }
type resolverNetworker interface { type resolverNetworker interface {

View File

@ -25,15 +25,16 @@ func (h *ErrorWrapperTLSHandshaker) Handshake(
) (net.Conn, tls.ConnectionState, error) { ) (net.Conn, tls.ConnectionState, error) {
tlsconn, state, err := h.TLSHandshaker.Handshake(ctx, conn, config) tlsconn, state, err := h.TLSHandshaker.Handshake(ctx, conn, config)
err = SafeErrWrapperBuilder{ err = SafeErrWrapperBuilder{
Classifier: classifyTLSFailure, Classifier: ClassifyTLSHandshakeError,
Error: err, Error: err,
Operation: TLSHandshakeOperation, Operation: TLSHandshakeOperation,
}.MaybeBuild() }.MaybeBuild()
return tlsconn, state, err return tlsconn, state, err
} }
// classifyTLSFailure is a classifier to translate TLS errors to OONI error strings. // ClassifyTLSHandshakeError maps an error occurred during the TLS
func classifyTLSFailure(err error) string { // handshake to an OONI failure string.
func ClassifyTLSHandshakeError(err error) string {
var x509HostnameError x509.HostnameError var x509HostnameError x509.HostnameError
if errors.As(err, &x509HostnameError) { if errors.As(err, &x509HostnameError) {
// Test case: https://wrong.host.badssl.com/ // Test case: https://wrong.host.badssl.com/
@ -50,5 +51,5 @@ func classifyTLSFailure(err error) string {
// Test case: https://expired.badssl.com/ // Test case: https://expired.badssl.com/
return FailureSSLInvalidCertificate return FailureSSLInvalidCertificate
} }
return toFailureString(err) return ClassifyGenericError(err)
} }