From dccfd17bccd812d5da0bcbae266d8f17a629535f Mon Sep 17 00:00:00 2001 From: rramiachraf <51409801+rramiachraf@users.noreply.github.com> Date: Mon, 4 Mar 2024 18:46:23 +0100 Subject: [PATCH] fix: check ignored errors --- data/album.go | 17 ++++++++++------- handlers/album.go | 8 ++++++-- handlers/annotations.go | 8 +++++--- handlers/lyrics.go | 4 +++- handlers/proxy.go | 4 +++- handlers/search.go | 6 +++++- 6 files changed, 32 insertions(+), 15 deletions(-) diff --git a/data/album.go b/data/album.go index 775f7bb..65dc60f 100644 --- a/data/album.go +++ b/data/album.go @@ -45,30 +45,33 @@ type Artist struct { Name string `json:"name"` } -func (a *Album) parseAlbumData(doc *goquery.Document) { +func (a *Album) parseAlbumData(doc *goquery.Document) error { pageMetadata, exists := doc.Find("meta[itemprop='page_data']").Attr("content") if !exists { - return + return nil } var albumMetadataFromPage albumMetadata - json.Unmarshal([]byte(pageMetadata), &albumMetadataFromPage) + if err := json.Unmarshal([]byte(pageMetadata), &albumMetadataFromPage); err != nil { + return err + } albumData := albumMetadataFromPage.Album a.Artist = albumData.Artist.Name a.Name = albumData.Name a.Image = albumData.Image a.About[0] = albumData.Description - //a.About[1] = truncateText(albumData.Description) + a.About[1] = truncateText(albumData.Description) a.About[1] = "" for _, track := range albumMetadataFromPage.AlbumAppearances { url := strings.Replace(track.Song.Url, "https://genius.com", "", -1) a.Tracks = append(a.Tracks, Track{Title: track.Song.Title, Url: url}) } + + return nil } -func (a *Album) Parse(doc *goquery.Document) { - a.parseAlbumData(doc) +func (a *Album) Parse(doc *goquery.Document) error { + return a.parseAlbumData(doc) } - diff --git a/handlers/album.go b/handlers/album.go index bbdca54..e9aa8bc 100644 --- a/handlers/album.go +++ b/handlers/album.go @@ -58,10 +58,14 @@ func Album(l *logrus.Logger) http.HandlerFunc { } var a data.Album - a.Parse(doc) + if err = a.Parse(doc); err != nil { + l.Error(err) + } views.AlbumPage(a).Render(context.Background(), w) - setCache(id, a) + if err = setCache(id, a); err != nil { + l.Errorln(err) + } } } diff --git a/handlers/annotations.go b/handlers/annotations.go index a12df8f..2696c98 100644 --- a/handlers/annotations.go +++ b/handlers/annotations.go @@ -85,9 +85,11 @@ func Annotations(l *logrus.Logger) http.HandlerFunc { return } - setCache(id, body) - _, err = w.Write(response) - if err != nil { + if err = setCache(id, body); err != nil { + l.Errorln(err) + } + + if _, err = w.Write(response); err != nil { l.Errorln("Error sending response: ", err) } } diff --git a/handlers/lyrics.go b/handlers/lyrics.go index 3e2ee4b..d0e1c34 100644 --- a/handlers/lyrics.go +++ b/handlers/lyrics.go @@ -57,6 +57,8 @@ func Lyrics(l *logrus.Logger) http.HandlerFunc { s.Parse(doc) views.LyricsPage(s).Render(context.Background(), w) - setCache(id, s) + if err = setCache(id, s); err != nil { + l.Errorln(err) + } } } diff --git a/handlers/proxy.go b/handlers/proxy.go index ea3003d..3f85baa 100644 --- a/handlers/proxy.go +++ b/handlers/proxy.go @@ -53,6 +53,8 @@ func ImageProxy(l *logrus.Logger) http.HandlerFunc { } w.Header().Add("Content-type", fmt.Sprintf("image/%s", ext)) - io.Copy(w, res.Body) + if _, err = io.Copy(w, res.Body); err != nil { + l.Errorln("unable to write image", err) + } } } diff --git a/handlers/search.go b/handlers/search.go index 26265d9..b2a83cd 100644 --- a/handlers/search.go +++ b/handlers/search.go @@ -30,7 +30,11 @@ func Search(l *logrus.Logger) http.HandlerFunc { var sRes data.SearchResponse d := json.NewDecoder(res.Body) - d.Decode(&sRes) + if err = d.Decode(&sRes); err != nil { + l.Errorln(err) + w.WriteHeader(http.StatusInternalServerError) + views.ErrorPage(500, "something went wrong").Render(context.Background(), w) + } results := data.SearchResults{Query: query, Sections: sRes.Response.Sections}