fix(http): convert view errors
parent
07edc3108b
commit
1f983f7896
49
bolt/view.go
49
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
|
||||
}
|
||||
|
|
11
view.go
11
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
|
||||
|
|
Loading…
Reference in New Issue