diff --git a/core/domain/errors.go b/core/domain/errors.go new file mode 100644 index 0000000000..745bfd54bd --- /dev/null +++ b/core/domain/errors.go @@ -0,0 +1,5 @@ +package domain + +import "errors" + +var ErrFileNotFound = errors.New("file not found") diff --git a/core/files/files.go b/core/files/files.go index 9132bf37b7..cc5de013f6 100644 --- a/core/files/files.go +++ b/core/files/files.go @@ -23,6 +23,7 @@ import ( "github.com/multiformats/go-base32" mh "github.com/multiformats/go-multihash" + "github.com/anyproto/anytype-heart/core/domain" "github.com/anyproto/anytype-heart/core/filestorage" "github.com/anyproto/anytype-heart/core/filestorage/filesync" "github.com/anyproto/anytype-heart/pb" @@ -853,15 +854,13 @@ func (s *service) StoreFileKeys(fileKeys ...FileKeys) error { return s.fileStore.AddFileKeys(fks...) } -var ErrFileNotFound = fmt.Errorf("file not found") - func (s *service) FileByHash(ctx context.Context, hash string) (File, error) { ok, err := s.isDeleted(hash) if err != nil { return nil, fmt.Errorf("check if file is deleted: %w", err) } if ok { - return nil, ErrFileNotFound + return nil, domain.ErrFileNotFound } fileList, err := s.fileStore.ListByTarget(hash) @@ -874,7 +873,7 @@ func (s *service) FileByHash(ctx context.Context, hash string) (File, error) { fileList, err = s.fileIndexInfo(ctx, hash, false) if err != nil { log.With("cid", hash).Errorf("FileByHash: failed to retrieve from IPFS: %s", err.Error()) - return nil, ErrFileNotFound + return nil, domain.ErrFileNotFound } ok, err := s.fileStore.IsFileImported(hash) if err != nil { diff --git a/core/files/image.go b/core/files/image.go index 4d7216b622..f63804145e 100644 --- a/core/files/image.go +++ b/core/files/image.go @@ -11,6 +11,7 @@ import ( "github.com/gogo/protobuf/types" + "github.com/anyproto/anytype-heart/core/domain" "github.com/anyproto/anytype-heart/pkg/lib/bundle" "github.com/anyproto/anytype-heart/pkg/lib/mill" "github.com/anyproto/anytype-heart/pkg/lib/pb/model" @@ -262,7 +263,7 @@ func (i *image) getFileForWidthFromCache(wantWidth int) (File, error) { }, nil } - return nil, ErrFileNotFound + return nil, domain.ErrFileNotFound } func (i *image) extractLastModifiedDate(ctx context.Context, imageExif *mill.ImageExifSchema) int64 { diff --git a/core/files/images.go b/core/files/images.go index 964963ef06..a96872545b 100644 --- a/core/files/images.go +++ b/core/files/images.go @@ -4,20 +4,19 @@ import ( "context" "fmt" + "github.com/anyproto/anytype-heart/core/domain" "github.com/anyproto/anytype-heart/pkg/lib/localstore/filestore" "github.com/anyproto/anytype-heart/pkg/lib/mill/schema/anytype" "github.com/anyproto/anytype-heart/pkg/lib/pb/storage" ) -var ErrImageNotFound = fmt.Errorf("image not found") - func (s *service) ImageByHash(ctx context.Context, hash string) (Image, error) { ok, err := s.isDeleted(hash) if err != nil { return nil, fmt.Errorf("check if file is deleted: %w", err) } if ok { - return nil, ErrFileNotFound + return nil, domain.ErrFileNotFound } files, err := s.fileStore.ListByTarget(hash) @@ -31,7 +30,7 @@ func (s *service) ImageByHash(ctx context.Context, hash string) (Image, error) { files, err = s.fileIndexInfo(ctx, hash, true) if err != nil { log.Errorf("ImageByHash: failed to retrieve from IPFS: %s", err.Error()) - return nil, ErrImageNotFound + return nil, domain.ErrFileNotFound } } diff --git a/core/filestorage/filesync/stats.go b/core/filestorage/filesync/stats.go index 61e55bfdf7..1058d21fc0 100644 --- a/core/filestorage/filesync/stats.go +++ b/core/filestorage/filesync/stats.go @@ -13,6 +13,7 @@ import ( "github.com/samber/lo" "go.uber.org/zap" + "github.com/anyproto/anytype-heart/core/domain" "github.com/anyproto/anytype-heart/pb" "github.com/anyproto/anytype-heart/util/conc" ) @@ -139,7 +140,7 @@ func (f *fileSync) FileStat(ctx context.Context, spaceId, fileId string) (fs Fil return } if len(fi) == 0 { - return FileStat{}, fmt.Errorf("file not found") + return FileStat{}, domain.ErrFileNotFound } file := fi[0] diff --git a/core/indexer/indexer.go b/core/indexer/indexer.go index d3988a34f8..a91a56e65d 100644 --- a/core/indexer/indexer.go +++ b/core/indexer/indexer.go @@ -20,6 +20,7 @@ import ( "github.com/anyproto/anytype-heart/core/block/editor" smartblock2 "github.com/anyproto/anytype-heart/core/block/editor/smartblock" "github.com/anyproto/anytype-heart/core/block/source" + "github.com/anyproto/anytype-heart/core/domain" "github.com/anyproto/anytype-heart/core/files" "github.com/anyproto/anytype-heart/core/relation/relationutils" "github.com/anyproto/anytype-heart/metrics" @@ -318,8 +319,8 @@ func (i *indexer) indexLinkedFiles(ctx context.Context, fileHashes []string) { } // file's hash is id idxErr := i.reindexDoc(ctx, id) - if idxErr != nil { - log.With("id", id).Errorf("failed to reindex file: %s", idxErr.Error()) + if idxErr != nil && !errors.Is(idxErr, domain.ErrFileNotFound) { + log.With("id", id).Errorf("failed to reindex file: %s", idxErr) } idxErr = i.store.AddToIndexQueue(id) if idxErr != nil { diff --git a/core/syncstatus/file_status_registry.go b/core/syncstatus/file_status_registry.go index 978a02ce69..6d83eb9a85 100644 --- a/core/syncstatus/file_status_registry.go +++ b/core/syncstatus/file_status_registry.go @@ -2,12 +2,14 @@ package syncstatus import ( "context" + "errors" "fmt" "sync" "time" "github.com/anyproto/anytype-heart/core/block/editor/basic" "github.com/anyproto/anytype-heart/core/block/getblock" + "github.com/anyproto/anytype-heart/core/domain" "github.com/anyproto/anytype-heart/core/filestorage/filesync" "github.com/anyproto/anytype-heart/pb" "github.com/anyproto/anytype-heart/pkg/lib/bundle" @@ -127,8 +129,6 @@ func validStatusTransition(from, to FileStatus) bool { } } -var errFileNotFound = fmt.Errorf("file is not found") - func (r *fileStatusRegistry) updateFileStatus(ctx context.Context, status fileStatus, key fileWithSpace) (fileStatus, error) { now := time.Now() if status.status == FileStatusSynced { @@ -163,7 +163,7 @@ func (r *fileStatusRegistry) updateFileStatus(ctx context.Context, status fileSt return status, fmt.Errorf("check that file is in store: %w", err) } if !ok { - return status, errFileNotFound + return status, domain.ErrFileNotFound } fstat, err := r.fileSyncService.FileStat(ctx, key.spaceID, key.fileID) if err != nil { @@ -185,7 +185,7 @@ func (r *fileStatusRegistry) indexFileSyncStatus(fileID string, status FileStatu }, }, true) }) - if err != nil { + if err != nil && !errors.Is(err, domain.ErrFileNotFound) { log.With("fileID", fileID, "status", status).Errorf("failed to index file sync status: %v", err) } } diff --git a/core/syncstatus/file_watcher.go b/core/syncstatus/file_watcher.go index 5c5fc10573..8ae3626758 100644 --- a/core/syncstatus/file_watcher.go +++ b/core/syncstatus/file_watcher.go @@ -3,6 +3,7 @@ package syncstatus import ( "bytes" "context" + "errors" "fmt" "sort" "sync" @@ -12,6 +13,7 @@ import ( "github.com/dgraph-io/badger/v3" "go.uber.org/zap" + "github.com/anyproto/anytype-heart/core/domain" "github.com/anyproto/anytype-heart/pkg/lib/datastore" "github.com/anyproto/anytype-heart/space" ) @@ -164,7 +166,7 @@ func (s *fileWatcher) list() []fileWithSpace { func (s *fileWatcher) updateFileStatus(ctx context.Context, key fileWithSpace) error { status, err := s.registry.GetFileStatus(ctx, key.spaceID, key.fileID) - if err == errFileNotFound { + if errors.Is(err, domain.ErrFileNotFound) { s.Unwatch(key.spaceID, key.fileID) return err } diff --git a/core/syncstatus/linked_files_watcher.go b/core/syncstatus/linked_files_watcher.go index 39752ad4ed..2424b4879e 100644 --- a/core/syncstatus/linked_files_watcher.go +++ b/core/syncstatus/linked_files_watcher.go @@ -2,11 +2,13 @@ package syncstatus import ( "context" + "errors" "sync" "time" "go.uber.org/zap" + "github.com/anyproto/anytype-heart/core/domain" "github.com/anyproto/anytype-heart/pb" "github.com/anyproto/anytype-heart/space" ) @@ -88,7 +90,7 @@ func (w *linkedFilesWatcher) updateLinkedFilesSummary(parentObjectID string, fil var pinStatus pb.EventStatusThreadCafePinStatus for _, fileID := range fileIDs { status, err := w.fileStatusRegistry.GetFileStatus(context.Background(), w.spaceService.AccountId(), fileID) - if err == errFileNotFound { + if errors.Is(err, domain.ErrFileNotFound) { continue } if err != nil { diff --git a/go.mod b/go.mod index 411589b091..27996e6b22 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/PuerkitoBio/goquery v1.8.1 github.com/VividCortex/ewma v1.2.0 github.com/adrium/goheif v0.0.0-20230113233934-ca402e77a786 - github.com/anyproto/any-sync v0.2.13 + github.com/anyproto/any-sync v0.2.14 github.com/anyproto/go-naturaldate/v2 v2.0.2-0.20230524105841-9829cfd13438 github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de github.com/blevesearch/bleve/v2 v2.3.9 diff --git a/go.sum b/go.sum index 96bcdb902e..53507d2657 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,8 @@ github.com/andybalholm/cascadia v1.2.0/go.mod h1:YCyR8vOZT9aZ1CHEd8ap0gMVm2aFgxB github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c= github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= -github.com/anyproto/any-sync v0.2.13 h1:g6nVGRZvg6jzYYuenKXgTNBXvaJ+ibDDUmQJ47PtSe8= -github.com/anyproto/any-sync v0.2.13/go.mod h1:DSylRQYM/tTWZ8B6hZwsOFgLFupakVvhbZh7thE8V+g= +github.com/anyproto/any-sync v0.2.14 h1:nu9y+JkeD8UQXpRMDqRK9PyfEhCFaMhzXSlUoG/gmqw= +github.com/anyproto/any-sync v0.2.14/go.mod h1:DSylRQYM/tTWZ8B6hZwsOFgLFupakVvhbZh7thE8V+g= github.com/anyproto/go-chash v0.1.0 h1:I9meTPjXFRfXZHRJzjOHC/XF7Q5vzysKkiT/grsogXY= github.com/anyproto/go-chash v0.1.0/go.mod h1:0UjNQi3PDazP0fINpFYu6VKhuna+W/V+1vpXHAfNgLY= github.com/anyproto/go-ds-badger3 v0.3.1-0.20230524095230-434cf6346d9b h1:SMizb43hfILk2bpMgpTd30n6yQQdxW0ZbDti0wqfsBw= diff --git a/pkg/lib/gateway/gateway.go b/pkg/lib/gateway/gateway.go index d2576b84f0..cb04145f40 100644 --- a/pkg/lib/gateway/gateway.go +++ b/pkg/lib/gateway/gateway.go @@ -15,6 +15,7 @@ import ( "github.com/anyproto/any-sync/app" + "github.com/anyproto/anytype-heart/core/domain" "github.com/anyproto/anytype-heart/core/files" "github.com/anyproto/anytype-heart/pb" "github.com/anyproto/anytype-heart/pkg/lib/logging" @@ -224,7 +225,7 @@ func (g *gateway) fileHandler(w http.ResponseWriter, r *http.Request) { file, reader, err := g.getFile(ctx, r) if err != nil { log.With("path", r.URL.Path).Errorf("error getting file: %s", err) - if strings.Contains(err.Error(), "file not found") { + if errors.Is(err, domain.ErrFileNotFound) { http.NotFound(w, r) return } @@ -271,7 +272,7 @@ func (g *gateway) imageHandler(w http.ResponseWriter, r *http.Request) { file, reader, err := g.getImage(ctx, r) if err != nil { log.With("path", r.URL.Path).Errorf("error getting image: %s", err) - if strings.Contains(err.Error(), "file not found") { + if errors.Is(err, domain.ErrFileNotFound) { http.NotFound(w, r) return }