Use fmt.Errorf instead of errors.New & new invalid session id error message

This commit is contained in:
bytedream 2022-05-13 19:53:16 +02:00
parent f046b68371
commit 192a85afb8
2 changed files with 5 additions and 7 deletions

View file

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "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 { 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) return nil, fmt.Errorf("failed to parse start session with session id response: %w", err)
} }
if _, ok := jsonBody["message"]; ok { if isError, ok := jsonBody["error"]; ok && isError.(bool) {
return nil, errors.New("invalid session id") return nil, fmt.Errorf("invalid session id (%s): %s", jsonBody["message"].(string), jsonBody["code"])
} }
data := jsonBody["data"].(map[string]interface{}) data := jsonBody["data"].(map[string]interface{})
crunchy.Config.CountryCode = data["country_code"].(string) crunchy.Config.CountryCode = data["country_code"].(string)
user := data["user"] user := data["user"]
if user == nil { 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"] == "" { if user.(map[string]interface{})["premium"] == "" {
crunchy.Config.Premium = false crunchy.Config.Premium = false
@ -346,7 +345,7 @@ func (c *Crunchyroll) FindVideoByName(seriesName string) (Video, error) {
} else if len(m) > 0 { } else if len(m) > 0 {
return m[0], nil 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. // FindEpisodeByName finds an episode by its crunchyroll series name and episode title.

View file

@ -2,7 +2,6 @@ package crunchyroll
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"github.com/grafov/m3u8" "github.com/grafov/m3u8"
"regexp" "regexp"
@ -83,7 +82,7 @@ func fromVideoStreams(crunchy *Crunchyroll, endpoint string) (streams []*Stream,
if len(jsonBody) == 0 { if len(jsonBody) == 0 {
// this may get thrown when the crunchyroll account has just a normal account and not one with premium // 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) audioLocale := jsonBody["audio_locale"].(string)