diff --git a/backend/app/service/app.go b/backend/app/service/app.go index dd2153667..06a0321d9 100644 --- a/backend/app/service/app.go +++ b/backend/app/service/app.go @@ -3,7 +3,6 @@ package service import ( "encoding/base64" "encoding/json" - "fmt" "os" "path" "strings" @@ -132,11 +131,11 @@ func (a AppService) Install(name string, appDetailId uint, params map[string]int httpPort, err := checkPort("PANEL_APP_PORT_HTTP", params) if err != nil { - return nil, fmt.Errorf("%d port is in used", httpPort) + return nil, err } httpsPort, err := checkPort("PANEL_APP_PORT_HTTPS", params) if err != nil { - return nil, fmt.Errorf("%d port is in used", httpsPort) + return nil, err } appDetail, err := appDetailRepo.GetFirst(commonRepo.WithByID(appDetailId)) diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go index a13a28e6e..01216bffe 100644 --- a/backend/app/service/app_utils.go +++ b/backend/app/service/app_utils.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/1Panel-dev/1Panel/backend/buserr" "io/ioutil" "math" "net/http" @@ -88,7 +89,7 @@ func checkPort(key string, params map[string]interface{}) (int, error) { if ok { portN := int(math.Ceil(port.(float64))) if common.ScanPort(portN) { - return portN, errors.New("port is in used") + return portN, buserr.New(constant.ErrPortInUsed, portN, nil) } else { return portN, nil } diff --git a/backend/buserr/errors.go b/backend/buserr/errors.go new file mode 100644 index 000000000..c06f068a7 --- /dev/null +++ b/backend/buserr/errors.go @@ -0,0 +1,32 @@ +package buserr + +import ( + "github.com/1Panel-dev/1Panel/backend/i18n" + "github.com/pkg/errors" +) + +type BusinessError struct { + Msg string + Detail interface{} + Err error +} + +func (e BusinessError) Error() string { + + content := i18n.GetErrMsg(e.Msg, map[string]interface{}{"detail": e.Detail}) + if content == "" { + if e.Err != nil { + return e.Err.Error() + } + return errors.New(e.Msg).Error() + } + return content +} + +func New(Key string, detail interface{}, err error) BusinessError { + return BusinessError{ + Msg: Key, + Detail: detail, + Err: err, + } +} diff --git a/backend/constant/errs.go b/backend/constant/errs.go index 42cbd4582..29c07ab57 100644 --- a/backend/constant/errs.go +++ b/backend/constant/errs.go @@ -41,3 +41,8 @@ var ( ErrTypePasswordExpired = "ErrPasswordExpired" ErrTypeNotSafety = "ErrNotSafety" ) + +// app +var ( + ErrPortInUsed = "ErrPortInUsed" +) diff --git a/backend/i18n/i18n.go b/backend/i18n/i18n.go index 78bdf3b08..261a2e1fb 100644 --- a/backend/i18n/i18n.go +++ b/backend/i18n/i18n.go @@ -11,15 +11,6 @@ import ( "gopkg.in/yaml.v2" ) -func GetMsg(msg string) string { - content := ginI18n.MustGetMessage(msg) - if content == "" { - return msg - } else { - return content - } -} - func GetMsgWithMap(msg string, maps map[string]interface{}) string { content := "" if maps == nil { @@ -40,6 +31,21 @@ func GetMsgWithMap(msg string, maps map[string]interface{}) string { } } +func GetErrMsg(msg string, maps map[string]interface{}) string { + content := "" + if maps == nil { + content = ginI18n.MustGetMessage(&i18n.LocalizeConfig{ + MessageID: msg, + }) + } else { + content = ginI18n.MustGetMessage(&i18n.LocalizeConfig{ + MessageID: msg, + TemplateData: maps, + }) + } + return content +} + //go:embed lang/* var fs embed.FS diff --git a/backend/i18n/lang/en.yaml b/backend/i18n/lang/en.yaml index c680dcab6..c88d2b11e 100644 --- a/backend/i18n/lang/en.yaml +++ b/backend/i18n/lang/en.yaml @@ -11,4 +11,8 @@ ErrStructTransform: "Type conversion failure: {{ .detail }}" ErrNotLogin: "User is not Login: {{ .detail }}" ErrNotSafety: "The login status of the current user is unsafe: {{ .detail }}" ErrPasswordExpired: "The current password has expired: {{ .detail }}" -ErrNotSupportType: "The system does not support the current type: {{ .detail }}" \ No newline at end of file +ErrNotSupportType: "The system does not support the current type: {{ .detail }}" + + +#app +ErrPortInUsed: "{{ .detail }} 端口已被占用" \ No newline at end of file diff --git a/backend/i18n/lang/zh.yaml b/backend/i18n/lang/zh.yaml index 515975dc8..802c8f67e 100644 --- a/backend/i18n/lang/zh.yaml +++ b/backend/i18n/lang/zh.yaml @@ -11,4 +11,8 @@ ErrStructTransform: "类型转换失败: {{ .detail }}" ErrNotLogin: "用户未登录: {{ .detail }}" ErrNotSafety: "当前用户登录状态不安全: {{ .detail }}" ErrPasswordExpired: "当前密码已过期: {{ .detail }}" -ErrNotSupportType: "系统暂不支持当前类型: {{ .detail }}" \ No newline at end of file +ErrNotSupportType: "系统暂不支持当前类型: {{ .detail }}" + + +#app +ErrPortInUsed: "{{ .detail }} 已被占用!" diff --git a/frontend/src/views/host/file-management/index.vue b/frontend/src/views/host/file-management/index.vue index 0ca6a9e50..c93eadebb 100644 --- a/frontend/src/views/host/file-management/index.vue +++ b/frontend/src/views/host/file-management/index.vue @@ -115,7 +115,7 @@ :label="$t('file.updateTime')" prop="modTime" :formatter="dateFromat" - min-width="100" + min-width="150" show-overflow-tooltip >