From 9a655080487303f4ad7be6a15e82988c67ee7659 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 6 Jul 2016 12:00:28 +0100 Subject: [PATCH] New errors page --- errors/errors.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ filemanager.go | 10 +++++++- 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 errors/errors.go diff --git a/errors/errors.go b/errors/errors.go new file mode 100644 index 00000000..d404e54e --- /dev/null +++ b/errors/errors.go @@ -0,0 +1,60 @@ +package errors + +import ( + "net/http" + "strconv" + "strings" +) + +const template = ` + + + TITLE + + + + +
+

TITLE

+ +

Try reloading the page or hitting the back button. If this error persists, it seems that you may have found a bug! Please create an issue at hacdias/caddy-filemanager repository on GitHub with the code below.

+ + CODE +
+` + +// PrintHTML prints the error page +func PrintHTML(w http.ResponseWriter, code int, err error) (int, error) { + tpl := template + tpl = strings.Replace(tpl, "TITLE", strconv.Itoa(code)+" "+http.StatusText(code), -1) + tpl = strings.Replace(tpl, "CODE", err.Error(), -1) + + _, err = w.Write([]byte(tpl)) + + if err != nil { + return http.StatusInternalServerError, err + } + return http.StatusOK, nil +} diff --git a/filemanager.go b/filemanager.go index ecc80708..654b242e 100644 --- a/filemanager.go +++ b/filemanager.go @@ -21,6 +21,7 @@ import ( "github.com/hacdias/caddy-filemanager/assets" "github.com/hacdias/caddy-filemanager/config" "github.com/hacdias/caddy-filemanager/directory" + "github.com/hacdias/caddy-filemanager/errors" "github.com/hacdias/caddy-filemanager/page" "github.com/mholt/caddy/caddyhttp/httpserver" ) @@ -50,6 +51,9 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err if r.Method != http.MethodPost && !serveAssets { fi, code, err = directory.GetInfo(r.URL, c) if err != nil { + if r.Method == http.MethodGet { + return errors.PrintHTML(w, code, err) + } return code, err } @@ -89,7 +93,11 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err } } - return fi.ServeAsHTML(w, r, c) + code, err := fi.ServeAsHTML(w, r, c) + if err != nil { + return errors.PrintHTML(w, code, err) + } + return code, err case http.MethodPut: if fi.IsDir { return http.StatusNotAcceptable, nil