1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-08 14:07:02 +09:00

Add verbose errors

This commit is contained in:
mcrakhman 2024-05-16 13:28:32 +02:00
parent 1e65e90fe7
commit f8b49b36b5
No known key found for this signature in database
GPG key ID: DED12CFEF5B8396B
3 changed files with 16 additions and 7 deletions

View file

@ -4,14 +4,16 @@ package objecttree
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"sync" "sync"
"time" "time"
"github.com/anyproto/any-sync/util/crypto" "go.uber.org/zap"
"github.com/anyproto/any-sync/commonspace/object/acl/list" "github.com/anyproto/any-sync/commonspace/object/acl/list"
"github.com/anyproto/any-sync/commonspace/object/tree/treechangeproto" "github.com/anyproto/any-sync/commonspace/object/tree/treechangeproto"
"github.com/anyproto/any-sync/commonspace/object/tree/treestorage" "github.com/anyproto/any-sync/commonspace/object/tree/treestorage"
"github.com/anyproto/any-sync/util/crypto"
"github.com/anyproto/any-sync/util/slice" "github.com/anyproto/any-sync/util/slice"
) )
@ -124,6 +126,7 @@ func (ot *objectTree) rebuildFromStorage(theirHeads []string, newChanges []*Chan
ot.treeBuilder.Reset() ot.treeBuilder.Reset()
ot.tree, err = ot.treeBuilder.Build(theirHeads, newChanges) ot.tree, err = ot.treeBuilder.Build(theirHeads, newChanges)
if err != nil { if err != nil {
ot.tree = oldTree
return return
} }
@ -434,15 +437,20 @@ func (ot *objectTree) addRawChanges(ctx context.Context, changesPayload RawChang
err = ot.rebuildFromStorage(headsToUse, ot.newChangesBuf) err = ot.rebuildFromStorage(headsToUse, ot.newChangesBuf)
if err != nil { if err != nil {
// rebuilding without new changes // rebuilding without new changes
ot.rebuildFromStorage(nil, nil) rebuildErr := ot.rebuildFromStorage(nil, nil)
if rebuildErr != nil {
log.Error("failed to rebuild", zap.String("treeId", ot.id), zap.Strings("heads", ot.Heads()), zap.Error(err))
}
return return
} }
addResult, err = ot.createAddResult(prevHeadsCopy, Rebuild, nil, changesPayload.RawChanges) addResult, err = ot.createAddResult(prevHeadsCopy, Rebuild, nil, changesPayload.RawChanges)
if err != nil { if err != nil {
// that means that some unattached changes were somehow corrupted in memory // that means that some unattached changes were somehow corrupted in memory
// this shouldn't happen but if that happens, then rebuilding from storage // this shouldn't happen but if that happens, then rebuilding from storage
ot.rebuildFromStorage(nil, nil) rebuildErr := ot.rebuildFromStorage(nil, nil)
return if rebuildErr != nil {
log.Error("failed to rebuild after add result", zap.String("treeId", ot.id), zap.Strings("heads", ot.Heads()), zap.Error(err))
}
} }
return return
} }
@ -463,7 +471,7 @@ func (ot *objectTree) addRawChanges(ctx context.Context, changesPayload RawChang
err = ot.validateTree(treeChangesAdded) err = ot.validateTree(treeChangesAdded)
if err != nil { if err != nil {
rollback(treeChangesAdded) rollback(treeChangesAdded)
err = ErrHasInvalidChanges err = fmt.Errorf("%w: %w", ErrHasInvalidChanges, err)
return return
} }
addResult, err = ot.createAddResult(prevHeadsCopy, mode, treeChangesAdded, changesPayload.RawChanges) addResult, err = ot.createAddResult(prevHeadsCopy, mode, treeChangesAdded, changesPayload.RawChanges)

View file

@ -2,6 +2,7 @@ package objecttree
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"testing" "testing"
"time" "time"
@ -1228,6 +1229,6 @@ func TestObjectTree(t *testing.T) {
Heads: []string{"3"}, Heads: []string{"3"},
Changes: rawChanges, Changes: rawChanges,
}, ctx.aclList) }, ctx.aclList)
require.Equal(t, ErrHasInvalidChanges, err) require.True(t, errors.Is(err, ErrHasInvalidChanges))
}) })
} }

View file

@ -170,7 +170,7 @@ func ValidateRawTreeBuildFunc(payload treestorage.TreeStorageCreatePayload, buil
return return
} }
if !slice.UnsortedEquals(res.Heads, payload.Heads) { if !slice.UnsortedEquals(res.Heads, payload.Heads) {
return payload, ErrHasInvalidChanges return payload, fmt.Errorf("heads mismatch: %v != %v, %w", res.Heads, payload.Heads, ErrHasInvalidChanges)
} }
// if tree has only one change we still should check if the snapshot id is same as root // if tree has only one change we still should check if the snapshot id is same as root
if IsEmptyDerivedTree(tree) { if IsEmptyDerivedTree(tree) {