mirror of
https://github.com/dnote/dnote
synced 2026-03-15 06:55:49 +01:00
Display helpful error message to the client (#314)
This commit is contained in:
parent
22aa25498d
commit
6d474b1f18
1 changed files with 9 additions and 8 deletions
|
|
@ -166,24 +166,25 @@ func validateRegisterPayload(p registerPayload) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func parseRegisterPaylaod(r *http.Request) (registerPayload, bool) {
|
||||
func parseRegisterPaylaod(r *http.Request) (registerPayload, error) {
|
||||
var ret registerPayload
|
||||
if err := json.NewDecoder(r.Body).Decode(&ret); err != nil {
|
||||
return ret, false
|
||||
}
|
||||
if err := validateRegisterPayload(ret); err != nil {
|
||||
return ret, false
|
||||
return ret, errors.Wrap(err, "decoding json")
|
||||
}
|
||||
|
||||
return ret, true
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (a *App) register(w http.ResponseWriter, r *http.Request) {
|
||||
params, ok := parseRegisterPaylaod(r)
|
||||
if !ok {
|
||||
params, err := parseRegisterPaylaod(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid payload", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := validateRegisterPayload(params); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var count int
|
||||
if err := a.DB.Model(database.Account{}).Where("email = ?", params.Email).Count(&count).Error; err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue