all repos — fixyoutube-go @ c28738590ff3158a01e25f42edd0a61b2bc20d91

A better way to embed YouTube videos everywhere (inspired by FixTweet).

better error handling
Marco Andronaco andronacomarco@gmail.com
Fri, 12 Jan 2024 10:10:06 +0100
commit

c28738590ff3158a01e25f42edd0a61b2bc20d91

parent

913b5e94318ac196a2c6111e459ffe2ba801cd36

1 files changed, 17 insertions(+), 3 deletions(-)

jump to
M invidious/invidious.goinvidious/invidious.go

@@ -72,6 +72,14 @@ }

return res } +type HTTPError struct { + StatusCode int +} + +func (e HTTPError) Error() string { + return fmt.Sprintf("HTTP error: %d", e.StatusCode) +} + func (c *Client) fetchVideo(videoId string) (*Video, error) { if c.Instance == "" { err := c.NewInstance()

@@ -92,7 +100,7 @@ return nil, err

} if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf(string(body)) + return nil, HTTPError{resp.StatusCode} } res := &Video{}

@@ -127,9 +135,15 @@

video, err = c.fetchVideo(videoId) if err != nil { - if err.Error() == "{}" { - return nil, err + if httpErr, ok := err.(HTTPError); ok { + // handle HTTPError + if httpErr.StatusCode == http.StatusNotFound { + logger.Debug("Video does not exist.") + return nil, err + } + logger.Debug("Invidious HTTP error: ", httpErr.StatusCode) } + // handle generic error logger.Error(err) err = c.NewInstance() if err != nil {