vaxilu/x-ui · warning

username can not be empty

Error message

username can not be empty

What it means

UpdateFirstUser in web/service/user.go validates its arguments before touching the database: if the username parameter is an empty string it returns this error immediately. The method updates the first (initial) user record, and an empty username would corrupt the account, so the guard exists to prevent that.

Source

Thrown at web/service/user.go:56

	} else if err != nil {
		logger.Warning("check user err:", err)
		return nil
	}
	return user
}

func (s *UserService) UpdateUser(id int, username string, password string) error {
	db := database.GetDB()
	return db.Model(model.User{}).
		Where("id = ?", id).
		Update("username", username).
		Update("password", password).
		Error
}

func (s *UserService) UpdateFirstUser(username string, password string) error {
	if username == "" {
		return errors.New("username can not be empty")
	} else if password == "" {
		return errors.New("password can not be empty")
	}
	db := database.GetDB()
	user := &model.User{}
	err := db.Model(model.User{}).First(user).Error
	if database.IsNotFound(err) {
		user.Username = username
		user.Password = password
		return db.Model(model.User{}).Create(user).Error
	} else if err != nil {
		return err
	}
	user.Username = username
	user.Password = password
	return db.Save(user).Error
}

View on GitHub (pinned to 9c1be8c57a)

Solutions

  1. Provide a non-empty username when calling UpdateFirstUser or submitting the settings form
  2. Add server-side form binding validation (e.g. binding:"required") so empty fields are rejected at the handler level
  3. Check that the client actually sends the username field in the request body

Example fix

// before
func (s *UserService) UpdateFirstUser(username string, password string) error {
    if username == "" { return errors.New("username can not be empty") }
// after (reject earlier, at handler level)
type settingForm struct {
    Username string `form:"username" binding:"required"`
    Password string `form:"password" binding:"required"`
}
if err := c.ShouldBind(&form); err != nil {
    jsonMsg(c, "设置", err); return
}
err := a.userService.UpdateFirstUser(form.Username, form.Password)
Defensive patterns

Strategy: validation

Validate before calling

if username == "" {
    return errors.New("username can not be empty")
}

Try / catch

if err := userService.UpdateFirstUser(username, password); err != nil {
    switch err.Error() {
    case "username can not be empty", "password can not be empty":
        // 400 Bad Request — caller input problem
    default:
        // 500 — persist/log
    }
}

Prevention

When it happens

Trigger: Calling UpdateFirstUser("", password), typically via updateSetting where the submitted settings form contained an empty username value that was passed straight through.

Common situations: Settings page submitted with the username field blank; a client integration that omits the username key from the payload; form binding failing silently so the field defaults to "".

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of vaxilu/x-ui@9c1be8c57a (2026-09-02). Data as JSON: /api/errors/4da2182222000e96. Report an issue: GitHub.