From 19998ebb8acc3fbe3fed585e302a487345d822bd Mon Sep 17 00:00:00 2001 From: JustSong Date: Sun, 2 Feb 2025 16:25:52 +0800 Subject: [PATCH] feat: update i18n messages and improve error handling --- common/i18n/locales/en.json | 12 +++--------- common/i18n/locales/zh-CN.json | 12 +++--------- common/message/email.go | 7 ++++--- controller/misc.go | 10 +++++----- controller/option.go | 10 ++++++---- controller/user.go | 14 +++++++------- 6 files changed, 28 insertions(+), 37 deletions(-) diff --git a/common/i18n/locales/en.json b/common/i18n/locales/en.json index 7c225e00..4b24dea7 100644 --- a/common/i18n/locales/en.json +++ b/common/i18n/locales/en.json @@ -1,11 +1,5 @@ { - "success": "Success", - "unauthorized": "Unauthorized", - "forbidden": "Forbidden", - "invalid_token": "Invalid token", - "channel_not_found": "Channel not found", - "invalid_request": "Invalid request", - "internal_error": "Internal server error", - "quota_exceeded": "Quota exceeded", - "invalid_input": "Invalid input, please check your input" + "invalid_input": "Invalid input, please check your input", + "send_email_failed": "failed to send email: ", + "invalid_parameter": "invalid parameter" } diff --git a/common/i18n/locales/zh-CN.json b/common/i18n/locales/zh-CN.json index 8da01c84..805d5c5a 100644 --- a/common/i18n/locales/zh-CN.json +++ b/common/i18n/locales/zh-CN.json @@ -1,11 +1,5 @@ { - "success": "成功", - "unauthorized": "未授权", - "forbidden": "禁止访问", - "invalid_token": "无效的令牌", - "channel_not_found": "未找到渠道", - "invalid_request": "无效的请求", - "internal_error": "服务器内部错误", - "quota_exceeded": "配额已用尽", - "invalid_input": "无效的输入,请检查您的输入" + "invalid_input": "无效的输入,请检查您的输入", + "send_email_failed": "发送邮件失败:", + "invalid_parameter": "无效的参数" } diff --git a/common/message/email.go b/common/message/email.go index 187ac8c3..4e742f33 100644 --- a/common/message/email.go +++ b/common/message/email.go @@ -5,11 +5,12 @@ import ( "crypto/tls" "encoding/base64" "fmt" - "github.com/songquanpeng/one-api/common/config" "net" "net/smtp" "strings" "time" + + "github.com/songquanpeng/one-api/common/config" ) func shouldAuth() bool { @@ -98,8 +99,8 @@ func SendEmail(subject string, receiver string, content string) error { if err != nil { return err } - } else { - err = smtp.SendMail(addr, auth, config.SMTPAccount, to, mail) + return nil } + err = smtp.SendMail(addr, auth, config.SMTPAccount, to, mail) return err } diff --git a/controller/misc.go b/controller/misc.go index c2764b57..d3981dc2 100644 --- a/controller/misc.go +++ b/controller/misc.go @@ -8,6 +8,7 @@ import ( "github.com/songquanpeng/one-api/common" "github.com/songquanpeng/one-api/common/config" + "github.com/songquanpeng/one-api/common/i18n" "github.com/songquanpeng/one-api/common/message" "github.com/songquanpeng/one-api/model" @@ -86,7 +87,7 @@ func SendEmailVerification(c *gin.Context) { if err := common.Validate.Var(email, "required,email"); err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, - "message": "无效的参数", + "message": i18n.Translate(c, "invalid_parameter"), }) return } @@ -121,7 +122,6 @@ func SendEmailVerification(c *gin.Context) { "

验证码 %d 分钟内有效,如果不是本人操作,请忽略。

", config.SystemName, code, common.VerificationValidMinutes) err := message.SendEmail(subject, email, content) if err != nil { - c.JSON(http.StatusOK, gin.H{ "success": false, "message": err.Error(), @@ -140,7 +140,7 @@ func SendPasswordResetEmail(c *gin.Context) { if err := common.Validate.Var(email, "required,email"); err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, - "message": "无效的参数", + "message": i18n.Translate(c, "invalid_parameter"), }) return } @@ -163,7 +163,7 @@ func SendPasswordResetEmail(c *gin.Context) { if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, - "message": err.Error(), + "message": fmt.Sprintf("%s%s", i18n.Translate(c, "send_email_failed"), err.Error()), }) return } @@ -185,7 +185,7 @@ func ResetPassword(c *gin.Context) { if req.Email == "" || req.Token == "" { c.JSON(http.StatusOK, gin.H{ "success": false, - "message": "无效的参数", + "message": i18n.Translate(c, "invalid_parameter"), }) return } diff --git a/controller/option.go b/controller/option.go index f86e3a64..310086ef 100644 --- a/controller/option.go +++ b/controller/option.go @@ -2,12 +2,14 @@ package controller import ( "encoding/json" - "github.com/songquanpeng/one-api/common/config" - "github.com/songquanpeng/one-api/common/helper" - "github.com/songquanpeng/one-api/model" "net/http" "strings" + "github.com/songquanpeng/one-api/common/config" + "github.com/songquanpeng/one-api/common/helper" + "github.com/songquanpeng/one-api/common/i18n" + "github.com/songquanpeng/one-api/model" + "github.com/gin-gonic/gin" ) @@ -38,7 +40,7 @@ func UpdateOption(c *gin.Context) { if err != nil { c.JSON(http.StatusBadRequest, gin.H{ "success": false, - "message": "无效的参数", + "message": i18n.Translate(c, "invalid_parameter"), }) return } diff --git a/controller/user.go b/controller/user.go index 8c2837a0..d7fd8d77 100644 --- a/controller/user.go +++ b/controller/user.go @@ -35,7 +35,7 @@ func Login(c *gin.Context) { err := json.NewDecoder(c.Request.Body).Decode(&loginRequest) if err != nil { c.JSON(http.StatusOK, gin.H{ - "message": "无效的参数", + "message": i18n.Translate(c, "invalid_parameter"), "success": false, }) return @@ -44,7 +44,7 @@ func Login(c *gin.Context) { password := loginRequest.Password if username == "" || password == "" { c.JSON(http.StatusOK, gin.H{ - "message": "无效的参数", + "message": i18n.Translate(c, "invalid_parameter"), "success": false, }) return @@ -131,7 +131,7 @@ func Register(c *gin.Context) { if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, - "message": "无效的参数", + "message": i18n.Translate(c, "invalid_parameter"), }) return } @@ -371,7 +371,7 @@ func UpdateUser(c *gin.Context) { if err != nil || updatedUser.Id == 0 { c.JSON(http.StatusOK, gin.H{ "success": false, - "message": "无效的参数", + "message": i18n.Translate(c, "invalid_parameter"), }) return } @@ -435,7 +435,7 @@ func UpdateSelf(c *gin.Context) { if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, - "message": "无效的参数", + "message": i18n.Translate(c, "invalid_parameter"), }) return } @@ -545,7 +545,7 @@ func CreateUser(c *gin.Context) { if err != nil || user.Username == "" || user.Password == "" { c.JSON(http.StatusOK, gin.H{ "success": false, - "message": "无效的参数", + "message": i18n.Translate(c, "invalid_parameter"), }) return } @@ -601,7 +601,7 @@ func ManageUser(c *gin.Context) { if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, - "message": "无效的参数", + "message": i18n.Translate(c, "invalid_parameter"), }) return }