zalgonoise/eljoth-go-code-review

internal/repo/memdb: FindByCode method improvements

zalgonoise opened this issue · 0 comments

  • The returned error should not have Cupon capitalized (Go errors should start with lowercase unless it's an acronym or similar)
  • The returned error is not actually leveraging fmt.Errorf(). It should:
    • Actually be a declared error (var ErrNotFound = errors.New("not found")
    • The fmt.Errorf() call should wrap this error with a custom message pointing to the input cupon: return nil, fmt.Errorf("%w: no cupon with code %s", ErrNotFound, code)
  • While it is fine as is, the method could be simplified to:
var ErrNotFound = errors.New("not found")

func (r *Repository) FindByCode(code string) (*entity.Coupon, error) {
	if coupon, ok := r.entries[code]; ok {
		return &coupon, nil
	}
	return nil, fmt.Errorf("%w: no cupon with code %s", ErrNotFound, code)
}