Commits

Miki Tebeka committed eddbba5

error

Comments (0)

Files changed (2)

3-user/shortie/html.go

             </span>
         </div>
 
+        {{ if .Error }}
+        <div class="error">
+            ERROR: {{ .Error | html}}
+        </div>
+        {{ end }}
+
         <form method="post">
             <label for="url">Url:</label><input name="url" /> 
             <input type="submit" value="Shorten">

3-user/shortie/shortie.go

 	User       string
 	LoginTitle string
 	LoginURL   string
+	Error      string
 }
 
 // fillUser fills user details in template parameters
 // rootHandler handles the main page.
 func rootHandler(w http.ResponseWriter, r *http.Request) {
 	ctx := appengine.NewContext(r)
+	var err error
+	params := new(homeParams)
 
-	params := new(homeParams)
-	fillUser(r, ctx, params)
-	homeTemplate.Execute(w, params)
+	// Run at end. We check "err" and update params if needed. Then serve homeTemplate.
+	defer func() {
+		if err != nil {
+			params.Error = err.Error()
+			ctx.Errorf("%v", err) // Log error
+		}
+		homeTemplate.Execute(w, params)
+	}()
+
+	err = fillUser(r, ctx, params)
 }