Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

daemon, store: switch to new store APIs in snapd #2036

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
86 changes: 53 additions & 33 deletions daemon/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,44 @@ func postCreateUser(c *Command, r *http.Request, user *auth.UserState) Response
}, nil)
}

func convertBuyError(err error) Response {
switch err {
default:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd rather default be the last case in a switch

return InternalError("%v", err)
case store.ErrInvalidCredentials:
return Unauthorized(err.Error())
case store.ErrUnauthenticated:
return SyncResponse(&resp{
Copy link
Contributor

@chipaca chipaca Sep 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add helper so these all become a return BadRequestKind(kind, err)? See e.g. makeErrorResponder in response.go

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(and yes, I know there already are a bunch of these, so if you want to leave it I'll do it in a branch all of its own)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed to fix this in a followup

Type: ResponseTypeError,
Result: &errorResult{
Message: err.Error(),
Kind: errorKindLoginRequired,
},
Status: http.StatusBadRequest,
}, nil)
case store.ErrTOSNotAccepted:
return SyncResponse(&resp{
Type: ResponseTypeError,
Result: &errorResult{
Message: err.Error(),
Kind: errorKindTermsNotAccepted,
},
Status: http.StatusBadRequest,
}, nil)
case store.ErrNoPaymentMethods:
return SyncResponse(&resp{
Type: ResponseTypeError,
Result: &errorResult{
Message: err.Error(),
Kind: errorKindNoPaymentMethods,
},
Status: http.StatusBadRequest,
}, nil)
case nil:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

followup nit: in the spirit of "exit early", this is usually the first case in the switch

return nil
}
}

func postBuy(c *Command, r *http.Request, user *auth.UserState) Response {
var opts store.BuyOptions

Expand All @@ -1795,13 +1833,9 @@ func postBuy(c *Command, r *http.Request, user *auth.UserState) Response {

buyResult, err := s.Buy(&opts, user)

switch err {
default:
return InternalError("%v", err)
case store.ErrInvalidCredentials:
return Unauthorized(err.Error())
case nil:
// continue
resp := convertBuyError(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you don't need resp outside the if, so move it into it

if resp != nil {
return resp
}

return SyncResponse(buyResult, nil)
Expand All @@ -1818,6 +1852,15 @@ func getPaymentMethods(c *Command, r *http.Request, user *auth.UserState) Respon
return InternalError("%v", err)
case store.ErrInvalidCredentials:
return Unauthorized(err.Error())
case store.ErrUnauthenticated:
return SyncResponse(&resp{
Type: ResponseTypeError,
Result: &errorResult{
Message: err.Error(),
Kind: errorKindLoginRequired,
},
Status: http.StatusBadRequest,
}, nil)
case nil:
// continue
}
Expand All @@ -1829,32 +1872,9 @@ func readyToBuy(c *Command, r *http.Request, user *auth.UserState) Response {
s := getStore(c)

err := s.ReadyToBuy(user)

switch err {
default:
return InternalError("%v", err)
case store.ErrInvalidCredentials:
return Unauthorized(err.Error())
case store.ErrTOSNotAccepted:
return SyncResponse(&resp{
Type: ResponseTypeError,
Result: &errorResult{
Message: err.Error(),
Kind: errorKindTermsNotAccepted,
},
Status: http.StatusBadRequest,
}, nil)
case store.ErrNoPaymentMethods:
return SyncResponse(&resp{
Type: ResponseTypeError,
Result: &errorResult{
Message: err.Error(),
Kind: errorKindNoPaymentMethods,
},
Status: http.StatusBadRequest,
}, nil)
case nil:
// continue
resp := convertBuyError(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto here wrt moving it into the if

if resp != nil {
return resp
}

return SyncResponse(true, nil)
Expand Down