Added custom error type when no username and password are given when creating a new user

This commit is contained in:
kolaente 2018-01-25 12:15:12 +01:00
parent 3d27bb1438
commit a734f21ac2
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 19 additions and 1 deletions

View File

@ -37,6 +37,19 @@ func (err ErrNoUsername) Error() string {
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.
type ErrUserDoesNotExist struct {
UserID int64

View File

@ -12,7 +12,7 @@ func CreateUser(user User) (newUser User, err error) {
// Check if we have all needed informations
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

View File

@ -73,6 +73,11 @@ func UserAddOrUpdate(c echo.Context) error {
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
if models.IsErrUserDoesNotExist(err) {
return c.JSON(http.StatusBadRequest, models.Message{"The user does not exist."})