Skip to content

Commit

Permalink
Include field name in missing field error
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Sep 16, 2022
1 parent fc988de commit d80e00b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
10 changes: 5 additions & 5 deletions client/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (doc *Document) GetValue(field string) (Value, error) {
path, subPaths, hasSubPaths := parseFieldPath(field)
f, exists := doc.fields[path]
if !exists {
return nil, ErrFieldNotExist
return nil, ErrFieldNotExist(path)
}

val, err := doc.GetValueWithField(f)
Expand All @@ -200,7 +200,7 @@ func (doc *Document) GetValueWithField(f Field) (Value, error) {
defer doc.mu.RUnlock()
v, exists := doc.values[f]
if !exists {
return nil, ErrFieldNotExist
return nil, ErrFieldNotExist(f.Name())
}
return v, nil
}
Expand Down Expand Up @@ -246,7 +246,7 @@ func (doc *Document) Delete(fields ...string) error {
for _, f := range fields {
field, exists := doc.fields[f]
if !exists {
return ErrFieldNotExist
return ErrFieldNotExist(f)
}
doc.values[field].Delete()
}
Expand Down Expand Up @@ -432,7 +432,7 @@ func (doc *Document) toMap() (map[string]any, error) {
for k, v := range doc.fields {
value, exists := doc.values[v]
if !exists {
return nil, ErrFieldNotExist
return nil, ErrFieldNotExist(v.Name())
}

if value.IsDocument() {
Expand All @@ -457,7 +457,7 @@ func (doc *Document) toMapWithKey() (map[string]any, error) {
for k, v := range doc.fields {
value, exists := doc.values[v]
if !exists {
return nil, ErrFieldNotExist
return nil, ErrFieldNotExist(v.Name())
}

if value.IsDocument() {
Expand Down
5 changes: 4 additions & 1 deletion client/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import "github.com/sourcenetwork/defradb/errors"
// This list is incomplete and undefined errors may also be returned.
// Errors returned from this package may be tested against these errors with errors.Is.
var (
ErrFieldNotExist = errors.New("The given field does not exist")
ErrFieldNotObject = errors.New("Trying to access field on a non object type")
ErrValueTypeMismatch = errors.New("Value does not match indicated type")
ErrIndexNotFound = errors.New("No index found for given ID")
Expand All @@ -26,3 +25,7 @@ var (
ErrInvalidUpdater = errors.New("The updater of a document is of invalid type")
ErrInvalidDeleteTarget = errors.New("The target document to delete is of invalid type")
)

func ErrFieldNotExist(name string) error {
return errors.New("The given field does not exist", errors.NewKV("Name", name))
}
2 changes: 1 addition & 1 deletion db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ func (c *collection) save(
if val.IsDirty() {
fieldKey, fieldExists := c.tryGetFieldKey(primaryKey, k)
if !fieldExists {
return cid.Undef, client.ErrFieldNotExist
return cid.Undef, client.ErrFieldNotExist(k)
}

c, err := c.saveDocValue(ctx, txn, fieldKey, val)
Expand Down
2 changes: 1 addition & 1 deletion db/collection_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func (c *collection) applyMerge(
val := client.NewCBORValue(fd.Typ, cborVal)
fieldKey, fieldExists := c.tryGetFieldKey(key, mfield)
if !fieldExists {
return client.ErrFieldNotExist
return client.ErrFieldNotExist(mfield)
}

c, err := c.saveDocValue(ctx, txn, fieldKey, val)
Expand Down

0 comments on commit d80e00b

Please sign in to comment.