Refactor error handling in services to use internationalized error messages

- Updated OSSStorage validation errors to use internationalized messages.
- Changed error messages in provider.go for unsupported file storage types.
- Refactored tag_service.go to replace hardcoded error messages with internationalized versions.
- Updated ticket_service.go to use internationalized error messages for various validation checks.
- Refactored ticket_tag_service.go to use internationalized error messages for tag validation.
- Changed ticket_view_service.go to use internationalized error messages for view validation.
- Updated tool_catalog_service.go to use internationalized error messages for tool code validation.
- Refactored user_service.go to replace error messages with internationalized versions.
- Updated ws_service.go to use internationalized error messages for WebSocket handling.
- Refactored wxwork_kf_inbound_service.go to use internationalized error messages for message handling.
- Updated wxwork_kf_outbound_service.go to use internationalized error messages for outbound message handling.
- Refactored wxwork_login_service.go to use internationalized error messages for login handling.
- Updated login.go in wxwork package to use internationalized error messages for login state and ticket validation.
This commit is contained in:
mlogclub
2026-06-02 20:51:13 +08:00
parent 77be1b4f5e
commit 0b4a1b4594
103 changed files with 1688 additions and 1350 deletions
+12
View File
@@ -0,0 +1,12 @@
package httpx
import (
"agent-desk/internal/pkg/i18nx"
"github.com/gin-gonic/gin"
"github.com/mlogclub/simple/web"
)
func JsonErrorMsg(ctx *gin.Context, key string, args ...any) *web.JsonResult {
return web.JsonErrorMsg(i18nx.T(ctx, key, args...))
}
+1 -1
View File
@@ -12,7 +12,7 @@ import (
func GetPathInt64(ctx *gin.Context, name string) (int64, bool) {
value, err := strconv.ParseInt(ctx.Param(name), 10, 64)
if err != nil {
WriteHttpStatusJSON(ctx, http.StatusBadRequest, web.JsonErrorMsg(i18nx.T(ctx, "error.path.invalid", nil)))
WriteHttpStatusJSON(ctx, http.StatusBadRequest, web.JsonErrorMsg(i18nx.T(ctx, "error.path.invalid")))
return 0, false
}
return value, true
+14 -11
View File
@@ -1,6 +1,7 @@
package httpx
import (
"agent-desk/internal/pkg/errorsx"
"agent-desk/internal/pkg/i18nx"
"net/http"
@@ -20,6 +21,10 @@ type pageData struct {
paging *sqls.Paging
}
type localizedError interface {
Message(locale string) string
}
func CursorData(results any, cursor string, hasMore bool) any {
return cursorData{results: results, cursor: cursor, hasMore: hasMore}
}
@@ -29,22 +34,14 @@ func PageData(results any, paging *sqls.Paging) any {
}
func WriteJSON(ctx *gin.Context, result any) {
ctx.JSON(http.StatusOK, localizeJSONResult(ctx, buildJSONResult(result)))
ctx.JSON(http.StatusOK, buildJSONResult(ctx, result))
}
func WriteHttpStatusJSON(ctx *gin.Context, statusCode int, result any) {
ctx.JSON(statusCode, localizeJSONResult(ctx, buildJSONResult(result)))
ctx.JSON(statusCode, buildJSONResult(ctx, result))
}
func localizeJSONResult(ctx *gin.Context, result *web.JsonResult) *web.JsonResult {
if result == nil || result.Success || result.Message == "" {
return result
}
result.Message = i18nx.TranslateKnownMessage(i18nx.Locale(ctx), result.Message)
return result
}
func buildJSONResult(result any) *web.JsonResult {
func buildJSONResult(ctx *gin.Context, result any) *web.JsonResult {
switch value := result.(type) {
case nil:
return web.JsonSuccess()
@@ -56,6 +53,12 @@ func buildJSONResult(result any) *web.JsonResult {
return web.JsonError(value)
case web.CodeError:
return web.JsonError(&value)
case *errorsx.I18nError:
return value.JsonResult(i18nx.Locale(ctx))
case errorsx.I18nError:
return value.JsonResult(i18nx.Locale(ctx))
case localizedError:
return web.JsonErrorMsg(value.Message(i18nx.Locale(ctx)))
case error:
return web.JsonError(value)
case cursorData:
+3 -2
View File
@@ -1,6 +1,7 @@
package httpx
import (
"agent-desk/internal/pkg/errorsx"
"agent-desk/internal/pkg/i18nx"
"encoding/json"
"errors"
@@ -68,12 +69,12 @@ func TestWriteJSONWrapsCommonResultTypes(t *testing.T) {
}
}
func TestWriteJSONLocalizesKnownErrorMessages(t *testing.T) {
func TestWriteJSONLocalizesI18nErrors(t *testing.T) {
gin.SetMode(gin.TestMode)
ctx, recorder := testContext()
i18nx.SetLocale(ctx, i18nx.LocaleEnUS)
WriteJSON(ctx, web.JsonErrorMsg("会话不存在"))
WriteJSON(ctx, errorsx.InvalidParamI18n("error.e0116"))
var got web.JsonResult
if err := json.Unmarshal(recorder.Body.Bytes(), &got); err != nil {