hugolib: Use Page Kind in template errors to prevent log spam
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 2 Apr 2017 10:22:54 +0000 (12:22 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 2 Apr 2017 10:22:54 +0000 (12:22 +0200)
Having the content page name in the log key for the distinct error logger isnt't very usable when you have an error in a commonly used partial.

Using the Page Kind reduces the amount of log entries. Here is an example from an error in the partial menu.html, used in all the page templates:

```
Started building sites ...
ERROR 2017/04/02 12:19:43 Error while rendering "page": template: /Users/bep/sites/bepsays.com/layouts/_default/single.html:17:7: executing "/Users/bep/sites/bepsays.com/layouts/_default/single.html" at <partial "menu.html" ...>: error calling partial: template: partials/menu.html:9:11: executing "partials/menu.html" at <.DoesNotExist>: can't evaluate field DoesNotExist in type *hugolib.PageOutput
ERROR 2017/04/02 12:19:43 Error while rendering "section": template: /Users/bep/sites/bepsays.com/layouts/_default/section.html:17:7: executing "/Users/bep/sites/bepsays.com/layouts/_default/section.html" at <partial "menu.html" ...>: error calling partial: template: partials/menu.html:9:11: executing "partials/menu.html" at <.DoesNotExist>: can't evaluate field DoesNotExist in type *hugolib.PageOutput
ERROR 2017/04/02 12:19:43 Error while rendering "taxonomy": template: /Users/bep/sites/bepsays.com/layouts/_default/list.html:17:7: executing "/Users/bep/sites/bepsays.com/layouts/_default/list.html" at <partial "menu.html" ...>: error calling partial: template: partials/menu.html:9:11: executing "partials/menu.html" at <.DoesNotExist>: can't evaluate field DoesNotExist in type *hugolib.PageOutput
ERROR 2017/04/02 12:19:43 Error while rendering "home": template: /Users/bep/sites/bepsays.com/layouts/index.html:17:7: executing "/Users/bep/sites/bepsays.com/layouts/index.html" at <partial "menu.html" ...>: error calling partial: template: partials/menu.html:9:11: executing "partials/menu.html" at <.DoesNotExist>: can't evaluate field DoesNotExist in type *hugolib.PageOutput
ERROR 2017/04/02 12:19:43 Error while rendering "404": template: 404.html:2:3: executing "404.html" at <partial "menu.html" ...>: error calling partial: template: partials/menu.html:9:11: executing "partials/menu.html" at <.DoesNotExist>: can't evaluate field DoesNotExist in type *hugolib.PageOutput
Built site for language nn:
```
Which is pretty good.

hugolib/site.go
hugolib/site_render.go

index 1808a090e749dae9db38018e2da80ae5d66bb5ea..6134858643615b755e68d6ac3a52e3c946139326 100644 (file)
@@ -52,11 +52,6 @@ var testMode bool
 
 var defaultTimer *nitro.B
 
-var (
-       distinctErrorLogger    = helpers.NewDistinctErrorLogger()
-       distinctFeedbackLogger = helpers.NewDistinctFeedbackLogger()
-)
-
 // Site contains all the information relevant for constructing a static
 // site.  The basic flow of information is as follows:
 //
@@ -1834,11 +1829,11 @@ func (s *Site) renderAndWriteXML(name string, dest string, d interface{}, layout
 
 }
 
-func (s *Site) renderAndWritePage(name string, dest string, d interface{}, layouts ...string) error {
+func (s *Site) renderAndWritePage(name string, dest string, p *PageOutput, layouts ...string) error {
        renderBuffer := bp.GetBuffer()
        defer bp.PutBuffer(renderBuffer)
 
-       err := s.renderForLayouts(name, d, renderBuffer, layouts...)
+       err := s.renderForLayouts(p.Kind, p, renderBuffer, layouts...)
 
        if err != nil {
                return err
@@ -1858,7 +1853,7 @@ func (s *Site) renderAndWritePage(name string, dest string, d interface{}, layou
        }
 
        // For performance reasons we only inject the Hugo generator tag on the home page.
-       if n, ok := d.(*PageOutput); ok && n.IsHome() {
+       if p.IsHome() {
                if !s.Cfg.GetBool("disableHugoGeneratorInject") {
                        transformLinks = append(transformLinks, transform.HugoGeneratorInject)
                }
@@ -1887,7 +1882,7 @@ func (s *Site) renderAndWritePage(name string, dest string, d interface{}, layou
                        if !s.Cfg.GetBool("verbose") {
                                debugAddend = "* For more debugging information, run \"hugo -v\""
                        }
-                       distinctFeedbackLogger.Printf(`=============================================================
+                       helpers.DistinctFeedbackLog.Printf(`=============================================================
 Your rendered home page is blank: /index.html is zero-length
  * Did you specify a theme on the command-line or in your
    %q file?  (Current theme: %q)
@@ -1913,7 +1908,7 @@ Your rendered home page is blank: /index.html is zero-length
 func (s *Site) renderForLayouts(name string, d interface{}, w io.Writer, layouts ...string) error {
        templ := s.findFirstTemplate(layouts...)
        if templ == nil {
-               s.Log.WARN.Printf("[%s] Unable to locate layout for %s: %s\n", s.Language.Lang, name, layouts)
+               helpers.DistinctWarnLog.Printf("[%s] Unable to locate layout for %s: %s\n", s.Language.Lang, name, layouts)
 
                return nil
        }
@@ -1921,7 +1916,7 @@ func (s *Site) renderForLayouts(name string, d interface{}, w io.Writer, layouts
        if err := templ.Execute(w, d); err != nil {
 
                // Behavior here should be dependent on if running in server or watch mode.
-               distinctErrorLogger.Printf("Error while rendering %q: %s", name, err)
+               helpers.DistinctErrorLog.Printf("Error while rendering %q: %s", name, err)
                if !s.running() && !testMode {
                        // TODO(bep) check if this can be propagated
                        os.Exit(-1)
index 700912e654f47b1b4af9b68eabf0bb2a05febb6e..b27bf9752cd2d6af45ee7d2a54b29e8485b6b8f1 100644 (file)
@@ -19,6 +19,8 @@ import (
        "sync"
        "time"
 
+       "github.com/spf13/hugo/output"
+
        bp "github.com/spf13/hugo/bufferpool"
 )
 
@@ -248,7 +250,12 @@ func (s *Site) render404() error {
 
        nfLayouts := []string{"404.html"}
 
-       return s.renderAndWritePage("404 page", "404.html", p, s.appendThemeTemplates(nfLayouts)...)
+       pageOutput, err := newPageOutput(p, false, output.HTMLFormat)
+       if err != nil {
+               return err
+       }
+
+       return s.renderAndWritePage("404 page", "404.html", pageOutput, s.appendThemeTemplates(nfLayouts)...)
 
 }