2026-04-09 10:01:23 +08:00
|
|
|
package errorsx
|
|
|
|
|
|
2026-06-02 20:51:13 +08:00
|
|
|
import (
|
|
|
|
|
"agent-desk/internal/pkg/i18nx"
|
|
|
|
|
|
|
|
|
|
"github.com/mlogclub/simple/web"
|
|
|
|
|
)
|
2026-04-09 10:01:23 +08:00
|
|
|
|
|
|
|
|
const (
|
2026-04-30 17:42:24 +08:00
|
|
|
CodeInvalidParam = 1000
|
|
|
|
|
CodeBusinessError = 2000
|
|
|
|
|
CodeAuthUnauthorized = 3000
|
|
|
|
|
CodeAuthForbidden = 3001
|
|
|
|
|
CodeAuthInvalidToken = 3002
|
|
|
|
|
CodeAuthInvalidAccount = 3003
|
|
|
|
|
CodeAuthCredentialLocked = 3004
|
2026-04-09 10:01:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func InvalidParam(message string) error {
|
|
|
|
|
return web.NewError(CodeInvalidParam, message)
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-02 20:51:13 +08:00
|
|
|
func InvalidParamI18n(key string, args ...any) error {
|
|
|
|
|
return NewI18nError(CodeInvalidParam, key, args...)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-09 10:01:23 +08:00
|
|
|
func BusinessError(code int, message string) error {
|
|
|
|
|
return web.NewError(CodeBusinessError+code, message)
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-02 20:51:13 +08:00
|
|
|
func BusinessErrorI18n(code int, key string, args ...any) error {
|
|
|
|
|
return NewI18nError(CodeBusinessError+code, key, args...)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-09 10:01:23 +08:00
|
|
|
func Unauthorized(message string) error {
|
|
|
|
|
return web.NewError(CodeAuthUnauthorized, message)
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-02 20:51:13 +08:00
|
|
|
func UnauthorizedI18n(key string, args ...any) error {
|
|
|
|
|
return NewI18nError(CodeAuthUnauthorized, key, args...)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-09 10:01:23 +08:00
|
|
|
func Forbidden(message string) error {
|
|
|
|
|
return web.NewError(CodeAuthForbidden, message)
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-02 20:51:13 +08:00
|
|
|
func ForbiddenI18n(key string, args ...any) error {
|
|
|
|
|
return NewI18nError(CodeAuthForbidden, key, args...)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-09 10:01:23 +08:00
|
|
|
func InvalidToken(message string) error {
|
|
|
|
|
return web.NewError(CodeAuthInvalidToken, message)
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-02 20:51:13 +08:00
|
|
|
func InvalidTokenI18n(key string, args ...any) error {
|
|
|
|
|
return NewI18nError(CodeAuthInvalidToken, key, args...)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-09 10:01:23 +08:00
|
|
|
func InvalidAccount(message string) error {
|
|
|
|
|
return web.NewError(CodeAuthInvalidAccount, message)
|
|
|
|
|
}
|
2026-04-30 17:42:24 +08:00
|
|
|
|
2026-06-02 20:51:13 +08:00
|
|
|
func InvalidAccountI18n(key string, args ...any) error {
|
|
|
|
|
return NewI18nError(CodeAuthInvalidAccount, key, args...)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 17:42:24 +08:00
|
|
|
func CredentialLocked(message string) error {
|
|
|
|
|
return web.NewError(CodeAuthCredentialLocked, message)
|
|
|
|
|
}
|
2026-06-02 20:51:13 +08:00
|
|
|
|
|
|
|
|
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 ""
|
|
|
|
|
}
|
2026-06-26 14:46:01 +08:00
|
|
|
return e.Message(i18nx.DefaultLocale)
|
2026-06-02 20:51:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
}
|