shithub: hugo

Download patch

ref: 61bb3ccab3a552c4b80ffb5370714598660b7b37
parent: 794ea21e9449b876c5514f1ce8fe61449bbe4980
author: Bjørn Erik Pedersen <[email protected]>
date: Fri Jul 14 06:26:51 EDT 2017

hugolib: Improve panic handling in layout rendering

--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -1966,16 +1966,29 @@
 	return s.publish(dest, outBuffer)
 }
 
-func (s *Site) renderForLayouts(name string, d interface{}, w io.Writer, layouts ...string) error {
+func (s *Site) renderForLayouts(name string, d interface{}, w io.Writer, layouts ...string) (err error) {
+	var templ tpl.Template
+
 	defer func() {
-		recover()
+		if r := recover(); r != nil {
+			templName := ""
+			if templ != nil {
+				templName = templ.Name()
+			}
+			helpers.DistinctErrorLog.Printf("Failed to render %q: %s", templName, r)
+			// TOD(bep) we really need to fix this. Also see below.
+			if !s.running() && !testMode {
+				os.Exit(-1)
+			}
+		}
 	}()
-	templ := s.findFirstTemplate(layouts...)
+
+	templ = s.findFirstTemplate(layouts...)
 	if templ == nil {
 		return fmt.Errorf("[%s] Unable to locate layout for %q: %s\n", s.Language.Lang, name, layouts)
 	}
 
-	if err := templ.Execute(w, d); err != nil {
+	if err = templ.Execute(w, d); err != nil {
 		// Behavior here should be dependent on if running in server or watch mode.
 		helpers.DistinctErrorLog.Printf("Error while rendering %q: %s", name, err)
 		if !s.running() && !testMode {
@@ -1982,11 +1995,11 @@
 			// TODO(bep) check if this can be propagated
 			os.Exit(-1)
 		} else if testMode {
-			return err
+			return
 		}
 	}
 
-	return nil
+	return
 }
 
 func (s *Site) findFirstTemplate(layouts ...string) tpl.Template {