diff --git a/commands/common.go b/commands/common.go index 44a5ec30..850de72e 100644 --- a/commands/common.go +++ b/commands/common.go @@ -64,3 +64,11 @@ func toClientAndFilesAsync(args map[string]interface{}, fn func(phoenix.ThemeCli } return fn(themeClient, filenames) } + +func drainErrors(errs chan error) { + for { + if err := <-errs; err != nil { + phoenix.NotifyError(err) + } + } +} diff --git a/commands/download.go b/commands/download.go index 53a2cd85..98ee5a50 100644 --- a/commands/download.go +++ b/commands/download.go @@ -17,11 +17,9 @@ func Download(client phoenix.ThemeClient, filenames []string) (done chan bool) { done = make(chan bool) if len(filenames) <= 0 { - if assets, err := client.AssetList(); err != nil { - phoenix.NotifyError(err) - } else { - go downloadAllFiles(assets, done) - } + assets, errs := client.AssetList() + go drainErrors(errs) + go downloadAllFiles(assets, done) } else { go downloadFiles(client.Asset, filenames, done) } diff --git a/commands/replace.go b/commands/replace.go index 18577098..ed8fee29 100644 --- a/commands/replace.go +++ b/commands/replace.go @@ -18,21 +18,21 @@ func Replace(client phoenix.ThemeClient, filenames []string) chan bool { fmt.Println(<-messages) } }() + assets, errs := assetList(client, filenames) + go drainErrors(errs) + go removeAndUpload(assets, events) - if assets, err := assetList(client, filenames); err != nil { - phoenix.NotifyError(err) - } else { - go removeAndUpload(assets, events) - } return done } -func assetList(client phoenix.ThemeClient, filenames []string) (chan phoenix.Asset, error) { +func assetList(client phoenix.ThemeClient, filenames []string) (chan phoenix.Asset, chan error) { if len(filenames) == 0 { return client.AssetList() } assets := make(chan phoenix.Asset) + errs := make(chan error) + close(errs) go func() { for _, filename := range filenames { asset := phoenix.Asset{Key: filename} @@ -40,7 +40,7 @@ func assetList(client phoenix.ThemeClient, filenames []string) (chan phoenix.Ass } close(assets) }() - return assets, nil + return assets, errs } func removeAndUpload(assets chan phoenix.Asset, assetEvents chan phoenix.AssetEvent) { diff --git a/error_reporter.go b/error_reporter.go index b7d09d3a..82d5fbc4 100644 --- a/error_reporter.go +++ b/error_reporter.go @@ -25,7 +25,6 @@ func (h HaltExecutionReporter) Report(e error) { c := ConsoleReporter{} libraryInfo := fmt.Sprintf("%s%s%s", MessageSeparator, LibraryInfo(), MessageSeparator) c.Report(errors.New(libraryInfo)) - c.Report(e) log.Fatal(e) } diff --git a/theme_client.go b/theme_client.go index 5f1f3c65..10160f9c 100644 --- a/theme_client.go +++ b/theme_client.go @@ -59,18 +59,24 @@ func (t ThemeClient) GetConfiguration() Configuration { return t.config } -func (t ThemeClient) AssetList() (results chan Asset, err error) { +func (t ThemeClient) AssetList() (results chan Asset, errs chan error) { results = make(chan Asset) + errs = make(chan error) go func() { queryBuilder := func(path string) string { return path } bytes, err := t.query(queryBuilder) + if err != nil { + errs <- err + return + } + var assets map[string][]Asset err = json.Unmarshal(bytes, &assets) if err != nil { - close(results) + errs <- err return } @@ -78,6 +84,7 @@ func (t ThemeClient) AssetList() (results chan Asset, err error) { results <- asset } close(results) + close(errs) }() return } @@ -184,9 +191,10 @@ func (t ThemeClient) query(queryBuilder func(path string) string) ([]byte, error t.config.AddHeaders(req) resp, err := t.client.Do(req) - defer resp.Body.Close() if err != nil { return []byte{}, err + } else { + defer resp.Body.Close() } return ioutil.ReadAll(resp.Body) } diff --git a/theme_client_test.go b/theme_client_test.go index 26d40e44..51c2acfe 100644 --- a/theme_client_test.go +++ b/theme_client_test.go @@ -70,7 +70,7 @@ func TestRetrievingAnAssetList(t *testing.T) { })) client := NewThemeClient(conf(ts)) - assets := client.AssetList() + assets, _ := client.AssetList() assert.Equal(t, 2, count(assets)) } @@ -81,7 +81,7 @@ func TestRetrievingASingleAsset(t *testing.T) { })) client := NewThemeClient(conf(ts)) - asset := client.Asset("assets/foo.txt") + asset, _ := client.Asset("assets/foo.txt") assert.Equal(t, "hello world", asset.Value) }