diff --git a/cache.go b/cache.go index 02ec21a..8add5ef 100644 --- a/cache.go +++ b/cache.go @@ -58,13 +58,18 @@ func saveCredentials(credentials string) { } //Loads Client statistic data and returns Client's AnimeList -func loadData(c *mal.Client, ctx *cli.Context) mal.AnimeList { +func loadData(c *mal.Client, ctx *cli.Context) (mal.AnimeList, error) { var list []*mal.Anime if ctx.GlobalBool("refresh") || cacheNotExist() { - mal.DoFuncWithWaitAnimation("Fetching your list", func() { - list = c.AnimeList(mal.All) - }) + { + var err error + mal.DoFuncWithWaitAnimation("Fetching your list", func() { + list, err = c.AnimeList(mal.All) + }) + return nil, fmt.Errorf("error fetching your list\n%v", err) + } + cacheList(list) cacheClient(c) @@ -75,7 +80,7 @@ func loadData(c *mal.Client, ctx *cli.Context) mal.AnimeList { list = loadCachedList() loadCachedStats(c) } - return list + return list, nil } func cacheNotExist() bool { diff --git a/main.go b/main.go index 6172448..c364d6d 100644 --- a/main.go +++ b/main.go @@ -281,9 +281,9 @@ func loadMAL(ctx *cli.Context) (*mal.Client, mal.AnimeList, error) { return nil, nil, fmt.Errorf("error creating mal.Client") } - list := loadData(c, ctx) + list, err := loadData(c, ctx) - return c, list, nil + return c, list, err } func defaultAction(ctx *cli.Context) error { diff --git a/mal/mal.go b/mal/mal.go index cd2e2ff..938ce7d 100644 --- a/mal/mal.go +++ b/mal/mal.go @@ -12,8 +12,8 @@ import ( "net/http" "net/url" "strings" - "text/template" "sync" + "text/template" ) const ( @@ -89,18 +89,19 @@ func newRequest(url, credentials, method string) *http.Request { //Note: Since anime list endpoint, besides anime list, returns account stats, this method also //updates Client with them -func (c *Client) AnimeList(status MyStatus) []*Anime { +func (c *Client) AnimeList(status MyStatus) ([]*Anime, error) { url := fmt.Sprintf(UserAnimeListEndpoint, c.Username, "all") //Anything other than `all` doesn't really work req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { - fmt.Printf("Request error: %v", err) - return nil + return nil, err } resp, err := http.DefaultClient.Do(req) if err != nil { - log.Printf("List list getting error: %v", err) + return nil, err + } else if resp.StatusCode != 200 { + return nil, fmt.Errorf("server returned: %v", resp.Status) } decoder := xml.NewDecoder(resp.Body) @@ -123,7 +124,7 @@ func (c *Client) AnimeList(status MyStatus) []*Anime { } } - return list + return list, nil } func (c *Client) Update(entry *Anime) bool {