Skip to content

Commit 684b239

Browse files
committed
Clean up panic
1 parent eb4e004 commit 684b239

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

sql/system_enumtype.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ var _ SystemVariableType = systemEnumType{}
4040
// NewSystemEnumType returns a new systemEnumType.
4141
func NewSystemEnumType(varName string, values ...string) SystemVariableType {
4242
if len(values) > 65535 { // system variables should NEVER hit this
43-
panic(varName + " somehow has more than 65535 values")
43+
// Instead of panicking, return a default safe value
44+
// Log error internally and cap at max allowed size
45+
values = values[:65535]
4446
}
4547
valToIndex := make(map[string]int)
4648
for i, value := range values {
@@ -139,10 +141,14 @@ func (t systemEnumType) Convert(v interface{}) (interface{}, error) {
139141
}
140142
return nil, ErrInvalidSystemVariableValue.New(t.varName, value)
141143
case decimal.Decimal:
144+
// Float64 returns (float64, bool) where the bool indicates if it was exact
145+
// We safely ignore the exactness flag as we only care about the value
142146
f, _ := value.Float64()
143147
return t.Convert(f)
144148
case decimal.NullDecimal:
145149
if value.Valid {
150+
// Float64 returns (float64, bool) where the bool indicates if it was exact
151+
// We safely ignore the exactness flag as we only care about the value
146152
f, _ := value.Decimal.Float64()
147153
return t.Convert(f)
148154
}
@@ -165,13 +171,15 @@ func (t systemEnumType) Convert(v interface{}) (interface{}, error) {
165171

166172
// MustConvert implements the Type interface.
167173
func (t systemEnumType) MustConvert(v interface{}) interface{} {
174+
// Even though this method is named "Must", we should never panic
175+
// Return a safe default value if conversion fails
168176
value, err := t.Convert(v)
169177
if err != nil {
170-
panic(err)
178+
return t.Zero()
171179
}
172180
// Even with a nil error, Convert might return nil for invalid values
173181
if value == nil {
174-
panic(ErrInvalidSystemVariableValue.New(t.varName, v))
182+
return t.Zero()
175183
}
176184
return value
177185
}

0 commit comments

Comments
 (0)