fix(netxlite): ensure HTTP errors are always wrapped (#584)
1. introduce implementations of HTTPTransport and HTTPClient that apply an error wrapping policy using the constructor for a generic top-level error wrapper 2. make sure we use the implementations in point 1 when we are constructing HTTPTransport and HTTPClient 3. make sure we apply error wrapping using the constructor for a generic top-level error wrapper when reading bodies 4. acknowledge that error wrapping would be broken if we do not return the same classification _and_ operation when we wrap an already wrapped error, so fix the to code to do that 5. acknowledge that the classifiers already deal with preserving the error string and explain why this is a quirk and why we cannot remove it right now and what needs to happen to safely remove this quirk from the codebase Closes https://github.com/ooni/probe/issues/1860
This commit is contained in:
parent
da34cfe6c9
commit
6a935d5407
9 changed files with 217 additions and 28 deletions
|
|
@ -1,6 +1,9 @@
|
|||
package netxlite
|
||||
|
||||
import "encoding/json"
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// ErrWrapper is our error wrapper for Go errors. The key objective of
|
||||
// this structure is to properly set Failure, which is also returned by
|
||||
|
|
@ -78,7 +81,19 @@ type Classifier func(err error) string
|
|||
//
|
||||
// This function panics if classifier is nil, or operation
|
||||
// is the empty string or error is nil.
|
||||
//
|
||||
// If the err argument has already been classified, the returned
|
||||
// error wrapper will use the same classification string and
|
||||
// failed operation of the original wrapped error.
|
||||
func NewErrWrapper(c Classifier, op string, err error) *ErrWrapper {
|
||||
var wrapper *ErrWrapper
|
||||
if errors.As(err, &wrapper) {
|
||||
return &ErrWrapper{
|
||||
Failure: wrapper.Failure,
|
||||
Operation: wrapper.Operation,
|
||||
WrappedErr: err,
|
||||
}
|
||||
}
|
||||
if c == nil {
|
||||
panic("nil classifier")
|
||||
}
|
||||
|
|
@ -97,6 +112,10 @@ func NewErrWrapper(c Classifier, op string, err error) *ErrWrapper {
|
|||
|
||||
// NewTopLevelGenericErrWrapper wraps an error occurring at top
|
||||
// level using ClassifyGenericError as classifier.
|
||||
//
|
||||
// If the err argument has already been classified, the returned
|
||||
// error wrapper will use the same classification string and
|
||||
// failed operation of the original error.
|
||||
func NewTopLevelGenericErrWrapper(err error) *ErrWrapper {
|
||||
return NewErrWrapper(ClassifyGenericError, TopLevelOperation, err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue