Improve error handling in GenerateIdForNewUser()

This commit is contained in:
Yang Luo 2023-12-14 10:11:06 +08:00
parent 85b86e8831
commit 5c103e8cd3
2 changed files with 17 additions and 1 deletions

View File

@ -1021,7 +1021,10 @@ func GenerateIdForNewUser(application *Application) (string, error) {
lastUserId := -1 lastUserId := -1
if lastUser != nil { if lastUser != nil {
lastUserId = util.ParseInt(lastUser.Id) lastUserId, err = util.ParseIntWithError(lastUser.Id)
if err != nil {
return util.GenerateId(), nil
}
} }
res := strconv.Itoa(lastUserId + 1) res := strconv.Itoa(lastUserId + 1)

View File

@ -45,6 +45,19 @@ func ParseInt(s string) int {
return i return i
} }
func ParseIntWithError(s string) (int, error) {
if s == "" {
return 0, fmt.Errorf("ParseIntWithError() error, empty string")
}
i, err := strconv.Atoi(s)
if err != nil {
return 0, err
}
return i, nil
}
func ParseFloat(s string) float64 { func ParseFloat(s string) float64 {
f, err := strconv.ParseFloat(s, 64) f, err := strconv.ParseFloat(s, 64)
if err != nil { if err != nil {