From 5709012dfe5f625426ba28b99b52720a6ff32b8f Mon Sep 17 00:00:00 2001 From: bytedream Date: Sun, 19 Jun 2022 00:36:27 +0200 Subject: [PATCH] Add custom error for internal request --- crunchyroll.go | 10 +++++----- episode.go | 9 +++++---- error.go | 17 +++++++++++++++++ video.go | 2 ++ 4 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 error.go diff --git a/crunchyroll.go b/crunchyroll.go index 7618fdf..4c29ab4 100644 --- a/crunchyroll.go +++ b/crunchyroll.go @@ -314,25 +314,25 @@ func request(req *http.Request, client *http.Client) (*http.Response, error) { var errMap map[string]any if err = json.Unmarshal(buf.Bytes(), &errMap); err != nil { - return nil, fmt.Errorf("invalid json response: %w", err) + return nil, &RequestError{Response: resp, Message: fmt.Sprintf("invalid json response: %w", err)} } if val, ok := errMap["error"]; ok { if errorAsString, ok := val.(string); ok { if code, ok := errMap["code"].(string); ok { - return nil, fmt.Errorf("error for endpoint %s (%d): %s - %s", req.URL.String(), resp.StatusCode, errorAsString, code) + return nil, &RequestError{Response: resp, Message: fmt.Sprintf("%s - %s", errorAsString, code)} } - return nil, fmt.Errorf("error for endpoint %s (%d): %s", req.URL.String(), resp.StatusCode, errorAsString) + return nil, &RequestError{Response: resp, Message: errorAsString} } else if errorAsBool, ok := val.(bool); ok && errorAsBool { if msg, ok := errMap["message"].(string); ok { - return nil, fmt.Errorf("error for endpoint %s (%d): %s", req.URL.String(), resp.StatusCode, msg) + return nil, &RequestError{Response: resp, Message: msg} } } } } if resp.StatusCode >= 400 { - return nil, fmt.Errorf("error for endpoint %s: %s", req.URL.String(), resp.Status) + return nil, &RequestError{Response: resp, Message: resp.Status} } } return resp, err diff --git a/episode.go b/episode.go index e99bd12..d0e9b14 100644 --- a/episode.go +++ b/episode.go @@ -184,10 +184,10 @@ func EpisodeFromID(crunchy *Crunchyroll, id string) (*Episode, error) { } // AddToWatchlist adds the current episode to the watchlist. -// There is currently a bug, or as I like to say in context of the crunchyroll api, feature, -// that only series and not individual episode can be added to the watchlist. Even though -// I somehow got an episode to my watchlist on the crunchyroll website, it never worked with the -// api here. So this function actually adds the whole series to the watchlist. +// Will return an RequestError with the response status code of 409 if the series was already on the watchlist before. +// There is currently a bug, or as I like to say in context of the crunchyroll api, feature, that only series and not +// individual episode can be added to the watchlist. Even though I somehow got an episode to my watchlist on the +// crunchyroll website, it never worked with the api here. So this function actually adds the whole series to the watchlist. func (e *Episode) AddToWatchlist() error { endpoint := fmt.Sprintf("https://beta.crunchyroll.com/content/v1/watchlist/%s?locale=%s", e.crunchy.Config.AccountID, e.crunchy.Locale) body, _ := json.Marshal(map[string]string{"content_id": e.SeriesID}) @@ -201,6 +201,7 @@ func (e *Episode) AddToWatchlist() error { } // RemoveFromWatchlist removes the current episode from the watchlist. +// Will return an RequestError with the response status code of 404 if the series was not on the watchlist before. func (e *Episode) RemoveFromWatchlist() error { endpoint := fmt.Sprintf("https://beta.crunchyroll.com/content/v1/watchlist/%s/%s?locale=%s", e.crunchy.Config.AccountID, e.SeriesID, e.crunchy.Locale) _, err := e.crunchy.request(endpoint, http.MethodDelete) diff --git a/error.go b/error.go new file mode 100644 index 0000000..b4f7329 --- /dev/null +++ b/error.go @@ -0,0 +1,17 @@ +package crunchyroll + +import ( + "fmt" + "net/http" +) + +type RequestError struct { + error + + Response *http.Response + Message string +} + +func (re *RequestError) String() string { + return fmt.Sprintf("error for endpoint %s (%d): %s", re.Response.Request.URL.String(), re.Response.StatusCode, re.Message) +} diff --git a/video.go b/video.go index 42efacd..3e7d7cf 100644 --- a/video.go +++ b/video.go @@ -195,6 +195,7 @@ func SeriesFromID(crunchy *Crunchyroll, id string) (*Series, error) { } // AddToWatchlist adds the current episode to the watchlist. +// Will return an RequestError with the response status code of 409 if the series was already on the watchlist before. func (s *Series) AddToWatchlist() error { endpoint := fmt.Sprintf("https://beta.crunchyroll.com/content/v1/watchlist/%s?locale=%s", s.crunchy.Config.AccountID, s.crunchy.Locale) body, _ := json.Marshal(map[string]string{"content_id": s.ID}) @@ -208,6 +209,7 @@ func (s *Series) AddToWatchlist() error { } // RemoveFromWatchlist removes the current episode from the watchlist. +// Will return an RequestError with the response status code of 404 if the series was not on the watchlist before. func (s *Series) RemoveFromWatchlist() error { endpoint := fmt.Sprintf("https://beta.crunchyroll.com/content/v1/watchlist/%s/%s?locale=%s", s.crunchy.Config.AccountID, s.ID, s.crunchy.Locale) _, err := s.crunchy.request(endpoint, http.MethodDelete)