[#1969] local_object_storage: Add a type for logical errors

All logic errors are wrapped in `logicerr.Logical` type and do not
affect shard error counter.

Signed-off-by: Evgenii Stratonikov <evgeniy@morphbits.ru>
This commit is contained in:
Evgenii Stratonikov 2022-10-26 15:23:12 +03:00 committed by fyrchik
parent 98034005f1
commit fcdbf5e509
42 changed files with 206 additions and 139 deletions

View file

@ -0,0 +1,21 @@
package logicerr
// Logical is a wrapper for logical errors.
type Logical struct {
err error
}
// Error implements the error interface.
func (e Logical) Error() string {
return e.err.Error()
}
// Wrap wraps arbitrary error into a logical one.
func Wrap(err error) Logical {
return Logical{err: err}
}
// Unwrap returns underlying error.
func (e Logical) Unwrap() error {
return e.err
}

View file

@ -0,0 +1,58 @@
package logicerr
import (
"errors"
"fmt"
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
func TestError(t *testing.T) {
t.Run("errors.Is", func(t *testing.T) {
e1 := errors.New("some error")
ee := Wrap(e1)
require.ErrorIs(t, ee, e1)
e2 := fmt.Errorf("wrap: %w", e1)
ee = Wrap(e2)
require.ErrorIs(t, ee, e1)
require.ErrorIs(t, ee, e2)
require.Equal(t, errors.Unwrap(ee), e2)
})
t.Run("errors.As", func(t *testing.T) {
e1 := testError{42}
ee := Wrap(e1)
{
var actual testError
require.ErrorAs(t, ee, &actual)
require.Equal(t, e1.data, actual.data)
}
{
var actual Logical
require.ErrorAs(t, ee, &actual)
require.Equal(t, e1, actual.err)
}
e2 := fmt.Errorf("wrap: %w", e1)
ee = Wrap(e2)
{
var actual testError
require.ErrorAs(t, ee, &actual)
require.Equal(t, e1.data, actual.data)
}
})
}
type testError struct {
data uint64
}
func (e testError) Error() string {
return strconv.FormatUint(e.data, 10)
}