- big big refactor.
This commit is contained in:
@@ -2,31 +2,43 @@ package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type ErrorWithCode struct {
|
||||
level int
|
||||
str string
|
||||
code string
|
||||
intCode int
|
||||
}
|
||||
|
||||
func NewErrorCode(err, code string) *ErrorWithCode {
|
||||
return &ErrorWithCode{
|
||||
str: err,
|
||||
code: code,
|
||||
intCode: Str2Int(code),
|
||||
func NewErrorCode(err, code string, level ...int) *ErrorWithCode {
|
||||
retVal := &ErrorWithCode{
|
||||
str: err,
|
||||
code: code,
|
||||
}
|
||||
retVal.intCode, _ = strconv.Atoi(code)
|
||||
if len(level) > 0 {
|
||||
retVal.level = level[0]
|
||||
}
|
||||
|
||||
return retVal
|
||||
}
|
||||
|
||||
func NewErrorIntCode(err string, code int) *ErrorWithCode {
|
||||
return &ErrorWithCode{
|
||||
str: err,
|
||||
code: Int2Str(code),
|
||||
intCode: code,
|
||||
}
|
||||
func NewErrorIntCode(err string, code int, level ...int) *ErrorWithCode {
|
||||
return NewErrorCode(err, Int2Str(code), level...)
|
||||
}
|
||||
|
||||
func (e *ErrorWithCode) Error() string {
|
||||
return fmt.Sprintf("%s, code:%s", e.str, e.code)
|
||||
return fmt.Sprintf("level:%d, str:%s, code:%s", e.level, e.str, e.code)
|
||||
}
|
||||
|
||||
func (e *ErrorWithCode) String() string {
|
||||
return e.Error()
|
||||
}
|
||||
|
||||
func (e *ErrorWithCode) Level() int {
|
||||
return e.level
|
||||
}
|
||||
|
||||
func (e *ErrorWithCode) Code() string {
|
||||
|
||||
@@ -92,7 +92,11 @@ func DictKeysMan(data interface{}, keysToRemove []string, keysToKeep []string) i
|
||||
func UnmarshalUseNumber(data []byte, result interface{}) error {
|
||||
d := json.NewDecoder(bytes.NewReader(data))
|
||||
d.UseNumber()
|
||||
return d.Decode(result)
|
||||
err := d.Decode(result)
|
||||
if err != nil {
|
||||
baseapi.SugarLogger.Errorf("decode data:%v, error:%v", string(data), err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func MustMarshal(obj interface{}) []byte {
|
||||
@@ -112,12 +116,18 @@ func Bool2String(value bool) string {
|
||||
}
|
||||
|
||||
func Str2Int(str string) int {
|
||||
retVal, _ := strconv.Atoi(str)
|
||||
retVal, err := strconv.Atoi(str)
|
||||
if err != nil {
|
||||
baseapi.SugarLogger.Errorf("error when convert %s to int", str)
|
||||
}
|
||||
return retVal
|
||||
}
|
||||
|
||||
func Str2Int64(str string) int64 {
|
||||
retVal, _ := strconv.ParseInt(str, 10, 64)
|
||||
retVal, err := strconv.ParseInt(str, 10, 64)
|
||||
if err != nil {
|
||||
baseapi.SugarLogger.Errorf("error when convert %s to int64", str)
|
||||
}
|
||||
return retVal
|
||||
}
|
||||
|
||||
@@ -150,15 +160,15 @@ func Timestamp2Str(timestamp int64) string {
|
||||
return time.Unix(timestamp/1000, 0).Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
func HttpResponse2Json(response *http.Response) (map[string]interface{}, error) {
|
||||
func HTTPResponse2Json(response *http.Response) (map[string]interface{}, error) {
|
||||
var jsonResult map[string]interface{}
|
||||
bodyData, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
baseapi.SugarLogger.Errorf("ioutil.ReadAll error:%v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = UnmarshalUseNumber(bodyData, &jsonResult)
|
||||
if err != nil {
|
||||
if err = UnmarshalUseNumber(bodyData, &jsonResult); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return jsonResult, nil
|
||||
|
||||
@@ -73,4 +73,7 @@ func TestJson(t *testing.T) {
|
||||
unmarshStr(t, jsonStr, &obj2)
|
||||
unmarshStr(t, jsonStr, &obj3)
|
||||
unmarshStr(t, jsonStr, &obj4)
|
||||
t.Skip("skip the flollowing")
|
||||
t.Fatal(1)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user