feat: add custom error page and log silent errors to stdout

This commit is contained in:
rramiachraf 2023-01-22 16:19:35 +01:00
parent c64bbdf591
commit 3df6bdbf22
5 changed files with 90 additions and 16 deletions

View File

@ -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)
}

View File

@ -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
}

View File

@ -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;
}
}

View File

@ -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
}

19
views/error.tmpl Normal file
View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<title>dumb</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="/static/style.css" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<main id="app">
{{template "navbar"}}
<div id="error">
<h1>{{.Status}}</h1>
<p>{{.Error}}</p>
</div>
{{template "footer"}}
</div>
</body>
</html>