Skip to content

Commit

Permalink
Log more useful details when proxy is not running
Browse files Browse the repository at this point in the history
If the proxy wasn't running, the errors you'd get when
trying to download would just be a massive stack trace.
  • Loading branch information
Chris Saunders committed Mar 11, 2015
1 parent 7f36c0b commit 0ef2ee8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 18 deletions.
8 changes: 8 additions & 0 deletions commands/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
8 changes: 3 additions & 5 deletions commands/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
14 changes: 7 additions & 7 deletions commands/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,29 @@ 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}
assets <- asset
}
close(assets)
}()
return assets, nil
return assets, errs
}

func removeAndUpload(assets chan phoenix.Asset, assetEvents chan phoenix.AssetEvent) {
Expand Down
1 change: 0 additions & 1 deletion error_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
14 changes: 11 additions & 3 deletions theme_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,32 @@ 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
}

for _, asset := range assets["assets"] {
results <- asset
}
close(results)
close(errs)
}()
return
}
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions theme_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand All @@ -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)
}

Expand Down

0 comments on commit 0ef2ee8

Please sign in to comment.