From 1f983f7896be29bc80e2187fca981405423ce8d0 Mon Sep 17 00:00:00 2001 From: Kelvin Wang Date: Mon, 17 Dec 2018 11:13:39 -0500 Subject: [PATCH] fix(http): convert view errors --- bolt/view.go | 49 +++++++++++++++++++++++++++++++++++-------------- view.go | 11 +++++++---- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/bolt/view.go b/bolt/view.go index 2caefacb97..b270f38a5c 100644 --- a/bolt/view.go +++ b/bolt/view.go @@ -40,21 +40,28 @@ func (c *Client) FindViewByID(ctx context.Context, id platform.ID) (*platform.Vi return d, nil } -func (c *Client) findViewByID(ctx context.Context, tx *bolt.Tx, id platform.ID) (*platform.View, error) { +func (c *Client) findViewByID(ctx context.Context, tx *bolt.Tx, id platform.ID) (*platform.View, *platform.Error) { var d platform.View encodedID, err := id.Encode() if err != nil { - return nil, err + return nil, &platform.Error{ + Err: err, + } } v := tx.Bucket(viewBucket).Get(encodedID) if len(v) == 0 { - return nil, platform.ErrViewNotFound + return nil, &platform.Error{ + Code: platform.ENotFound, + Msg: platform.ErrViewNotFound, + } } if err := json.Unmarshal(v, &d); err != nil { - return nil, err + return nil, &platform.Error{ + Err: err, + } } return &d, nil @@ -99,11 +106,16 @@ func (c *Client) FindView(ctx context.Context, filter platform.ViewFilter) (*pla }) if err != nil { - return nil, err + return nil, &platform.Error{ + Err: err, + } } if d == nil { - return nil, platform.ErrViewNotFound + return nil, &platform.Error{ + Code: platform.ENotFound, + Msg: platform.ErrViewNotFound, + } } return d, nil @@ -269,20 +281,29 @@ func (c *Client) DeleteView(ctx context.Context, id platform.ID) error { }) } -func (c *Client) deleteView(ctx context.Context, tx *bolt.Tx, id platform.ID) error { - _, err := c.findViewByID(ctx, tx, id) - if err != nil { - return err +func (c *Client) deleteView(ctx context.Context, tx *bolt.Tx, id platform.ID) *platform.Error { + _, pe := c.findViewByID(ctx, tx, id) + if pe != nil { + return pe } encodedID, err := id.Encode() if err != nil { - return err + return &platform.Error{ + Err: err, + } } if err := tx.Bucket(viewBucket).Delete(encodedID); err != nil { - return err + return &platform.Error{ + Err: err, + } } - return c.deleteUserResourceMappings(ctx, tx, platform.UserResourceMappingFilter{ + if err := c.deleteUserResourceMappings(ctx, tx, platform.UserResourceMappingFilter{ ResourceID: id, ResourceType: platform.ViewResourceType, - }) + }); err != nil { + return &platform.Error{ + Err: err, + } + } + return nil } diff --git a/view.go b/view.go index 82f7baadf9..bf616d1de2 100644 --- a/view.go +++ b/view.go @@ -6,8 +6,8 @@ import ( "fmt" ) -// ErrViewNotFound is the error for a missing View. -const ErrViewNotFound = ChronografError("view not found") +// ErrViewNotFound is the error msg for a missing View. +const ErrViewNotFound = "view not found" // ViewService represents a service for managing View data. type ViewService interface { @@ -36,10 +36,13 @@ type ViewUpdate struct { } // Valid validates the update struct. It expects minimal values to be set. -func (u ViewUpdate) Valid() error { +func (u ViewUpdate) Valid() *Error { _, ok := u.Properties.(EmptyViewProperties) if u.Name == nil && ok { - return fmt.Errorf("expected at least one attribute to be updated") + return &Error{ + Code: EInvalid, + Msg: "expected at least one attribute to be updated", + } } return nil