From e193183f86f2553e71e28b289c9be4ddda69a25b Mon Sep 17 00:00:00 2001 From: Daan Sprenkels Date: Tue, 15 Oct 2019 23:19:45 +0200 Subject: [PATCH] Allow internal server error using panic() --- assets/templates/html/index.html.tmpl | 6 +++--- assets/templates/txt/index.txt.tmpl | 6 +++--- go.mod | 1 + go.sum | 2 ++ handlers/handlers.go | 19 +++++-------------- handlers/router.go | 21 +++++++++++++++++++++ handlers/views.go | 10 ++++++++-- 7 files changed, 43 insertions(+), 22 deletions(-) diff --git a/assets/templates/html/index.html.tmpl b/assets/templates/html/index.html.tmpl index b12774b..7eae1d1 100644 --- a/assets/templates/html/index.html.tmpl +++ b/assets/templates/html/index.html.tmpl @@ -9,12 +9,12 @@ the command line. ## USAGE # Upload a file - curl -F'file=@yourfile.png' {{Request.Host}} + curl -F'file=@yourfile.png' {{.Request.Host}} # Shorten a URL - curl -F'shorten=http://example.com/some/long/url' {{Request.Host}} + curl -F'shorten=http://example.com/some/long/url' {{.Request.Host}} # Shorten a URL with a token to delete it later - curl -F'shorten=http://example.com/some/long/url' -F'deleteToken=' {{Request.Host}} + curl -F'shorten=http://example.com/some/long/url' -F'deleteToken=' {{.Request.Host}} {{end}} \ No newline at end of file diff --git a/assets/templates/txt/index.txt.tmpl b/assets/templates/txt/index.txt.tmpl index 7023676..ac238f4 100644 --- a/assets/templates/txt/index.txt.tmpl +++ b/assets/templates/txt/index.txt.tmpl @@ -7,10 +7,10 @@ the command line. ## USAGE # Upload a file - curl -F'file=@yourfile.png' {{Request.Host}} + curl -F'file=@yourfile.png' {{.Request.Host}} # Shorten a URL - curl -F'shorten=http://example.com/some/long/url' {{Request.Host}} + curl -F'shorten=http://example.com/some/long/url' {{.Request.Host}} # Shorten a URL with a token to delete it later - curl -F'shorten=http://example.com/some/long/url' -F'deleteToken=' {{Request.Host}} \ No newline at end of file + curl -F'shorten=http://example.com/some/long/url' -F'deleteToken=' {{.Request.Host}} \ No newline at end of file diff --git a/go.mod b/go.mod index 08f20a7..51b514b 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.12 require ( github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect + github.com/gorilla/handlers v1.4.2 github.com/gorilla/mux v1.7.3 github.com/pkg/errors v0.8.1 github.com/prometheus/client_golang v1.1.0 diff --git a/go.sum b/go.sum index 0ad81ed..7009e20 100644 --- a/go.sum +++ b/go.sum @@ -18,6 +18,8 @@ github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/gorilla/handlers v1.4.2 h1:0QniY0USkHQ1RGCLfKxeNHK9bkDHGRYGNDFBCS+YARg= +github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ= github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw= github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= diff --git a/handlers/handlers.go b/handlers/handlers.go index 8d982a3..3c17e86 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -84,8 +84,7 @@ func viewPasteHandlerInner(w http.ResponseWriter, r *http.Request, flags viewPas p, err = getPaste(tx, key) return err }); err != nil { - log.Printf("error: %v\n", err) - renderInternalServerError(w, r, err) + panic(err) return } if p == nil { @@ -124,9 +123,7 @@ func viewPasteHandlerInner(w http.ResponseWriter, r *http.Request, flags viewPas rawurl := string(p.Content) urlParse, err := url.Parse(rawurl) if err != nil { - log.Printf("error: invalid URL ('%v') in database for key '%v': %v\n", rawurl, p.Key, err) - renderInternalServerError(w, r, "invalid url in database") - return + panic(errors.Wrapf(err, "invalid URL ('%v') in database for key '%v'", rawurl, p.Key)) } http.Redirect(w, r, urlParse.String(), http.StatusSeeOther) } @@ -134,17 +131,13 @@ func viewPasteHandlerInner(w http.ResponseWriter, r *http.Request, flags viewPas case stateDeleted: renderError(w, r, http.StatusGone, "paste has been deleted") default: - log.Printf("error: invalid paste.State (%v) for key '%v'\n", p.State, p.Key) - msg := fmt.Sprintf("internal server error: invalid paste.State (%v\n)", p.State) - renderInternalServerError(w, r, msg) + panic(errors.Errorf("invalid paste.State (%v) for key '%v'", p.State, p.Key)) } } func newPasteHandler(w http.ResponseWriter, r *http.Request) { if err := r.ParseMultipartForm(50 * 1000 * 1000); err != nil { - log.Printf("error: %v\n", err) - renderInternalServerError(w, r, err) - return + panic(err) } // Determine what kind of post this is, currently only `shorten=...` @@ -189,9 +182,7 @@ func newRedirectPasteHandler(w http.ResponseWriter, r *http.Request) { paste, err = shortenURL(tx, userURL) return err }); err != nil { - log.Printf("error: %v\n", err) - renderInternalServerError(w, r, err) - return + panic(err) } data := map[string]interface{}{"Paste": paste} render(w, r, "newRedirectPasteSuccess", data) diff --git a/handlers/router.go b/handlers/router.go index f811bef..1749c26 100644 --- a/handlers/router.go +++ b/handlers/router.go @@ -1,6 +1,7 @@ package handlers import ( + "fmt" "log" "net/http" "time" @@ -8,9 +9,29 @@ import ( "github.com/gorilla/mux" ) +func recoveryMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + defer func() { + defer func() { + if err := recover(); err != nil { + log.Printf("error: panic while recovering from another panic: %v\n", err) + fmt.Fprintf(w, "internal server error: %v\n", err) + } + }() + + if err := recover(); err != nil { + log.Printf("error: %v\n", err) + renderInternalServerError(w, r, err) + } + }() + next.ServeHTTP(w, r) + }) +} + func StartMainServer() { // Initialize Gorilla router router := mux.NewRouter() + router.Use(recoveryMiddleware) router.HandleFunc("/", indexGetHandler).Methods("GET") router.HandleFunc("/", newPasteHandler).Methods("POST") router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}", viewPasteHandler).Methods("GET") diff --git a/handlers/views.go b/handlers/views.go index 44df17b..74b9acb 100644 --- a/handlers/views.go +++ b/handlers/views.go @@ -40,14 +40,20 @@ func init() { for _, tmplPath := range AssetNames() { if mustMatch("templates/txt/*.txt.tmpl", tmplPath) { base := text.Must(textBaseTemplate.Clone()) - tmpl := text.Must(base.Parse(string(MustAsset(tmplPath)))) + tmpl, err := base.Parse(string(MustAsset(tmplPath))) + if err != nil { + panic(errors.Wrapf(err, "parsing %v", tmplPath)) + } tmplName := strings.TrimSuffix(filepath.Base(tmplPath), ".txt.tmpl") textTemplates[tmplName] = tmpl continue } if mustMatch("templates/html/*.html.tmpl", tmplPath) { base := html.Must(htmlBaseTemplate.Clone()) - tmpl := html.Must(base.Parse(string(MustAsset(tmplPath)))) + tmpl, err := base.Parse(string(MustAsset(tmplPath))) + if err != nil { + panic(errors.Wrapf(err, "parsing %v", tmplPath)) + } tmplName := strings.TrimSuffix(filepath.Base(tmplPath), ".html.tmpl") htmlTemplates[tmplName] = tmpl continue