diff --git a/lyrics.go b/lyrics.go index 6516057..72d4c56 100644 --- a/lyrics.go +++ b/lyrics.go @@ -21,9 +21,11 @@ type song struct { func (s *song) parseLyrics(doc *goquery.Document) { doc.Find("[data-lyrics-container='true']").Each(func(i int, ss *goquery.Selection) { - if h, err := ss.Html(); err == nil { - s.Lyrics += h + h, err := ss.Html() + if err != nil { + logger.Errorln("unable to parse lyrics", err) } + s.Lyrics += h }) } @@ -80,26 +82,38 @@ func lyricsHandler(w http.ResponseWriter, r *http.Request) { url := fmt.Sprintf("https://genius.com/%s-lyrics", id) resp, err := http.Get(url) if err != nil { - write(w, http.StatusInternalServerError, []byte("can't reach genius servers")) + logger.Errorln(err) + w.WriteHeader(http.StatusInternalServerError) + render("error", w, map[string]string{ + "Status": "500", + "Error": "cannot reach genius servers", + }) return } if resp.StatusCode == http.StatusNotFound { - write(w, http.StatusNotFound, []byte("Not found")) + w.WriteHeader(http.StatusNotFound) + render("error", w, map[string]string{ + "Status": "404", + "Error": "page not found", + }) return } doc, err := goquery.NewDocumentFromReader(resp.Body) if err != nil { - write(w, http.StatusInternalServerError, []byte("something went wrong")) + logger.Errorln(err) + w.WriteHeader(http.StatusInternalServerError) + render("error", w, map[string]string{ + "Status": "500", + "Error": "something went wrong", + }) return } var s song s.parse(doc) - w.Header().Set("content-type", "text/html") - render("lyrics", w, s) setCache(id, s) } diff --git a/proxy.go b/proxy.go index 0e38159..c10216d 100644 --- a/proxy.go +++ b/proxy.go @@ -26,7 +26,11 @@ func proxyHandler(w http.ResponseWriter, r *http.Request) { ext := v["ext"] if !isValidExt(ext) { - write(w, http.StatusBadRequest, []byte("not an image :/")) + w.WriteHeader(http.StatusBadRequest) + render("error", w, map[string]string{ + "Status": "400", + "Error": "Something went wrong", + }) return } @@ -35,12 +39,22 @@ func proxyHandler(w http.ResponseWriter, r *http.Request) { res, err := http.Get(url) if err != nil { - write(w, http.StatusInternalServerError, []byte("can't reach genius servers")) + logger.Errorln(err) + w.WriteHeader(http.StatusInternalServerError) + render("error", w, map[string]string{ + "Status": "500", + "Error": "cannot reach genius servers", + }) return } if res.StatusCode != http.StatusOK { - write(w, res.StatusCode, []byte{}) + w.WriteHeader(http.StatusInternalServerError) + render("error", w, map[string]string{ + "Status": "500", + "Error": "something went wrong", + }) + return } diff --git a/static/style.css b/static/style.css index e6ea28f..38f94d1 100644 --- a/static/style.css +++ b/static/style.css @@ -230,13 +230,35 @@ footer a:hover { min-height: 100vh; } +#error { + display: flex; + flex-direction: column; + gap: 1rem; + align-items: center; + justify-content: center; + padding: 2rem; + flex-grow: 1; +} + +#error h1 { + font-size: 5rem; + color: #111; +} + +#error p { + text-transform: uppercase; + font-size: 1.6rem; + color: #222; +} + /* dark mode */ @media (prefers-color-scheme: dark) { body { background-color: #181d31; } - nav, footer { + nav, + footer { background-color: #fec260; } @@ -248,7 +270,8 @@ footer a:hover { color: #ddd; } - #metadata h2, #credits p { + #metadata h2, + #credits p { color: #eee; } @@ -256,15 +279,16 @@ footer a:hover { color: #ddd; } - #about p, #credits summary { - color: #ccc + #about p, + #credits summary { + color: #ccc; } - #home h1 { + #home h1, #error h1 { color: #eee; } - #home p { + #home p, #error p{ color: #ddd; } } diff --git a/utils.go b/utils.go index e9ce850..9ffa990 100644 --- a/utils.go +++ b/utils.go @@ -61,13 +61,16 @@ func getTemplates(templates ...string) []string { } func render(n string, w http.ResponseWriter, data interface{}) { + w.Header().Set("content-type", "text/html") t, err := template.ParseFiles(getTemplates(n, "navbar", "footer")...) if err != nil { + logger.Errorln(err) w.WriteHeader(http.StatusInternalServerError) return } if err = t.Execute(w, data); err != nil { + logger.Errorln(err) w.WriteHeader(http.StatusInternalServerError) return } diff --git a/views/error.tmpl b/views/error.tmpl new file mode 100644 index 0000000..4a59023 --- /dev/null +++ b/views/error.tmpl @@ -0,0 +1,19 @@ + + + + dumb + + + + + +
+ {{template "navbar"}} +
+

{{.Status}}

+

{{.Error}}

+
+ {{template "footer"}} + + +