feat: add custom error page and log silent errors to stdout
This commit is contained in:
parent
c64bbdf591
commit
3df6bdbf22
28
lyrics.go
28
lyrics.go
@ -21,9 +21,11 @@ type song struct {
|
|||||||
|
|
||||||
func (s *song) parseLyrics(doc *goquery.Document) {
|
func (s *song) parseLyrics(doc *goquery.Document) {
|
||||||
doc.Find("[data-lyrics-container='true']").Each(func(i int, ss *goquery.Selection) {
|
doc.Find("[data-lyrics-container='true']").Each(func(i int, ss *goquery.Selection) {
|
||||||
if h, err := ss.Html(); err == nil {
|
h, err := ss.Html()
|
||||||
s.Lyrics += h
|
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)
|
url := fmt.Sprintf("https://genius.com/%s-lyrics", id)
|
||||||
resp, err := http.Get(url)
|
resp, err := http.Get(url)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode == http.StatusNotFound {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var s song
|
var s song
|
||||||
s.parse(doc)
|
s.parse(doc)
|
||||||
|
|
||||||
w.Header().Set("content-type", "text/html")
|
|
||||||
|
|
||||||
render("lyrics", w, s)
|
render("lyrics", w, s)
|
||||||
setCache(id, s)
|
setCache(id, s)
|
||||||
}
|
}
|
||||||
|
20
proxy.go
20
proxy.go
@ -26,7 +26,11 @@ func proxyHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
ext := v["ext"]
|
ext := v["ext"]
|
||||||
|
|
||||||
if !isValidExt(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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,12 +39,22 @@ func proxyHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
res, err := http.Get(url)
|
res, err := http.Get(url)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if res.StatusCode != http.StatusOK {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -230,13 +230,35 @@ footer a:hover {
|
|||||||
min-height: 100vh;
|
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 */
|
/* dark mode */
|
||||||
@media (prefers-color-scheme: dark) {
|
@media (prefers-color-scheme: dark) {
|
||||||
body {
|
body {
|
||||||
background-color: #181d31;
|
background-color: #181d31;
|
||||||
}
|
}
|
||||||
|
|
||||||
nav, footer {
|
nav,
|
||||||
|
footer {
|
||||||
background-color: #fec260;
|
background-color: #fec260;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -248,7 +270,8 @@ footer a:hover {
|
|||||||
color: #ddd;
|
color: #ddd;
|
||||||
}
|
}
|
||||||
|
|
||||||
#metadata h2, #credits p {
|
#metadata h2,
|
||||||
|
#credits p {
|
||||||
color: #eee;
|
color: #eee;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -256,15 +279,16 @@ footer a:hover {
|
|||||||
color: #ddd;
|
color: #ddd;
|
||||||
}
|
}
|
||||||
|
|
||||||
#about p, #credits summary {
|
#about p,
|
||||||
color: #ccc
|
#credits summary {
|
||||||
|
color: #ccc;
|
||||||
}
|
}
|
||||||
|
|
||||||
#home h1 {
|
#home h1, #error h1 {
|
||||||
color: #eee;
|
color: #eee;
|
||||||
}
|
}
|
||||||
|
|
||||||
#home p {
|
#home p, #error p{
|
||||||
color: #ddd;
|
color: #ddd;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
3
utils.go
3
utils.go
@ -61,13 +61,16 @@ func getTemplates(templates ...string) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func render(n string, w http.ResponseWriter, data interface{}) {
|
func render(n string, w http.ResponseWriter, data interface{}) {
|
||||||
|
w.Header().Set("content-type", "text/html")
|
||||||
t, err := template.ParseFiles(getTemplates(n, "navbar", "footer")...)
|
t, err := template.ParseFiles(getTemplates(n, "navbar", "footer")...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Errorln(err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = t.Execute(w, data); err != nil {
|
if err = t.Execute(w, data); err != nil {
|
||||||
|
logger.Errorln(err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
19
views/error.tmpl
Normal file
19
views/error.tmpl
Normal 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>
|
Loading…
x
Reference in New Issue
Block a user