feat(errorsx): introduce NewTopLevelGenericErrWrapper (#511)

Part of https://github.com/ooni/probe/issues/1733 and diff has been
extracted from https://github.com/ooni/probe-cli/pull/506.
This commit is contained in:
Simone Basso 2021-09-27 14:55:47 +02:00 committed by GitHub
parent d7b9c8f0a8
commit 201f602a40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -92,3 +92,9 @@ func NewErrWrapper(c Classifier, op string, err error) *ErrWrapper {
WrappedErr: err,
}
}
// NewTopLevelGenericErrWrapper wraps an error occurring at top
// level using the most generic available classifier.
func NewTopLevelGenericErrWrapper(err error) *ErrWrapper {
return NewErrWrapper(ClassifyGenericError, TopLevelOperation, err)
}

View File

@ -102,3 +102,19 @@ func TestNewErrWrapper(t *testing.T) {
}
})
}
func TestNewTopLevelGenericErrWrapper(t *testing.T) {
out := NewTopLevelGenericErrWrapper(io.EOF)
if out.Failure != FailureEOFError {
t.Fatal("invalid failure")
}
if out.Operation != TopLevelOperation {
t.Fatal("invalid operation")
}
if !errors.Is(out, io.EOF) {
t.Fatal("invalid underlying error using errors.Is")
}
if !errors.Is(out.WrappedErr, io.EOF) {
t.Fatal("invalid WrappedErr")
}
}