Improve error handling/wrapping
This commit is contained in:
parent
bf2fa78afa
commit
042f67bbf6
@ -93,7 +93,7 @@ func (db *DB) Init(storeName string, opts ...any) error {
|
||||
}
|
||||
|
||||
if _, ok := db.store[storeName]; ok {
|
||||
return errStoreExists
|
||||
return ErrStoreExists
|
||||
}
|
||||
path := db.Path()
|
||||
if !strings.HasSuffix(db.Path(), "/") {
|
||||
@ -141,7 +141,7 @@ func (db *DB) Close(storeName string) error {
|
||||
defer db.mu.Unlock()
|
||||
st, ok := db.store[storeName]
|
||||
if !ok {
|
||||
return errBogusStore
|
||||
return ErrBogusStore
|
||||
}
|
||||
err := st.Close()
|
||||
if err != nil {
|
||||
@ -174,12 +174,12 @@ const (
|
||||
func (db *DB) withAll(action withAllAction) error {
|
||||
var errs = make([]error, len(db.store))
|
||||
if len(db.store) < 1 {
|
||||
return errNoStores
|
||||
return ErrNoStores
|
||||
}
|
||||
for name, store := range db.store {
|
||||
var err error
|
||||
if store.Bitcask == nil {
|
||||
errs = append(errs, namedErr(name, errBogusStore))
|
||||
errs = append(errs, namedErr(name, ErrBogusStore))
|
||||
continue
|
||||
}
|
||||
switch action {
|
||||
@ -188,7 +188,7 @@ func (db *DB) withAll(action withAllAction) error {
|
||||
case dsync:
|
||||
err = namedErr(name, store.Sync())
|
||||
default:
|
||||
return errUnknownAction
|
||||
return ErrUnknownAction
|
||||
}
|
||||
if err == nil {
|
||||
continue
|
||||
|
@ -58,7 +58,7 @@ func TestDB_Init(t *testing.T) { //nolint:funlen,gocognit,cyclop
|
||||
name: "storeExists",
|
||||
args: args{"simple"},
|
||||
wantErr: true,
|
||||
specErr: errStoreExists,
|
||||
specErr: ErrStoreExists,
|
||||
},
|
||||
{
|
||||
name: "newStore",
|
||||
@ -221,8 +221,8 @@ func Test_Close(t *testing.T) {
|
||||
|
||||
t.Run("CantCloseBogusStore", func(t *testing.T) {
|
||||
err := db.Close(c.RandStr(55))
|
||||
if !errors.Is(err, errBogusStore) {
|
||||
t.Errorf("[FAIL] got err %e, wanted err %e", err, errBogusStore)
|
||||
if !errors.Is(err, ErrBogusStore) {
|
||||
t.Errorf("[FAIL] got err %e, wanted err %e", err, ErrBogusStore)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -231,8 +231,8 @@ func Test_withAll(t *testing.T) {
|
||||
var db = newTestDB(t)
|
||||
t.Run("withAllNoStores", func(t *testing.T) {
|
||||
err := db.withAll(121)
|
||||
if !errors.Is(err, errNoStores) {
|
||||
t.Errorf("[FAIL] got err %e, wanted err %e", err, errBogusStore)
|
||||
if !errors.Is(err, ErrNoStores) {
|
||||
t.Errorf("[FAIL] got err %e, wanted err %e", err, ErrBogusStore)
|
||||
}
|
||||
})
|
||||
t.Run("withAllBogusAction", func(t *testing.T) {
|
||||
@ -241,8 +241,8 @@ func Test_withAll(t *testing.T) {
|
||||
t.Errorf("[FAIL] unexpected error: %e", err)
|
||||
}
|
||||
wAllErr := db.withAll(121)
|
||||
if !errors.Is(wAllErr, errUnknownAction) {
|
||||
t.Errorf("[FAIL] wanted error %e, got error %e", errUnknownAction, err)
|
||||
if !errors.Is(wAllErr, ErrUnknownAction) {
|
||||
t.Errorf("[FAIL] wanted error %e, got error %e", ErrUnknownAction, err)
|
||||
}
|
||||
})
|
||||
t.Run("ListAll", func(t *testing.T) {
|
||||
|
@ -2,34 +2,31 @@ package bitcask
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/go-multierror"
|
||||
)
|
||||
|
||||
//goland:noinspection GoExportedElementShouldHaveComment
|
||||
var (
|
||||
errUnknownAction = errors.New("unknown action")
|
||||
errBogusStore = errors.New("bogus store backend")
|
||||
errStoreExists = errors.New("store name already exists")
|
||||
errNoStores = errors.New("no stores initialized")
|
||||
ErrUnknownAction = errors.New("unknown action")
|
||||
ErrBogusStore = errors.New("bogus store backend")
|
||||
ErrStoreExists = errors.New("store name already exists")
|
||||
ErrNoStores = errors.New("no stores initialized")
|
||||
)
|
||||
|
||||
func namedErr(name string, err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return errors.New(name + ": " + err.Error())
|
||||
return multierror.Prefix(err, name)
|
||||
}
|
||||
|
||||
func compoundErrors(errs []error) error {
|
||||
var errstrs []string
|
||||
var isnil = true
|
||||
for _, err := range errs {
|
||||
if err != nil {
|
||||
isnil = false
|
||||
errstrs = append(errstrs, err.Error())
|
||||
func compoundErrors(errs []error) (err error) {
|
||||
for _, e := range errs {
|
||||
if e == nil {
|
||||
continue
|
||||
}
|
||||
err = multierror.Append(err, e)
|
||||
}
|
||||
if isnil {
|
||||
return nil
|
||||
}
|
||||
return errors.New(strings.Join(errstrs, ","))
|
||||
return
|
||||
}
|
||||
|
2
go.mod
2
go.mod
@ -6,11 +6,13 @@ require (
|
||||
git.tcp.direct/Mirrors/bitcask-mirror v0.0.0-20220228092422-1ec4297c7e34
|
||||
git.tcp.direct/kayos/common v0.7.1
|
||||
github.com/davecgh/go-spew v1.1.1
|
||||
github.com/hashicorp/go-multierror v1.0.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/abcum/lcp v0.0.0-20201209214815-7a3f3840be81 // indirect
|
||||
github.com/gofrs/flock v0.8.0 // indirect
|
||||
github.com/hashicorp/errwrap v1.0.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
|
2
go.sum
2
go.sum
@ -176,10 +176,12 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t
|
||||
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
|
||||
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
|
||||
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
|
||||
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
|
||||
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
|
||||
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
|
||||
github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
|
||||
github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o=
|
||||
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
|
||||
github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU=
|
||||
github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU=
|
||||
|
Loading…
Reference in New Issue
Block a user