From 192a85afb8ae1c0ffcbf3a4b80c52a4bf3f73043 Mon Sep 17 00:00:00 2001 From: bytedream Date: Fri, 13 May 2022 19:53:16 +0200 Subject: [PATCH] Use fmt.Errorf instead of errors.New & new invalid session id error message --- crunchyroll.go | 9 ++++----- stream.go | 3 +-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/crunchyroll.go b/crunchyroll.go index 6b1854f..00d3123 100644 --- a/crunchyroll.go +++ b/crunchyroll.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "errors" "fmt" "io" "net/http" @@ -131,15 +130,15 @@ func LoginWithSessionID(sessionID string, locale LOCALE, client *http.Client) (* if err = json.NewDecoder(resp.Body).Decode(&jsonBody); err != nil { return nil, fmt.Errorf("failed to parse start session with session id response: %w", err) } - if _, ok := jsonBody["message"]; ok { - return nil, errors.New("invalid session id") + if isError, ok := jsonBody["error"]; ok && isError.(bool) { + return nil, fmt.Errorf("invalid session id (%s): %s", jsonBody["message"].(string), jsonBody["code"]) } data := jsonBody["data"].(map[string]interface{}) crunchy.Config.CountryCode = data["country_code"].(string) user := data["user"] if user == nil { - return nil, errors.New("invalid session id, user is not logged in") + return nil, fmt.Errorf("invalid session id, user is not logged in") } if user.(map[string]interface{})["premium"] == "" { crunchy.Config.Premium = false @@ -346,7 +345,7 @@ func (c *Crunchyroll) FindVideoByName(seriesName string) (Video, error) { } else if len(m) > 0 { return m[0], nil } - return nil, errors.New("no series or movie could be found") + return nil, fmt.Errorf("no series or movie could be found") } // FindEpisodeByName finds an episode by its crunchyroll series name and episode title. diff --git a/stream.go b/stream.go index d8957e6..7505a24 100644 --- a/stream.go +++ b/stream.go @@ -2,7 +2,6 @@ package crunchyroll import ( "encoding/json" - "errors" "fmt" "github.com/grafov/m3u8" "regexp" @@ -83,7 +82,7 @@ func fromVideoStreams(crunchy *Crunchyroll, endpoint string) (streams []*Stream, if len(jsonBody) == 0 { // this may get thrown when the crunchyroll account has just a normal account and not one with premium - return nil, errors.New("no stream available") + return nil, fmt.Errorf("no stream available") } audioLocale := jsonBody["audio_locale"].(string)