From 79cc9f9d92197e7286c166978401f99c679932c2 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 31 Jan 2018 08:11:30 +0100 Subject: [PATCH] Always print an error when reporting an "internal server error" to the user. Also only print a reference ID to the user, not the error message. --- kraftakt.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/kraftakt.go b/kraftakt.go index 61a576e..4c69459 100644 --- a/kraftakt.go +++ b/kraftakt.go @@ -43,6 +43,12 @@ func init() { templates = t } +func internalServerError(ctx context.Context, w http.ResponseWriter, err error) { + log.Errorf(ctx, "%v", err) + + http.Error(w, "Internal Server Error\n\nReference: "+appengine.RequestID(ctx), http.StatusInternalServerError) +} + // ContextHandler implements http.Handler type ContextHandler func(context.Context, http.ResponseWriter, *http.Request) error @@ -50,12 +56,12 @@ func (hndl ContextHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ctx := appengine.NewContext(r) if err := app.LoadConfig(ctx); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + internalServerError(ctx, w, fmt.Errorf("LoadConfig() = %v", err)) return } if err := hndl(ctx, w, r); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + internalServerError(ctx, w, err) return } } @@ -66,7 +72,7 @@ func (hndl AuthenticatedHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques ctx := appengine.NewContext(r) if err := app.LoadConfig(ctx); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + internalServerError(ctx, w, fmt.Errorf("LoadConfig() = %v", err)) return } @@ -74,7 +80,7 @@ func (hndl AuthenticatedHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques if gaeUser == nil { url, err := user.LoginURL(ctx, r.URL.String()) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + internalServerError(ctx, w, fmt.Errorf("LoginURL() = %v", err)) return } http.Redirect(w, r, url, http.StatusTemporaryRedirect) @@ -83,12 +89,12 @@ func (hndl AuthenticatedHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques u, err := app.NewUser(ctx, gaeUser.Email) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + internalServerError(ctx, w, fmt.Errorf("NewUser(%q) = %v", gaeUser.Email, err)) return } if err := hndl(ctx, w, r, u); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + internalServerError(ctx, w, err) return } } -- 2.11.0