Skip to content

Commit

Permalink
properly log CLI error during playlist conversion (#69)
Browse files Browse the repository at this point in the history
* remove tests💀

* added vscode workspace folder

* chore: properly log error during playlist conversion

* chore: change api error dumping format
  • Loading branch information
prettyirrelevant authored Jul 23, 2023
1 parent 838cb7b commit bbda242
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 143 deletions.
27 changes: 0 additions & 27 deletions kilishi/streaming_platforms/deezer/deezer_test.go

This file was deleted.

4 changes: 3 additions & 1 deletion kilishi/streaming_platforms/deezer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func parseSearchTracksResponse(data deezerAPISearchTrackResponse) []utils.Track

func setupRequestClient(reqClient *req.Client) *req.Client {
return reqClient.
EnableDumpEachRequest().
SetCommonErrorResult(&deezerAPIError{}).
SetResultStateCheckFunc(func(resp *req.Response) req.ResultState {
if resp.StatusCode >= 400 {
Expand All @@ -95,7 +96,8 @@ func setupRequestClient(reqClient *req.Client) *req.Client {
// Edge case: neither an error state response nor a success state response,
// dump content to help troubleshoot.
if !resp.IsSuccessState() {
return fmt.Errorf("bad response, raw dump:\n%s", resp.Dump())
resp.Err = fmt.Errorf("bad status: %s\nraw content:\n%s", resp.Status, resp.Dump())
return nil
}
return nil
}).
Expand Down
30 changes: 0 additions & 30 deletions kilishi/streaming_platforms/spotify/spotify_test.go

This file was deleted.

4 changes: 3 additions & 1 deletion kilishi/streaming_platforms/spotify/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func trackToSearchQuery(track utils.Track) string {

func setupRequestClient(reqClient *req.Client) *req.Client {
return reqClient.
EnableDumpEachRequest().
SetCommonErrorResult(&spotifyAPIError{}).
OnAfterResponse(func(client *req.Client, resp *req.Response) error {
// There is an underlying error, e.g. network error or unmarshal error.
Expand All @@ -103,7 +104,8 @@ func setupRequestClient(reqClient *req.Client) *req.Client {
// Edge case: neither an error state response nor a success state response,
// dump content to help troubleshoot.
if !resp.IsSuccessState() {
return fmt.Errorf("bad response, raw dump:\n%s", resp.Dump())
resp.Err = fmt.Errorf("bad status: %s\nraw content:\n%s", resp.Status, resp.Dump())
return nil
}
return nil
}).
Expand Down
4 changes: 3 additions & 1 deletion kilishi/streaming_platforms/ytmusic/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func parseGetPlaylistResponse(response ytmusicAPIGetPlaylistResponse) utils.Play
func setupRequestClient(reqClient *req.Client, baseURL string) *req.Client {
return reqClient.
SetBaseURL(baseURL).
EnableDumpEachRequest().
SetCommonContentType(utils.ApplicationJSON).
SetCommonErrorResult(&ytmusiAPIError{}).
OnAfterResponse(func(client *req.Client, resp *req.Response) error {
Expand All @@ -64,7 +65,8 @@ func setupRequestClient(reqClient *req.Client, baseURL string) *req.Client {
// Edge case: neither an error state response nor a success state response,
// dump content to help troubleshoot.
if !resp.IsSuccessState() {
return fmt.Errorf("bad response, raw dump:\n%s", resp.Dump())
resp.Err = fmt.Errorf("bad status: %s\nraw content:\n%s", resp.Status, resp.Dump())
return nil
}
return nil
}).
Expand Down
32 changes: 0 additions & 32 deletions kilishi/streaming_platforms/ytmusic/ytmusic_test.go

This file was deleted.

22 changes: 14 additions & 8 deletions shaki/cmd/commands/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/briandowns/spinner"
"github.com/charmbracelet/log"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"

Expand All @@ -31,19 +32,24 @@ var ConvertCmd = &cobra.Command{
source := getStreamingPlatformInput("Source")
destination := getStreamingPlatformInput("Destination")
if source == destination {
panic(ErrPlaylistSourceAndDestinationSame)
log.Error(ErrPlaylistSourceAndDestinationSame)
return
}

// get the playlist
s := spinner.New(spinner.CharSets[11], 10*time.Millisecond)
s.Suffix = fmt.Sprintf(" Fetching playlist %s on %s...\n", url, source)
s.Color("green", "bold") //nolint:errcheck // error check is needless here
setColorErr := s.Color("green", "bold")
if setColorErr != nil {
log.Warn("Unable to set color for spinner", "err", setColorErr)
}
s.Start()

playlist, err := services.GetPlaylist(url, source)
s.Stop()
if err != nil {
panic(fmt.Errorf("an error occurred while fetching the playlist: %s", err.Error()))
log.Error("An error occurred while fetching the playlist", "err", err)
return
}

// now search for each tracks in the playlist
Expand Down Expand Up @@ -81,11 +87,10 @@ var ConvertCmd = &cobra.Command{
}
}

fmt.Printf("Found %d tracks from a total of %d.\n", len(successfulSearches), len(playlist.Data.Tracks))
log.Info("Playlist tracks conversion info:", "Tracks found", len(successfulSearches), "Total number of tracks", len(playlist.Data.Tracks))
if len(playlist.Data.Tracks)-len(successfulSearches) > 0 {
fmt.Println("Details of tracks not found below:")
for _, v := range failedSearchesIndex {
fmt.Printf("Track %d -> Title: %s Artists: %+v\n", v, playlist.Data.Tracks[v].Title, playlist.Data.Tracks[v].Artists)
log.Info("Track not found info:", "Track", v, "Title", playlist.Data.Tracks[v].Title, "Artists", playlist.Data.Tracks[v].Artists)
}
}

Expand All @@ -96,10 +101,11 @@ var ConvertCmd = &cobra.Command{
createPlaylistResp, err := services.CreatePlaylist(playlist.Data.Title, playlist.Data.Description, destination, successfulSearches)
s.Stop()
if err != nil {
panic(err)
log.Error("An error occurred during playlist creation", "err", err)
return
}

fmt.Printf("Playlist created successfully ;)\nURL -> %s\n", createPlaylistResp.Data)
log.Info("Playlist created successfully ;)", "URL", createPlaylistResp.Data)
},
}

Expand Down
9 changes: 0 additions & 9 deletions shaki/cmd/commands/history/history.go

This file was deleted.

2 changes: 0 additions & 2 deletions shaki/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/spf13/cobra"

"github.com/prettyirrelevant/shaki/cmd/commands/convert"
"github.com/prettyirrelevant/shaki/cmd/commands/history"
)

const Version = "0.0.4"
Expand All @@ -26,5 +25,4 @@ func Execute() {

func init() {
rootCmd.AddCommand(convert.ConvertCmd)
rootCmd.AddCommand(&history.HistoryCommand)
}
9 changes: 7 additions & 2 deletions shaki/cmd/services/playlists.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

var reqClient = req.C().
SetBaseURL("https://kilishi.fly.dev/api/v1").
EnableDumpEachRequest().
OnAfterResponse(func(client *req.Client, resp *req.Response) error {
if resp.Err != nil {
return nil
Expand All @@ -17,11 +18,15 @@ var reqClient = req.C().
return nil
}
if !resp.IsSuccessState() {
return fmt.Errorf("bad response, raw dump:\n%s", resp.Dump())
resp.Err = fmt.Errorf("bad status: %s\nraw content:\n%s", resp.Status, resp.String())
return nil
}
return nil
}).
SetCommonRetryCount(2)
SetCommonRetryCount(2).
SetCommonRetryCondition(func(resp *req.Response, err error) bool {
return resp.StatusCode < 500
})

func GetPlaylist(url, platform string) (APIGetPlaylistResponse, error) {
var response APIGetPlaylistResponse
Expand Down
31 changes: 22 additions & 9 deletions shaki/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,50 @@ replace github.com/prettyirrelevant/kilishi => ../kilishi

require (
github.com/briandowns/spinner v1.23.0
github.com/imroc/req/v3 v3.33.2
github.com/imroc/req/v3 v3.36.0
github.com/manifoldco/promptui v0.9.0
github.com/prettyirrelevant/kilishi v0.0.0-00010101000000-000000000000
github.com/spf13/cobra v1.7.0
)

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/caarlos0/env/v7 v7.1.0 // indirect
github.com/charmbracelet/lipgloss v0.7.1 // indirect
github.com/charmbracelet/log v0.2.2 // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/gaukas/godicttls v0.0.3 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/google/pprof v0.0.0-20230406165453-00490a63f317 // indirect
github.com/google/pprof v0.0.0-20230510103437-eeec1cb781c3 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/onsi/ginkgo/v2 v2.9.2 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.1 // indirect
github.com/onsi/ginkgo/v2 v2.9.5 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-19 v0.3.2 // indirect
github.com/quic-go/qtls-go1-20 v0.2.2 // indirect
github.com/quic-go/quic-go v0.33.0 // indirect
github.com/quic-go/quic-go v0.35.0 // indirect
github.com/refraction-networking/utls v1.3.2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/term v0.7.0 // indirect
golang.org/x/term v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.8.0 // indirect
golang.org/x/tools v0.9.1 // indirect
)
Loading

0 comments on commit bbda242

Please sign in to comment.