Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Panic -> Error migration #555

Merged
merged 3 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,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 "",
fmt.Errorf("redis message type %s is not a string", typeNames[typ])
}
return m.string, m.Error()
}
Expand Down Expand Up @@ -645,7 +646,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 false,
fmt.Errorf("redis message type %s is not a int, string or bool", typeNames[typ])
}
}

Expand All @@ -670,7 +672,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 0, 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 +684,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 false, 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 +696,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 0, 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 +708,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 nil, 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 +855,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 nil, 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 +871,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 ZScore{}, 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 +927,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 ScanEntry{}, 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 +939,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 nil, 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 +958,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 nil, 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 +986,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 nil, 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 +1004,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 KeyValues{}, fmt.Errorf("redis message type %s is not a LMPOP response", typeNames[typ])
}

type KeyZScores struct {
Expand All @@ -1020,7 +1022,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 KeyZScores{}, fmt.Errorf("redis message type %s is not a ZMPOP response", typeNames[typ])
}

type FtSearchDoc struct {
Expand Down Expand Up @@ -1098,7 +1100,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 0, nil, 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 +1141,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 0, nil, 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 +1214,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 nil, 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 +1253,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 nil, 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 +1321,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
Loading