From 23e971907390c41e0ab7f9987201d4817f8e3070 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Mon, 1 May 2017 15:48:26 -0400 Subject: [PATCH] cmd/chihaya: refactor out combination of errors --- cmd/chihaya/main.go | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/cmd/chihaya/main.go b/cmd/chihaya/main.go index 57e92b2..d599a0c 100644 --- a/cmd/chihaya/main.go +++ b/cmd/chihaya/main.go @@ -5,6 +5,7 @@ import ( "os" "os/signal" "runtime/pprof" + "strings" "syscall" log "github.com/Sirupsen/logrus" @@ -83,36 +84,25 @@ func (r *Run) Start(ps storage.PeerStore) error { return nil } +func combineErrors(prefix string, errs []error) error { + var errStrs []string + for _, err := range errs { + errStrs = append(errStrs, err.Error()) + } + + return errors.New(prefix + ": " + strings.Join(errStrs, "; ")) +} + // Stop shuts down an instance of Chihaya. func (r *Run) Stop(keepPeerStore bool) (storage.PeerStore, error) { log.Debug("stopping frontends and prometheus endpoint") if errs := r.sg.Stop(); len(errs) != 0 { - errDelimiter := "; " - errStr := "failed while shutting down frontends: " - - for _, err := range errs { - errStr += err.Error() + errDelimiter - } - - // Remove the last delimiter. - errStr = errStr[0 : len(errStr)-len(errDelimiter)] - - return nil, errors.New(errStr) + return nil, combineErrors("failed while shutting down frontends", errs) } log.Debug("stopping logic") if errs := r.logic.Stop(); len(errs) != 0 { - errDelimiter := "; " - errStr := "failed while shutting down middleware: " - - for _, err := range errs { - errStr += err.Error() + errDelimiter - } - - // Remove the last delimiter. - errStr = errStr[0 : len(errStr)-len(errDelimiter)] - - return nil, errors.New(errStr) + return nil, combineErrors("failed while shutting down middleware", errs) } if !keepPeerStore {