Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 13 additions & 34 deletions docstore/docstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,13 @@ func (e ActionListError) Error() string {
return strings.Join(s, "; ")
}

// Unwrap returns the error in e, if there is exactly one. If there is more than one
// error, Unwrap returns nil, since there is no way to determine which should be
// returned.
func (e ActionListError) Unwrap() error {
if len(e) == 1 {
return e[0].Err
}
// Return nil when e is nil, or has more than one error.
// When there are multiple errors, it doesn't make sense to return any of them.
return nil
// Unwrap returns all underlying errors from e.
func (e ActionListError) Unwrap() []error {
errs := []error{}
for _, err := range e {
errs = append(errs, err.Err)
}
return errs
}

// BeforeDo takes a callback function that will be called before the ActionList is
Expand Down Expand Up @@ -558,55 +555,37 @@ func (a *Action) String() string {
// Create is a convenience for building and running a single-element action list.
// See ActionList.Create.
func (c *Collection) Create(ctx context.Context, doc Document) error {
if err := c.Actions().Create(doc).Do(ctx); err != nil {
return err.(ActionListError).Unwrap()
}
return nil
return c.Actions().Create(doc).Do(ctx)
}

// Replace is a convenience for building and running a single-element action list.
// See ActionList.Replace.
func (c *Collection) Replace(ctx context.Context, doc Document) error {
if err := c.Actions().Replace(doc).Do(ctx); err != nil {
return err.(ActionListError).Unwrap()
}
return nil
return c.Actions().Replace(doc).Do(ctx)
}

// Put is a convenience for building and running a single-element action list.
// See ActionList.Put.
func (c *Collection) Put(ctx context.Context, doc Document) error {
if err := c.Actions().Put(doc).Do(ctx); err != nil {
return err.(ActionListError).Unwrap()
}
return nil
return c.Actions().Put(doc).Do(ctx)
}

// Delete is a convenience for building and running a single-element action list.
// See ActionList.Delete.
func (c *Collection) Delete(ctx context.Context, doc Document) error {
if err := c.Actions().Delete(doc).Do(ctx); err != nil {
return err.(ActionListError).Unwrap()
}
return nil
return c.Actions().Delete(doc).Do(ctx)
}

// Get is a convenience for building and running a single-element action list.
// See ActionList.Get.
func (c *Collection) Get(ctx context.Context, doc Document, fps ...FieldPath) error {
if err := c.Actions().Get(doc, fps...).Do(ctx); err != nil {
return err.(ActionListError).Unwrap()
}
return nil
return c.Actions().Get(doc, fps...).Do(ctx)
}

// Update is a convenience for building and running a single-element action list.
// See ActionList.Update.
func (c *Collection) Update(ctx context.Context, doc Document, mods Mods) error {
if err := c.Actions().Update(doc, mods).Do(ctx); err != nil {
return err.(ActionListError).Unwrap()
}
return nil
return c.Actions().Update(doc, mods).Do(ctx)
}

func parseFieldPath(fp FieldPath) ([]string, error) {
Expand Down
6 changes: 2 additions & 4 deletions docstore/docstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package docstore

import (
"context"
"errors"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -118,10 +119,7 @@ func TestClosedErrors(t *testing.T) {

check := func(err error) {
t.Helper()
if alerr, ok := err.(ActionListError); ok {
err = alerr.Unwrap()
}
if err != errClosed {
if !errors.Is(err, errClosed) {
t.Errorf("got %v, want errClosed", err)
}
}
Expand Down
Loading