Show basic errors in a nice format

This commit is contained in:
Daniele Tonon
2023-11-10 22:54:48 +01:00
parent 0b5766fdfa
commit bf182d1a68
4 changed files with 73 additions and 8 deletions

View File

@@ -5,6 +5,7 @@ package main
import (
_ "embed"
"html/template"
"strings"
"github.com/nbd-wtf/go-nostr/nip11"
sdk "github.com/nbd-wtf/nostr-sdk"
@@ -406,3 +407,27 @@ type SitemapPage struct {
}
func (*SitemapPage) TemplateText() string { return tmplSitemap }
var (
//go:embed templates/error.html
tmplError string
ErrorTemplate = tmpl.MustCompile(&ErrorPage{})
)
type ErrorPage struct {
HeadCommonPartial `tmpl:"head_common"`
TopPartial `tmpl:"top"`
FooterPartial `tmpl:"footer"`
Message string
Errors string
}
func (e *ErrorPage) TemplateText() string {
e.Message = "I cannot give any suggestions to solve the problem, maybe the best solution is to pubblicy blame the devs on Nostr"
if strings.Contains(e.Errors, "invalid checksum") {
e.Message = "It seems you entered an invalid event code, try to check if it is correct; a good idea is compare the first and the last characters"
} else if strings.Contains(e.Errors, "couldn't find this") {
e.Message = "I can't find the event, maybe it is new and has not been already propagated on the relays I'm checking; you can try again in some time"
}
return tmplError
}

View File

@@ -41,7 +41,11 @@ func renderEvent(w http.ResponseWriter, r *http.Request) {
_, redirectHex, err := nip19.Decode(code)
if err != nil {
w.Header().Set("Cache-Control", "max-age=60")
http.Error(w, "error decoding note1 code: "+err.Error(), 404)
errorPage := &ErrorPage{
Errors: err.Error(),
}
errorPage.TemplateText()
ErrorTemplate.Render(w, errorPage)
return
}
redirectNevent, _ := nip19.EncodeEvent(redirectHex.(string), []string{}, "")
@@ -58,7 +62,11 @@ func renderEvent(w http.ResponseWriter, r *http.Request) {
data, err := grabData(r.Context(), code, false)
if err != nil {
w.Header().Set("Cache-Control", "max-age=60")
http.Error(w, "failed to fetch event related data: "+err.Error(), 404)
errorPage := &ErrorPage{
Errors: err.Error(),
}
errorPage.TemplateText()
ErrorTemplate.Render(w, errorPage)
return
}

View File

@@ -19,17 +19,20 @@ func renderProfile(w http.ResponseWriter, r *http.Request, code string) {
}
data, err := grabData(r.Context(), code, isSitemap)
if err != nil {
w.Header().Set("Cache-Control", "max-age=60")
http.Error(w, "error fetching event: "+err.Error(), 404)
return
}
if len(data.renderableLastNotes) != 0 {
} else if len(data.renderableLastNotes) != 0 {
w.Header().Set("Cache-Control", "max-age=3600")
}
if !isSitemap {
if err != nil {
errorPage := &ErrorPage{
Errors: err.Error(),
}
errorPage.TemplateText()
ErrorTemplate.Render(w, errorPage)
} else if !isSitemap {
err = ProfileTemplate.Render(w, &ProfilePage{
HeadCommonPartial: HeadCommonPartial{IsProfile: true, TailwindDebugStuff: tailwindDebugStuff},
DetailsPartial: DetailsPartial{

29
templates/error.html Normal file
View File

@@ -0,0 +1,29 @@
<!doctype html>
<html class="theme--default text-lg font-light print:text-base sm:text-xl">
<meta charset="UTF-8" />
<head>
<title>Error</title>
{{template "head_common" .HeadCommonPartial}}
</head>
<body
class="mb-16 bg-white text-gray-600 dark:bg-neutral-900 dark:text-neutral-50 print:text-black"
>
{{template "top" .}}
<div class="mx-auto mt-12 w-10/12 text-center lg:w-9/12">
<div class="bold text-7xl font-bold sm:text-9xl">Oops!</div>
<div class="mt-4 text-4xl font-medium sm:text-5xl">
Something went wrong
</div>
<div class="mx-auto w-4/5 sm:w-3/5">
<div class="mt-4 text-xl">{{.Message}}</div>
<div class="mt-8 italic text-neutral-400 dark:text-neutral-500">
{{.Errors}}
</div>
</div>
</div>
{{template "footer" .}}
</body>
</html>