Repos / s4g / 3f0a2e0b5b
commit 3f0a2e0b5bfa15936d21748459570cab782ed0ed
Author: Nhân <hi@imnhan.com>
Date: Sun Jul 16 23:49:54 2023 +0700
support showing arbitrary errors in html
diff --git a/livereload/error.go b/livereload/error.go
index b8ed95d..ac650db 100644
--- a/livereload/error.go
+++ b/livereload/error.go
@@ -18,9 +18,21 @@ type htmlErr interface {
Html() template.HTML
}
-func serveError(w http.ResponseWriter, r *http.Request, err htmlErr) {
+func serveError(w http.ResponseWriter, r *http.Request, err error) {
var buf bytes.Buffer
- errTmpl.Execute(&buf, err)
+ _, ok := err.(htmlErr)
+ if ok {
+ errTmpl.Execute(&buf, err)
+ } else {
+ // Shim for errors that don't support HTML output
+ errTmpl.Execute(&buf, struct {
+ Error string
+ Html template.HTML
+ }{
+ Error: err.Error(),
+ Html: template.HTML(err.Error()),
+ })
+ }
body := withLiveReload(buf.Bytes())
w.Write(body)
}
diff --git a/livereload/livereload.go b/livereload/livereload.go
index b51105a..4eb7615 100644
--- a/livereload/livereload.go
+++ b/livereload/livereload.go
@@ -86,7 +86,7 @@ func Middleware(mux *http.ServeMux, root string, fsys writablefs.FS, f http.Hand
err = state.err
state.errMut.RUnlock()
if err != nil {
- serveError(w, r, err.(htmlErr))
+ serveError(w, r, err)
return
}