Skip to content

Commit

Permalink
feat: message.go remove panics and return default error value with er…
Browse files Browse the repository at this point in the history
…ror str
  • Loading branch information
SoulPancake committed Jun 3, 2024
1 parent 691a533 commit f19c080
Showing 1 changed file with 46 additions and 19 deletions.
65 changes: 46 additions & 19 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,29 @@ const messageStructSize = int(unsafe.Sizeof(RedisMessage{}))
// Nil represents a Redis Nil message
var Nil = &RedisError{typ: typeNull}

// default values for Errors
const (
defaultErrorBool = false
defaultErrorString = "ERR"
defaultErrorInt64 = int64(0)
defaultErrorFloat64 = float64(0.0)
)

var defaultErrorRedisMessageSlice []RedisMessage
var defaultErrorXRangeEntrySliceMap = make(map[string][]XRangeEntry)
var defaultErrorZScoreSliceMap = make(map[string][]ZScore)
var defaultErrorZScore = ZScore{}
var defaultErrorRedisMessageMap = make(map[string]RedisMessage)
var defaultErrorScanEntry = ScanEntry{}
var defaultErrorStrMap = make(map[string]string)
var defaultErrorStrMapSlice = make([]map[string]string, 0)
var defaultErrorIntMap = make(map[string]int64)
var defaultErrorKeyValues = KeyValues{}
var defaultErrorKeyZScores = KeyZScores{}
var defaultErrorFtSearchDocSlice = make([]FtSearchDoc, 0)
var defaultErrorResp3Map = make(map[string]RedisMessage)
var defaultErrorAny = any(nil)

// IsRedisNil is a handy method to check if error is a redis nil response.
// All redis nil response returns as an error.
func IsRedisNil(err error) bool {
Expand Down Expand Up @@ -571,7 +594,8 @@ func (m *RedisMessage) ToString() (val string, err error) {
}
if m.IsInt64() || m.values != nil {
typ := m.typ
panic(fmt.Sprintf("redis message type %s is not a string", typeNames[typ]))
return defaultErrorString,
fmt.Errorf("redis message type %s is not a string", typeNames[typ])
}
return m.string, m.Error()
}
Expand Down Expand Up @@ -645,7 +669,8 @@ func (m *RedisMessage) AsBool() (val bool, err error) {
return
default:
typ := m.typ
panic(fmt.Sprintf("redis message type %s is not a int, string or bool", typeNames[typ]))
return defaultErrorBool,
fmt.Errorf("redis message type %s is not a int, string or bool", typeNames[typ])
}
}

Expand All @@ -670,7 +695,7 @@ func (m *RedisMessage) ToInt64() (val int64, err error) {
return 0, err
}
typ := m.typ
panic(fmt.Sprintf("redis message type %s is not a RESP3 int64", typeNames[typ]))
return defaultErrorInt64, fmt.Errorf("redis message type %s is not a RESP3 int64", typeNames[typ])
}

// ToBool check if message is a redis RESP3 bool response, and return it
Expand All @@ -682,7 +707,7 @@ func (m *RedisMessage) ToBool() (val bool, err error) {
return false, err
}
typ := m.typ
panic(fmt.Sprintf("redis message type %s is not a RESP3 bool", typeNames[typ]))
return defaultErrorBool, fmt.Errorf("redis message type %s is not a RESP3 bool", typeNames[typ])
}

// ToFloat64 check if message is a redis RESP3 double response, and return it
Expand All @@ -694,7 +719,7 @@ func (m *RedisMessage) ToFloat64() (val float64, err error) {
return 0, err
}
typ := m.typ
panic(fmt.Sprintf("redis message type %s is not a RESP3 float64", typeNames[typ]))
return defaultErrorFloat64, fmt.Errorf("redis message type %s is not a RESP3 float64", typeNames[typ])
}

// ToArray check if message is a redis array/set response, and return it
Expand All @@ -706,7 +731,7 @@ func (m *RedisMessage) ToArray() ([]RedisMessage, error) {
return nil, err
}
typ := m.typ
panic(fmt.Sprintf("redis message type %s is not a array", typeNames[typ]))
return defaultErrorRedisMessageSlice, fmt.Errorf("redis message type %s is not a array", typeNames[typ])
}

// AsStrSlice check if message is a redis array/set response, and convert to []string.
Expand Down Expand Up @@ -853,7 +878,7 @@ func (m *RedisMessage) AsXRead() (ret map[string][]XRangeEntry, err error) {
return ret, nil
}
typ := m.typ
panic(fmt.Sprintf("redis message type %s is not a map/array/set or its length is not even", typeNames[typ]))
return defaultErrorXRangeEntrySliceMap, fmt.Errorf("redis message type %s is not a map/array/set", typeNames[typ])
}

// ZScore is the element type of ZRANGE WITHSCORES, ZDIFF WITHSCORES and ZPOPMAX command response
Expand All @@ -869,7 +894,7 @@ func toZScore(values []RedisMessage) (s ZScore, err error) {
}
return s, err
}
panic("redis message is not a map/array/set or its length is not 2")
return defaultErrorZScore, fmt.Errorf("redis message is not a map/array/set or its length is not 2")
}

// AsZScore converts ZPOPMAX and ZPOPMIN command with count 1 response to a single ZScore
Expand Down Expand Up @@ -925,7 +950,7 @@ func (m *RedisMessage) AsScanEntry() (e ScanEntry, err error) {
return e, err
}
typ := m.typ
panic(fmt.Sprintf("redis message type %s is not a scan response or its length is not at least 2", typeNames[typ]))
return defaultErrorScanEntry, fmt.Errorf("redis message type %s is not a scan response or its length is not at least 2", typeNames[typ])
}

// AsMap check if message is a redis array/set response, and convert to map[string]RedisMessage
Expand All @@ -937,7 +962,7 @@ func (m *RedisMessage) AsMap() (map[string]RedisMessage, error) {
return toMap(m.values), nil
}
typ := m.typ
panic(fmt.Sprintf("redis message type %s is not a map/array/set or its length is not even", typeNames[typ]))
return defaultErrorRedisMessageMap, fmt.Errorf("redis message type %s is not a map/array/set or its length is not even", typeNames[typ])
}

// AsStrMap check if message is a redis map/array/set response, and convert to map[string]string.
Expand All @@ -956,7 +981,7 @@ func (m *RedisMessage) AsStrMap() (map[string]string, error) {
return r, nil
}
typ := m.typ
panic(fmt.Sprintf("redis message type %s is not a map/array/set or its length is not even", typeNames[typ]))
return defaultErrorStrMap, fmt.Errorf("redis message type %s is not a map/array/set or its length is not even", typeNames[typ])
}

// AsIntMap check if message is a redis map/array/set response, and convert to map[string]int64.
Expand Down Expand Up @@ -984,7 +1009,7 @@ func (m *RedisMessage) AsIntMap() (map[string]int64, error) {
return r, nil
}
typ := m.typ
panic(fmt.Sprintf("redis message type %s is not a map/array/set or its length is not even", typeNames[typ]))
return defaultErrorIntMap, fmt.Errorf("redis message type %s is not a map/array/set or its length is not even", typeNames[typ])
}

type KeyValues struct {
Expand All @@ -1002,7 +1027,7 @@ func (m *RedisMessage) AsLMPop() (kvs KeyValues, err error) {
return
}
typ := m.typ
panic(fmt.Sprintf("redis message type %s is not a LMPOP response", typeNames[typ]))
return defaultErrorKeyValues, fmt.Errorf("redis message type %s is not a LMPOP response", typeNames[typ])
}

type KeyZScores struct {
Expand All @@ -1020,7 +1045,7 @@ func (m *RedisMessage) AsZMPop() (kvs KeyZScores, err error) {
return
}
typ := m.typ
panic(fmt.Sprintf("redis message type %s is not a ZMPOP response", typeNames[typ]))
return defaultErrorKeyZScores, fmt.Errorf("redis message type %s is not a ZMPOP response", typeNames[typ])
}

type FtSearchDoc struct {
Expand Down Expand Up @@ -1098,7 +1123,7 @@ func (m *RedisMessage) AsFtSearch() (total int64, docs []FtSearchDoc, err error)
return
}
typ := m.typ
panic(fmt.Sprintf("redis message type %s is not a FT.SEARCH response", typeNames[typ]))
return defaultErrorInt64, defaultErrorFtSearchDocSlice, fmt.Errorf("redis message type %s is not a FT.SEARCH response", typeNames[typ])
}

func (m *RedisMessage) AsFtAggregate() (total int64, docs []map[string]string, err error) {
Expand Down Expand Up @@ -1139,7 +1164,7 @@ func (m *RedisMessage) AsFtAggregate() (total int64, docs []map[string]string, e
return
}
typ := m.typ
panic(fmt.Sprintf("redis message type %s is not a FT.AGGREGATE response", typeNames[typ]))
return defaultErrorInt64, defaultErrorStrMapSlice, fmt.Errorf("redis message type %s is not a FT.AGGREGATE response", typeNames[typ])
}

func (m *RedisMessage) AsFtAggregateCursor() (cursor, total int64, docs []map[string]string, err error) {
Expand Down Expand Up @@ -1212,7 +1237,7 @@ func (m *RedisMessage) ToMap() (map[string]RedisMessage, error) {
return nil, err
}
typ := m.typ
panic(fmt.Sprintf("redis message type %s is not a RESP3 map", typeNames[typ]))
return defaultErrorResp3Map, fmt.Errorf("redis message type %s is not a RESP3 map", typeNames[typ])
}

// ToAny turns message into go any value
Expand Down Expand Up @@ -1251,7 +1276,7 @@ func (m *RedisMessage) ToAny() (any, error) {
return vs, nil
}
typ := m.typ
panic(fmt.Sprintf("redis message type %s is not a supported in ToAny", typeNames[typ]))
return defaultErrorAny, fmt.Errorf("redis message type %s is not a supported in ToAny", typeNames[typ])
}

// IsCacheHit check if message is from client side cache
Expand Down Expand Up @@ -1319,7 +1344,9 @@ func toMap(values []RedisMessage) map[string]RedisMessage {
continue
}
typ := values[i].typ
panic(fmt.Sprintf("redis message type %s as map key is not supported", typeNames[typ]))
return map[string]RedisMessage{
"error": {typ: typeSimpleErr, string: fmt.Sprintf("redis message type %s as map key is not supported", typeNames[typ])},
}
}
return r
}
Expand Down

0 comments on commit f19c080

Please sign in to comment.