[environ] change interface of Env.Iter

While re-writing run_isolated.py to go, I found environ package.
And if Env.Iter returns error, it is useful than nothing returned.

Bug: 962804
Change-Id: Ie1c9a167b8d63902a82d5bf490b8fefacb78e3cf
Reviewed-on: https://chromium-review.googlesource.com/c/infra/luci/luci-go/+/1616710
Reviewed-by: Robbie Iannucci <iannucci@chromium.org>
Reviewed-by: Yoshisato Yanagisawa <yyanagisawa@google.com>
Commit-Queue: Takuto Ikuta <tikuta@chromium.org>
Auto-Submit: Takuto Ikuta <tikuta@chromium.org>
diff --git a/common/system/environ/environment.go b/common/system/environ/environment.go
index d709151..078c971 100644
--- a/common/system/environ/environment.go
+++ b/common/system/environ/environment.go
@@ -147,11 +147,11 @@
 // the callback function, fn, for each key/value pair. If fn returns true, the
 // key is removed from the environment.
 func (e *Env) RemoveMatch(fn func(k, v string) bool) {
-	e.iterate(func(realKey, k, v string) bool {
+	e.iterate(func(realKey, k, v string) error {
 		if fn(k, v) {
 			delete(e.env, realKey)
 		}
-		return true
+		return nil
 	})
 }
 
@@ -246,27 +246,28 @@
 }
 
 // Internal iteration function. Invokes cb for every entry in the environment.
-// If the callback returns false, iteration stops.
+// If the callback returns error, iteration stops and returns that error.
 //
 // It is safe to mutate the environment map during iteration.
 //
 // realKey is the real, normalized map key. envKey and envValue are the split
 // map value.
-func (e *Env) iterate(cb func(realKey, envKey, envValue string) bool) {
+func (e *Env) iterate(cb func(realKey, envKey, envValue string) error) error {
 	for k, v := range e.env {
 		envKey, envValue := Split(v)
-		if !cb(k, envKey, envValue) {
-			break
+		if err := cb(k, envKey, envValue); err != nil {
+			return err
 		}
 	}
+	return nil
 }
 
 // Iter iterates through all of the key/value pairs in Env and invokes the
 // supplied callback, cb, for each element.
 //
-// If the callback returns false, iteration will stop immediately.
-func (e Env) Iter(cb func(k, v string) bool) {
-	e.iterate(func(_, k, v string) bool { return cb(k, v) })
+// If the callback returns error, iteration will stop immediately.
+func (e Env) Iter(cb func(k, v string) error) error {
+	return e.iterate(func(_, k, v string) error { return cb(k, v) })
 }
 
 // Split splits the supplied environment variable value into a key/value pair.
diff --git a/common/system/environ/environment_test.go b/common/system/environ/environment_test.go
index 2f6e8de..3ad93a1 100644
--- a/common/system/environ/environment_test.go
+++ b/common/system/environ/environment_test.go
@@ -164,10 +164,10 @@
 
 			Convey(`Can perform iteration`, func() {
 				buildMap := make(map[string]string)
-				env.Iter(func(k, v string) bool {
+				So(env.Iter(func(k, v string) error {
 					buildMap[k] = v
-					return true
-				})
+					return nil
+				}), ShouldBeNil)
 				So(env.Map(), ShouldResemble, buildMap)
 			})