CR: export error string generation to a function

This commit is contained in:
Guy Lewin 2020-02-11 16:57:38 +02:00
parent 9440bc5d72
commit 98fa3855bd
4 changed files with 13 additions and 16 deletions

View File

@ -31,6 +31,7 @@ import (
"github.com/fatedier/frp/server/controller"
"github.com/fatedier/frp/server/proxy"
"github.com/fatedier/frp/server/stats"
"github.com/fatedier/frp/utils/util"
"github.com/fatedier/frp/utils/version"
"github.com/fatedier/frp/utils/xlog"
@ -438,12 +439,8 @@ func (ctl *Control) manager() {
ProxyName: m.ProxyName,
}
if err != nil {
if ctl.serverCfg.DetailedErrorsToClient {
resp.Error = err.Error()
} else {
resp.Error = fmt.Sprintf("new proxy [%s] error", m.ProxyName)
}
xl.Warn("new proxy [%s] error: %v", m.ProxyName, err)
resp.Error = util.GenerateResponseErrorString(fmt.Sprintf("new proxy [%s] error", m.ProxyName), err, ctl.serverCfg.DetailedErrorsToClient)
} else {
resp.RemoteAddr = remoteAddr
xl.Info("new proxy [%s] success", m.ProxyName)

View File

@ -320,13 +320,9 @@ func (svr *Service) HandleListener(l net.Listener) {
// Otherwise send success message in control's work goroutine.
if err != nil {
xl.Warn("register control error: %v", err)
errStr := "register control error"
if svr.cfg.DetailedErrorsToClient {
errStr = err.Error()
}
msg.WriteMsg(conn, &msg.LoginResp{
Version: version.Full(),
Error: errStr,
Error: util.GenerateResponseErrorString("register control error", err, svr.cfg.DetailedErrorsToClient),
})
conn.Close()
}
@ -335,13 +331,9 @@ func (svr *Service) HandleListener(l net.Listener) {
case *msg.NewVisitorConn:
if err = svr.RegisterVisitorConn(conn, m); err != nil {
xl.Warn("register visitor conn error: %v", err)
errStr := "register visitor conn error"
if svr.cfg.DetailedErrorsToClient {
errStr = err.Error()
}
msg.WriteMsg(conn, &msg.NewVisitorConnResp{
ProxyName: m.ProxyName,
Error: errStr,
Error: util.GenerateResponseErrorString("register visitor conn error", err, svr.cfg.DetailedErrorsToClient),
})
conn.Close()
} else {

View File

@ -101,3 +101,11 @@ func ParseRangeNumbers(rangeStr string) (numbers []int64, err error) {
}
return
}
func GenerateResponseErrorString(summary string, err error, detailed bool) string {
if detailed {
return err.Error()
} else {
return summary
}
}