2021-07-01 16:34:36 +02:00
|
|
|
// Package errorsx contains error extensions.
|
|
|
|
package errorsx
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-06-22 14:08:29 +02:00
|
|
|
|
2021-09-07 17:09:30 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2021-06-23 11:32:53 +02:00
|
|
|
// 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
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
// 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 {
|
2021-06-23 11:32:53 +02:00
|
|
|
classifier := b.Classifier
|
|
|
|
if classifier == nil {
|
2021-09-07 17:09:30 +02:00
|
|
|
classifier = errorsx.ClassifyGenericError
|
2021-06-23 11:32:53 +02:00
|
|
|
}
|
2021-09-07 17:09:30 +02:00
|
|
|
err = &errorsx.ErrWrapper{
|
2021-06-23 13:36:45 +02:00
|
|
|
Failure: classifier(b.Error),
|
|
|
|
Operation: toOperationString(b.Error, b.Operation),
|
|
|
|
WrappedErr: b.Error,
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func toOperationString(err error, operation string) string {
|
2021-09-07 17:09:30 +02:00
|
|
|
var errwrapper *errorsx.ErrWrapper
|
2021-02-02 12:05:47 +01:00
|
|
|
if errors.As(err, &errwrapper) {
|
|
|
|
// Basically, as explained in ErrWrapper docs, let's
|
|
|
|
// keep the child major operation, if any.
|
2021-09-07 17:09:30 +02:00
|
|
|
if errwrapper.Operation == errorsx.ConnectOperation {
|
2021-02-02 12:05:47 +01:00
|
|
|
return errwrapper.Operation
|
|
|
|
}
|
2021-09-07 17:09:30 +02:00
|
|
|
if errwrapper.Operation == errorsx.HTTPRoundTripOperation {
|
2021-02-02 12:05:47 +01:00
|
|
|
return errwrapper.Operation
|
|
|
|
}
|
2021-09-07 17:09:30 +02:00
|
|
|
if errwrapper.Operation == errorsx.ResolveOperation {
|
2021-02-02 12:05:47 +01:00
|
|
|
return errwrapper.Operation
|
|
|
|
}
|
2021-09-07 17:09:30 +02:00
|
|
|
if errwrapper.Operation == errorsx.TLSHandshakeOperation {
|
2021-02-02 12:05:47 +01:00
|
|
|
return errwrapper.Operation
|
|
|
|
}
|
2021-09-07 17:09:30 +02:00
|
|
|
if errwrapper.Operation == errorsx.QUICHandshakeOperation {
|
2021-02-02 12:05:47 +01:00
|
|
|
return errwrapper.Operation
|
|
|
|
}
|
|
|
|
if errwrapper.Operation == "quic_handshake_start" {
|
2021-09-07 17:09:30 +02:00
|
|
|
return errorsx.QUICHandshakeOperation
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
if errwrapper.Operation == "quic_handshake_done" {
|
2021-09-07 17:09:30 +02:00
|
|
|
return errorsx.QUICHandshakeOperation
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
// FALLTHROUGH
|
|
|
|
}
|
|
|
|
return operation
|
|
|
|
}
|