code refactor: extract _s and _e func

This commit is contained in:
UlricQin 2021-07-15 18:46:40 +08:00
parent 473239cc9a
commit 9758e55b72
2 changed files with 14 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package http
import (
"fmt"
"net/http"
"strconv"
"strings"
@ -15,12 +16,20 @@ import (
const defaultLimit = 20
func _e(format string, a ...interface{}) error {
return fmt.Errorf(_s(format, a...))
}
func _s(format string, a ...interface{}) string {
return i18n.Sprintf(format, a...)
}
func dangerous(v interface{}, code ...int) {
ierr.Dangerous(v, code...)
}
func bomb(code int, format string, a ...interface{}) {
ierr.Bomb(code, i18n.Sprintf(format, a...))
ierr.Bomb(code, _s(format, a...))
}
func bind(c *gin.Context, ptr interface{}) {
@ -138,7 +147,7 @@ func renderMessage(c *gin.Context, v interface{}, statusCode ...int) {
switch t := v.(type) {
case string:
c.JSON(code, gin.H{"err": i18n.Sprintf(t)})
c.JSON(code, gin.H{"err": _s(t)})
case error:
c.JSON(code, gin.H{"err": t.Error()})
}

View File

@ -9,7 +9,6 @@ import (
"github.com/didi/nightingale/v5/cache"
"github.com/didi/nightingale/v5/models"
"github.com/didi/nightingale/v5/pkg/i18n"
)
type collectRuleForm struct {
@ -244,15 +243,15 @@ func includeIllegalChar(s string) bool {
// 生成返回错误信息
func genErrMsg(sign string) string {
return i18n.Sprintf("regular match failed, please check your configuration:[%s]", sign)
return _s("regular match failed, please check your configuration:[%s]", sign)
}
// 生成子串匹配错误信息
func genSubErrMsg(sign string) string {
return i18n.Sprintf("regular match was successful. according to the configuration, it does not get the substring inside(), please check your configuration:[%s]", sign)
return _s("regular match was successful. according to the configuration, it does not get the substring inside(), please check your configuration:[%s]", sign)
}
// 生成子串匹配错误信息
func genIllegalCharErrMsg() string {
return i18n.Sprintf(`key or value of tag contains illegal characters:[:,/=\r\n\t]`)
return _s(`key or value of tag contains illegal characters:[:,/=\r\n\t]`)
}