ooni-probe-cli/internal/errorsx/errorsx.go
Simone Basso 83440cf110
refactor: split errorsx in good and legacy (#477)
The legacy part for now is internal/errorsx. It will stay there until
I figure out whether it also needs some extra bug fixing.

The good part is now in internal/netxlite/errorsx and contains all the
logic for mapping errors. We need to further improve upon this logic
by writing more thorough integration tests for QUIC.

We also need to copy the various dialer, conn, etc adapters that set
errors. We will put them inside netxlite and we will generate errors in
a way that is less crazy with respect to the major operation. (The
idea is to always wrap, given that now we measure in an incremental way
and we don't measure every operation together.)

Part of https://github.com/ooni/probe/issues/1591
2021-09-07 17:09:30 +02:00

71 lines
1.9 KiB
Go

// Package errorsx contains error extensions.
package errorsx
import (
"errors"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
)
// SafeErrWrapperBuilder contains a builder for ErrWrapper that
// is safe, i.e., behaves correctly when the error is nil.
type SafeErrWrapperBuilder struct {
// Error is the error, if any
Error error
// Classifier is the local error to string classifier. When there is no
// configured classifier we will use the generic classifier.
Classifier func(err error) string
// Operation is the operation that failed
Operation string
}
// MaybeBuild builds a new ErrWrapper, if b.Error is not nil, and returns
// a nil error value, instead, if b.Error is nil.
func (b SafeErrWrapperBuilder) MaybeBuild() (err error) {
if b.Error != nil {
classifier := b.Classifier
if classifier == nil {
classifier = errorsx.ClassifyGenericError
}
err = &errorsx.ErrWrapper{
Failure: classifier(b.Error),
Operation: toOperationString(b.Error, b.Operation),
WrappedErr: b.Error,
}
}
return
}
func toOperationString(err error, operation string) string {
var errwrapper *errorsx.ErrWrapper
if errors.As(err, &errwrapper) {
// Basically, as explained in ErrWrapper docs, let's
// keep the child major operation, if any.
if errwrapper.Operation == errorsx.ConnectOperation {
return errwrapper.Operation
}
if errwrapper.Operation == errorsx.HTTPRoundTripOperation {
return errwrapper.Operation
}
if errwrapper.Operation == errorsx.ResolveOperation {
return errwrapper.Operation
}
if errwrapper.Operation == errorsx.TLSHandshakeOperation {
return errwrapper.Operation
}
if errwrapper.Operation == errorsx.QUICHandshakeOperation {
return errwrapper.Operation
}
if errwrapper.Operation == "quic_handshake_start" {
return errorsx.QUICHandshakeOperation
}
if errwrapper.Operation == "quic_handshake_done" {
return errorsx.QUICHandshakeOperation
}
// FALLTHROUGH
}
return operation
}