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:
@@ -1,6 +1,10 @@
|
||||
package errorsx
|
||||
|
||||
import "github.com/mlogclub/simple/web"
|
||||
import (
|
||||
"agent-desk/internal/pkg/i18nx"
|
||||
|
||||
"github.com/mlogclub/simple/web"
|
||||
)
|
||||
|
||||
const (
|
||||
CodeInvalidParam = 1000
|
||||
@@ -16,26 +20,92 @@ func InvalidParam(message string) error {
|
||||
return web.NewError(CodeInvalidParam, message)
|
||||
}
|
||||
|
||||
func InvalidParamI18n(key string, args ...any) error {
|
||||
return NewI18nError(CodeInvalidParam, key, args...)
|
||||
}
|
||||
|
||||
func BusinessError(code int, message string) error {
|
||||
return web.NewError(CodeBusinessError+code, message)
|
||||
}
|
||||
|
||||
func BusinessErrorI18n(code int, key string, args ...any) error {
|
||||
return NewI18nError(CodeBusinessError+code, key, args...)
|
||||
}
|
||||
|
||||
func Unauthorized(message string) error {
|
||||
return web.NewError(CodeAuthUnauthorized, message)
|
||||
}
|
||||
|
||||
func UnauthorizedI18n(key string, args ...any) error {
|
||||
return NewI18nError(CodeAuthUnauthorized, key, args...)
|
||||
}
|
||||
|
||||
func Forbidden(message string) error {
|
||||
return web.NewError(CodeAuthForbidden, message)
|
||||
}
|
||||
|
||||
func ForbiddenI18n(key string, args ...any) error {
|
||||
return NewI18nError(CodeAuthForbidden, key, args...)
|
||||
}
|
||||
|
||||
func InvalidToken(message string) error {
|
||||
return web.NewError(CodeAuthInvalidToken, message)
|
||||
}
|
||||
|
||||
func InvalidTokenI18n(key string, args ...any) error {
|
||||
return NewI18nError(CodeAuthInvalidToken, key, args...)
|
||||
}
|
||||
|
||||
func InvalidAccount(message string) error {
|
||||
return web.NewError(CodeAuthInvalidAccount, message)
|
||||
}
|
||||
|
||||
func InvalidAccountI18n(key string, args ...any) error {
|
||||
return NewI18nError(CodeAuthInvalidAccount, key, args...)
|
||||
}
|
||||
|
||||
func CredentialLocked(message string) error {
|
||||
return web.NewError(CodeAuthCredentialLocked, message)
|
||||
}
|
||||
|
||||
func CredentialLockedI18n(key string, args ...any) error {
|
||||
return NewI18nError(CodeAuthCredentialLocked, key, args...)
|
||||
}
|
||||
|
||||
type I18nError struct {
|
||||
Code int
|
||||
Key string
|
||||
Args []any
|
||||
}
|
||||
|
||||
func NewI18nError(code int, key string, args ...any) *I18nError {
|
||||
return &I18nError{Code: code, Key: key, Args: args}
|
||||
}
|
||||
|
||||
func (e *I18nError) Error() string {
|
||||
if e == nil {
|
||||
return ""
|
||||
}
|
||||
return e.Message(i18nx.LocaleZhCN)
|
||||
}
|
||||
|
||||
func (e *I18nError) Unwrap() error {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
return web.NewError(e.Code, e.Error())
|
||||
}
|
||||
|
||||
func (e *I18nError) Message(locale string) string {
|
||||
if e == nil {
|
||||
return ""
|
||||
}
|
||||
return i18nx.Getf(locale, e.Key, e.Args...)
|
||||
}
|
||||
|
||||
func (e *I18nError) JsonResult(locale string) *web.JsonResult {
|
||||
if e == nil {
|
||||
return web.JsonSuccess()
|
||||
}
|
||||
return web.JsonErrorCode(e.Code, e.Message(locale))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user