Always print an error when reporting an "internal server error" to the user.
[kraftakt.git] / kraftakt.go
index 8d56299..4c69459 100644 (file)
@@ -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
        }
 }
@@ -171,21 +177,9 @@ func fitbitDisconnectHandler(ctx context.Context, w http.ResponseWriter, r *http
                return err
        }
 
-       var errs appengine.MultiError
-
-       for _, collection := range []string{"activities", "sleep"} {
-               if err := c.Unsubscribe(ctx, collection); err != nil {
-                       errs = append(errs, fmt.Errorf("Unsubscribe(%q) = %v", collection, err))
-                       continue
-               }
-               log.Infof(ctx, "Successfully unsubscribed from %q", collection)
-       }
-
-       if err := c.DeleteToken(ctx); err != nil {
-               errs = append(errs, fmt.Errorf("DeleteToken() = %v", err))
-       }
-       if len(errs) != 0 {
-               return errs
+       if err := c.UnsubscribeAll(ctx); err != nil {
+               log.Errorf(ctx, "UnsubscribeAll() = %v", err)
+               return fmt.Errorf("deleting all subscriptions failed")
        }
 
        redirectURL := r.URL