diff --git a/pages.go b/pages.go index 9778d65..2f685b6 100644 --- a/pages.go +++ b/pages.go @@ -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 +} diff --git a/render_event.go b/render_event.go index 65a8b93..fa13b5f 100644 --- a/render_event.go +++ b/render_event.go @@ -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 } diff --git a/render_profile.go b/render_profile.go index 8595ae4..afda701 100644 --- a/render_profile.go +++ b/render_profile.go @@ -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{ diff --git a/templates/error.html b/templates/error.html new file mode 100644 index 0000000..98c15ce --- /dev/null +++ b/templates/error.html @@ -0,0 +1,29 @@ + + + + + Error + {{template "head_common" .HeadCommonPartial}} + + + + {{template "top" .}} + +
+
Oops!
+
+ Something went wrong +
+
+
{{.Message}}
+
+ {{.Errors}} +
+
+
+ + {{template "footer" .}} + +