From 88bcdb254d548c0c8d0c19001062de3ac9d73fbb Mon Sep 17 00:00:00 2001 From: Simone Basso Date: Mon, 27 Sep 2021 14:55:47 +0200 Subject: [PATCH] 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. --- internal/netxlite/errorsx/errwrapper.go | 6 ++++++ internal/netxlite/errorsx/errwrapper_test.go | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/internal/netxlite/errorsx/errwrapper.go b/internal/netxlite/errorsx/errwrapper.go index 81c3ac2bba..a814b6eaeb 100644 --- a/internal/netxlite/errorsx/errwrapper.go +++ b/internal/netxlite/errorsx/errwrapper.go @@ -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) +} diff --git a/internal/netxlite/errorsx/errwrapper_test.go b/internal/netxlite/errorsx/errwrapper_test.go index d7986b82c7..68a643778a 100644 --- a/internal/netxlite/errorsx/errwrapper_test.go +++ b/internal/netxlite/errorsx/errwrapper_test.go @@ -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") + } +}