2022-12-01 15:48:40 +08:00
|
|
|
package buserr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/i18n"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BusinessError struct {
|
|
|
|
Msg string
|
|
|
|
Detail interface{}
|
2023-02-07 16:29:54 +08:00
|
|
|
Map map[string]interface{}
|
2022-12-01 15:48:40 +08:00
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e BusinessError) Error() string {
|
2022-12-04 12:47:19 +08:00
|
|
|
content := ""
|
|
|
|
if e.Detail != nil {
|
|
|
|
content = i18n.GetErrMsg(e.Msg, map[string]interface{}{"detail": e.Detail})
|
2023-02-07 16:29:54 +08:00
|
|
|
} else if e.Map != nil {
|
|
|
|
content = i18n.GetErrMsg(e.Msg, e.Map)
|
2022-12-04 12:47:19 +08:00
|
|
|
} else {
|
|
|
|
content = i18n.GetErrMsg(e.Msg, nil)
|
|
|
|
}
|
2022-12-01 15:48:40 +08:00
|
|
|
if content == "" {
|
|
|
|
if e.Err != nil {
|
|
|
|
return e.Err.Error()
|
|
|
|
}
|
|
|
|
return errors.New(e.Msg).Error()
|
|
|
|
}
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
2022-12-04 12:47:19 +08:00
|
|
|
func New(Key string) BusinessError {
|
|
|
|
return BusinessError{
|
|
|
|
Msg: Key,
|
|
|
|
Detail: nil,
|
|
|
|
Err: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 16:29:54 +08:00
|
|
|
func WithDetail(Key string, detail interface{}, err error) BusinessError {
|
2022-12-01 15:48:40 +08:00
|
|
|
return BusinessError{
|
|
|
|
Msg: Key,
|
|
|
|
Detail: detail,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 16:29:54 +08:00
|
|
|
|
2023-03-08 23:58:42 +08:00
|
|
|
func WithErr(Key string, err error) BusinessError {
|
2024-03-25 12:20:07 +08:00
|
|
|
paramMap := map[string]interface{}{}
|
|
|
|
if err != nil {
|
|
|
|
paramMap["err"] = err
|
|
|
|
}
|
2023-03-08 23:58:42 +08:00
|
|
|
return BusinessError{
|
2024-03-25 12:20:07 +08:00
|
|
|
Msg: Key,
|
|
|
|
Map: paramMap,
|
|
|
|
Err: err,
|
2023-03-08 23:58:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 16:29:54 +08:00
|
|
|
func WithMap(Key string, maps map[string]interface{}, err error) BusinessError {
|
|
|
|
return BusinessError{
|
|
|
|
Msg: Key,
|
|
|
|
Map: maps,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
2023-08-11 15:52:13 +08:00
|
|
|
|
|
|
|
func WithNameAndErr(Key string, name string, err error) BusinessError {
|
|
|
|
paramMap := map[string]interface{}{}
|
|
|
|
if name != "" {
|
|
|
|
paramMap["name"] = name
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
paramMap["err"] = err.Error()
|
|
|
|
}
|
|
|
|
return BusinessError{
|
|
|
|
Msg: Key,
|
|
|
|
Map: paramMap,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
2023-10-16 15:48:06 +08:00
|
|
|
|
|
|
|
func WithName(Key string, name string) BusinessError {
|
|
|
|
paramMap := map[string]interface{}{}
|
|
|
|
if name != "" {
|
|
|
|
paramMap["name"] = name
|
|
|
|
}
|
|
|
|
return BusinessError{
|
|
|
|
Msg: Key,
|
|
|
|
Map: paramMap,
|
|
|
|
}
|
|
|
|
}
|