fix: 增加buserr实现 修改端口占用提示

This commit is contained in:
zhengkunwang223 2022-12-01 15:48:40 +08:00 committed by zhengkunwang223
parent 8f6b664a53
commit 8bc6545ce2
8 changed files with 67 additions and 16 deletions

View File

@ -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))

View File

@ -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
}

32
backend/buserr/errors.go Normal file
View File

@ -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,
}
}

View File

@ -41,3 +41,8 @@ var (
ErrTypePasswordExpired = "ErrPasswordExpired"
ErrTypeNotSafety = "ErrNotSafety"
)
// app
var (
ErrPortInUsed = "ErrPortInUsed"
)

View File

@ -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

View File

@ -12,3 +12,7 @@ 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 }}"
#app
ErrPortInUsed: "{{ .detail }} 端口已被占用"

View File

@ -12,3 +12,7 @@ ErrNotLogin: "用户未登录: {{ .detail }}"
ErrNotSafety: "当前用户登录状态不安全: {{ .detail }}"
ErrPasswordExpired: "当前密码已过期: {{ .detail }}"
ErrNotSupportType: "系统暂不支持当前类型: {{ .detail }}"
#app
ErrPortInUsed: "{{ .detail }} 已被占用!"

View File

@ -115,7 +115,7 @@
:label="$t('file.updateTime')"
prop="modTime"
:formatter="dateFromat"
min-width="100"
min-width="150"
show-overflow-tooltip
></el-table-column>