-
Notifications
You must be signed in to change notification settings - Fork 601
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1782,6 +1782,44 @@ func postCreateUser(c *Command, r *http.Request, user *auth.UserState) Response | |
}, nil) | ||
} | ||
|
||
func convertBuyError(err error) Response { | ||
switch err { | ||
default: | ||
return InternalError("%v", err) | ||
case store.ErrInvalidCredentials: | ||
return Unauthorized(err.Error()) | ||
case store.ErrUnauthenticated: | ||
return SyncResponse(&resp{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you add helper so these all become a There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: you don't need |
||
if resp != nil { | ||
return resp | ||
} | ||
|
||
return SyncResponse(buyResult, nil) | ||
|
@@ -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 | ||
} | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment.
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