Commit 473787bf authored by Steven Allen's avatar Steven Allen

feat: make not-found errors discoverable

Now we just need a common place to put:

func IsNotFound(e error) bool {
  for e != nil {
    if ne, ok := e.(interface { NotFound() bool }); ok {
      return ne.NotFound()
    }
    if ue, ok := e.(interface { Unwrap() error }); ok {
      e = ue.Unwrap()
    } else {
      return false
    }
  }
  return false
}
parent 507733cb
......@@ -192,14 +192,18 @@ type TxnDatastore interface {
// Errors
type dsError struct {
error
isNotFound bool
}
func (e *dsError) NotFound() bool {
return e.isNotFound
}
// ErrNotFound is returned by Get and GetSize when a datastore does not map the
// given key to a value.
var ErrNotFound = errors.New("datastore: key not found")
// ErrInvalidType is returned by Put when a given value is incopatible with
// the type the datastore supports. This means a conversion (or serialization)
// is needed beforehand.
var ErrInvalidType = errors.New("datastore: invalid type error")
var ErrNotFound error = &dsError{error: errors.New("datastore: key not found"), isNotFound: true}
// GetBackedHas provides a default Datastore.Has implementation.
// It exists so Datastore.Has implementations can use it, like so:
......
......@@ -6,5 +6,6 @@ require (
github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8
github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8
github.com/kr/pretty v0.1.0 // indirect
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)
......@@ -11,5 +11,7 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
......@@ -5,13 +5,13 @@ package mount
import (
"container/heap"
"errors"
"fmt"
"sort"
"strings"
"sync"
ds "github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
xerrors "golang.org/x/xerrors"
)
var (
......@@ -370,7 +370,7 @@ func (d *Datastore) Check() error {
for _, m := range d.mounts {
if c, ok := m.Datastore.(ds.CheckedDatastore); ok {
if err := c.Check(); err != nil {
return fmt.Errorf("checking datastore at %s: %s", m.Prefix.String(), err.Error())
return xerrors.Errorf("checking datastore at %s: %w", m.Prefix.String(), err)
}
}
}
......@@ -381,7 +381,7 @@ func (d *Datastore) Scrub() error {
for _, m := range d.mounts {
if c, ok := m.Datastore.(ds.ScrubbedDatastore); ok {
if err := c.Scrub(); err != nil {
return fmt.Errorf("scrubbing datastore at %s: %s", m.Prefix.String(), err.Error())
return xerrors.Errorf("scrubbing datastore at %s: %w", m.Prefix.String(), err)
}
}
}
......@@ -392,7 +392,7 @@ func (d *Datastore) CollectGarbage() error {
for _, m := range d.mounts {
if c, ok := m.Datastore.(ds.GCDatastore); ok {
if err := c.CollectGarbage(); err != nil {
return fmt.Errorf("gc on datastore at %s: %s", m.Prefix.String(), err.Error())
return xerrors.Errorf("gc on datastore at %s: %w", m.Prefix.String(), err)
}
}
}
......
......@@ -3,10 +3,10 @@
package retrystore
import (
"fmt"
"time"
ds "github.com/ipfs/go-datastore"
xerrors "golang.org/x/xerrors"
)
// Datastore wraps a Batching datastore with a
......@@ -23,7 +23,7 @@ type Datastore struct {
ds.Batching
}
var errFmtString = "ran out of retries trying to get past temporary error: %s"
var errFmtString = "ran out of retries trying to get past temporary error: %w"
func (d *Datastore) runOp(op func() error) error {
err := op()
......@@ -40,7 +40,7 @@ func (d *Datastore) runOp(op func() error) error {
}
}
return fmt.Errorf(errFmtString, err)
return xerrors.Errorf(errFmtString, err)
}
// DiskUsage implements the PersistentDatastore interface.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment