pull try out of Add

deferring a recovery handler is normal in Go but opaque. Pulling out a helper for this (`try`) makes it more obvious that this is just another "if this operation fails, return error" case.
This commit is contained in:
2024-09-29 12:41:49 -07:00
parent 68cab7d2be
commit a237fa81bf
2 changed files with 26 additions and 11 deletions

View File

@@ -220,7 +220,7 @@ var ErrFieldSetPanic = fmt.Errorf("%w: panic when setting", ErrBadFieldLabel)
// Use a FieldLabel to add an amount to the matching field. If no such
// field can be found, the field is not exported, or the field is not
// of type float64, this returns an error ad does not change any values.
func (k *KoboldMine) Add(which FieldLabel, amount float64) (finalErr error) {
func (k *KoboldMine) Add(which FieldLabel, amount float64) error {
kv := reflect.ValueOf(k).Elem()
f := kv.FieldByName(string(which))
if !f.IsValid() {
@@ -230,15 +230,8 @@ func (k *KoboldMine) Add(which FieldLabel, amount float64) (finalErr error) {
return fmt.Errorf("cannot add %f to field %q: %w", amount, which, ErrFieldNotFloat)
}
defer func() {
if r := recover(); r != nil {
if e, ok := r.(error); ok {
finalErr = fmt.Errorf("could not add %f to field %q: %w: %w", amount, which, ErrFieldSetPanic, e)
} else {
finalErr = fmt.Errorf("could not add %f to field %q: %w: %v", amount, which, ErrFieldSetPanic, r)
}
}
}()
f.SetFloat(f.Float() + amount)
if err := try(func() { f.SetFloat(f.Float() + amount) }); err != nil {
return fmt.Errorf("could not add %f to field %q: %w", amount, which, err)
}
return nil
}