package util

import (
	"fmt"
	"net/http"
	"sync"
)

type StatusGroup struct {
	sync.WaitGroup
	sync.Mutex
	Status int
	Err    error
}

func NewStatusGroup() *StatusGroup {
	return &StatusGroup{Status: http.StatusOK}
}

func (sg *StatusGroup) Done(status int, err error) {
	if status == 0 {
		// An early exit.
		status = http.StatusInternalServerError
		err = fmt.Errorf("Unkown errors occur in an goroutine.")
	}

	sg.Lock()
	if sg.Err == nil && err != nil {
		sg.Status = status
		sg.Err = err
	}
	sg.Unlock()
	sg.WaitGroup.Done()
}

func (sg *StatusGroup) Wait() (int, error) {
	sg.WaitGroup.Wait()
	return sg.Status, sg.Err
}

package util

import (
	"net/http"
	"sync"
)

type StatusGroup struct {
	sync.WaitGroup
	sync.Mutex
	Status int
	Err    error
}

func NewStatusGroup() *StatusGroup {
	return &StatusGroup{Status: http.StatusOK}
}

func (sg *StatusGroup) Done(status int, err error) {
	sg.Lock()
	if sg.Err == nil && err != nil {
		if status == 0 {
			// Usually caused by an early exit.
			status = http.StatusInternalServerError
		}
		sg.Status = status
		sg.Err = err
	}
	sg.Unlock()
	sg.WaitGroup.Done()
}

func (sg *StatusGroup) Wait() (int, error) {
	sg.WaitGroup.Wait()
	return sg.Status, sg.Err
}

--- 
+++ 
@@ -1,6 +1,7 @@
 package util
 
 import (
+	"fmt"
 	"net/http"
 	"sync"
 )
@@ -17,12 +18,14 @@
 }
 
 func (sg *StatusGroup) Done(status int, err error) {
+	if status == 0 {
+		// An early exit.
+		status = http.StatusInternalServerError
+		err = fmt.Errorf("Unkown errors occur in an goroutine.")
+	}
+
 	sg.Lock()
 	if sg.Err == nil && err != nil {
-		if status == 0 {
-			// Usually caused by an early exit.
-			status = http.StatusInternalServerError
-		}
 		sg.Status = status
 		sg.Err = err
 	}
Fix a minor bug in StatusGroup.

err was not set when status == 0
