mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 20:22:01 -06:00
Add custom error for internal request
This commit is contained in:
parent
c5f2b55f34
commit
5709012dfe
4 changed files with 29 additions and 9 deletions
|
|
@ -314,25 +314,25 @@ func request(req *http.Request, client *http.Client) (*http.Response, error) {
|
||||||
var errMap map[string]any
|
var errMap map[string]any
|
||||||
|
|
||||||
if err = json.Unmarshal(buf.Bytes(), &errMap); err != nil {
|
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 val, ok := errMap["error"]; ok {
|
||||||
if errorAsString, ok := val.(string); ok {
|
if errorAsString, ok := val.(string); ok {
|
||||||
if code, ok := errMap["code"].(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 {
|
} else if errorAsBool, ok := val.(bool); ok && errorAsBool {
|
||||||
if msg, ok := errMap["message"].(string); ok {
|
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 {
|
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
|
return resp, err
|
||||||
|
|
|
||||||
|
|
@ -184,10 +184,10 @@ func EpisodeFromID(crunchy *Crunchyroll, id string) (*Episode, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddToWatchlist adds the current episode to the watchlist.
|
// 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,
|
// Will return an RequestError with the response status code of 409 if the series was already on the watchlist before.
|
||||||
// that only series and not individual episode can be added to the watchlist. Even though
|
// There is currently a bug, or as I like to say in context of the crunchyroll api, feature, that only series and not
|
||||||
// I somehow got an episode to my watchlist on the crunchyroll website, it never worked with the
|
// individual episode can be added to the watchlist. Even though I somehow got an episode to my watchlist on the
|
||||||
// api here. So this function actually adds the whole series to the watchlist.
|
// 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 {
|
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)
|
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})
|
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.
|
// 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 {
|
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)
|
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)
|
_, err := e.crunchy.request(endpoint, http.MethodDelete)
|
||||||
|
|
|
||||||
17
error.go
Normal file
17
error.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
2
video.go
2
video.go
|
|
@ -195,6 +195,7 @@ func SeriesFromID(crunchy *Crunchyroll, id string) (*Series, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddToWatchlist adds the current episode to the watchlist.
|
// 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 {
|
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)
|
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})
|
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.
|
// 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 {
|
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)
|
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)
|
_, err := s.crunchy.request(endpoint, http.MethodDelete)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue