Adding error codes on bad requests.
This commit is contained in:
parent
087b7ceb7b
commit
cd235d382d
@ -3,11 +3,11 @@ package usercontroller
|
|||||||
import (
|
import (
|
||||||
d "WWWShop/wwwshop/dao/database"
|
d "WWWShop/wwwshop/dao/database"
|
||||||
u "WWWShop/wwwshop/dao/usermanager"
|
u "WWWShop/wwwshop/dao/usermanager"
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"bytes"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserController struct{}
|
type UserController struct{}
|
||||||
@ -21,30 +21,35 @@ func (self UserController) POST(w http.ResponseWriter, r *http.Request) {
|
|||||||
decoder := json.NewDecoder(r.Body)
|
decoder := json.NewDecoder(r.Body)
|
||||||
err := decoder.Decode(&decoded_user_struct)
|
err := decoder.Decode(&decoded_user_struct)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||||
json_response, _ := EncodeJSONResponse(NewResponseError("Unable to decode json"))
|
json_response, _ := EncodeJSONResponse(NewResponseError("Unable to decode json"))
|
||||||
fmt.Fprintf(w, json_response)
|
fmt.Fprintf(w, json_response)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if decoded_user_struct.Username == "" {
|
if decoded_user_struct.Username == "" {
|
||||||
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||||
json_response, _ := EncodeJSONResponse(NewResponseError("Unable to get username"))
|
json_response, _ := EncodeJSONResponse(NewResponseError("Unable to get username"))
|
||||||
fmt.Fprintf(w, json_response)
|
fmt.Fprintf(w, json_response)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if decoded_user_struct.PasswordPlaintext == "" {
|
if decoded_user_struct.PasswordPlaintext == "" {
|
||||||
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||||
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
json_response, _ := EncodeJSONResponse(NewResponseError("Unable to get password"))
|
json_response, _ := EncodeJSONResponse(NewResponseError("Unable to get password"))
|
||||||
fmt.Fprintf(w, json_response)
|
fmt.Fprintf(w, json_response)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user_manager := u.New(d.DB())
|
user_manager := u.New(d.DB())
|
||||||
user, err := user_manager.AddWithPlainPassword(decoded_user_struct.Username, decoded_user_struct.PasswordPlaintext)
|
user, err := user_manager.AddWithPlainPassword(decoded_user_struct.Username, decoded_user_struct.PasswordPlaintext)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
json_response, _ := EncodeJSONResponse(NewResponseError(strings.Join(
|
json_response, _ := EncodeJSONResponse(NewResponseError(strings.Join(
|
||||||
[]string{"Unable to create user ", err.Error()},
|
[]string{"Unable to create user ", err.Error()},
|
||||||
"")))
|
"")))
|
||||||
fmt.Fprintf(w, json_response)
|
fmt.Fprintf(w, json_response)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
json_response, err := EncodeJSONResponse(user);
|
json_response, err := EncodeJSONResponse(user)
|
||||||
fmt.Fprintf(w, json_response)
|
fmt.Fprintf(w, json_response)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user