2026-06-02 20:51:13 +08:00
|
|
|
package i18nx
|
|
|
|
|
|
|
|
|
|
type Error struct {
|
|
|
|
|
Key string
|
|
|
|
|
Args []any
|
|
|
|
|
Cause error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Errorf(key string, args ...any) *Error {
|
|
|
|
|
err := &Error{Key: key, Args: args}
|
|
|
|
|
for _, arg := range args {
|
|
|
|
|
if cause, ok := arg.(error); ok {
|
|
|
|
|
err.Cause = cause
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Error) Error() string {
|
|
|
|
|
if e == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2026-06-26 14:46:01 +08:00
|
|
|
return e.Message(DefaultLocale)
|
2026-06-02 20:51:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Error) Message(locale string) string {
|
|
|
|
|
if e == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return Getf(locale, e.Key, e.Args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Error) Unwrap() error {
|
|
|
|
|
if e == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return e.Cause
|
|
|
|
|
}
|