// Author: Mjy
// Create Date: 2018-11-23
package errno
var (
// Common errors
OK = &Errno{Code: 0, Message: "OK"}
InternalServerError = &Errno{Code: 10001, Message: "Internal server error"}
ErrBind = &Errno{Code: 10002, Message: "Error occurred while binding the request body to the struct"}
// user errors
ErrUserNotFound = &Errno{Code: 20102, Message: "The user was not found."}
)
func Create(c *gin.Context) {
var r struct {
Username string `json:"username"`
Password string `json:"password"`
}
var err error
if err := c.Bind(&r); err != nil {
c.JSON(http.StatusOK, gin.H{"error": errno.ErrBind})
return
}
utils.PrintJson(r)
log.Debugf("username is: [%s], password is [%s]", r.Username, r.Password)
if r.Username == "" {
err = errno.New(errno.ErrUserNotFound, fmt.Errorf("username can not found in db: xx.xx.xx.xx")).Add("This is add message.")
log.Errorf(err, "Get an error")
}
if errno.IsErrUserNotFound(err) {
log.Debug("err type is ErrUserNotFound")
}
if r.Password == "" {
err = fmt.Errorf("password is empty")
}
code, message := errno.DecodeErr(err)
c.JSON(http.StatusOK, gin.H{"code": code, "message": message})
}