Added custom error type when no username and password are given when creating a new user
This commit is contained in:
parent
3d27bb1438
commit
a734f21ac2
|
@ -37,6 +37,19 @@ func (err ErrNoUsername) Error() string {
|
||||||
return fmt.Sprintf("you need to specify a username [user id: %d]", err.UserID)
|
return fmt.Sprintf("you need to specify a username [user id: %d]", err.UserID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ErrNoUsernamePassword represents a "UsernameAlreadyExists" kind of error.
|
||||||
|
type ErrNoUsernamePassword struct {}
|
||||||
|
|
||||||
|
// IsErrNoUsernamePassword checks if an error is a ErrUsernameExists.
|
||||||
|
func IsErrNoUsernamePassword(err error) bool {
|
||||||
|
_, ok := err.(ErrNoUsernamePassword)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func (err ErrNoUsernamePassword) Error() string {
|
||||||
|
return fmt.Sprintf("you need to specify a username and a password")
|
||||||
|
}
|
||||||
|
|
||||||
// ErrUserDoesNotExist represents a "UsernameAlreadyExists" kind of error.
|
// ErrUserDoesNotExist represents a "UsernameAlreadyExists" kind of error.
|
||||||
type ErrUserDoesNotExist struct {
|
type ErrUserDoesNotExist struct {
|
||||||
UserID int64
|
UserID int64
|
||||||
|
|
|
@ -12,7 +12,7 @@ func CreateUser(user User) (newUser User, err error) {
|
||||||
|
|
||||||
// Check if we have all needed informations
|
// Check if we have all needed informations
|
||||||
if newUser.Password == "" || newUser.Username == "" {
|
if newUser.Password == "" || newUser.Username == "" {
|
||||||
return User{}, fmt.Errorf("you need to specify at least a username and a password")
|
return User{}, ErrNoUsernamePassword{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the user already existst
|
// Check if the user already existst
|
||||||
|
|
|
@ -73,6 +73,11 @@ func UserAddOrUpdate(c echo.Context) error {
|
||||||
return c.JSON(http.StatusBadRequest, models.Message{"Please specify a username."})
|
return c.JSON(http.StatusBadRequest, models.Message{"Please specify a username."})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for no username or password provided
|
||||||
|
if models.IsErrNoUsernamePassword(err) {
|
||||||
|
return c.JSON(http.StatusBadRequest, models.Message{"Please specify a username and a password."})
|
||||||
|
}
|
||||||
|
|
||||||
// Check for user does not exist
|
// Check for user does not exist
|
||||||
if models.IsErrUserDoesNotExist(err) {
|
if models.IsErrUserDoesNotExist(err) {
|
||||||
return c.JSON(http.StatusBadRequest, models.Message{"The user does not exist."})
|
return c.JSON(http.StatusBadRequest, models.Message{"The user does not exist."})
|
||||||
|
|
Loading…
Reference in New Issue