all: Handle all errors
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 6 Apr 2017 15:39:20 +0000 (17:39 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 6 Apr 2017 18:35:26 +0000 (20:35 +0200)
As reported by `errcheck`.

commands/gendoc.go
commands/genman.go
commands/hugo.go
hugolib/hugo_sites.go
hugolib/site.go
hugolib/site_render.go
transform/hugogeneratorinject.go

index 5ffd084e14d4d81d98d1a62d6b692f6371e54de8..8f241446599264fa61f33a2e1def4118578c6240 100644 (file)
@@ -53,7 +53,9 @@ for rendering in Hugo.`,
                }
                if found, _ := helpers.Exists(gendocdir, hugofs.Os); !found {
                        jww.FEEDBACK.Println("Directory", gendocdir, "does not exist, creating...")
-                       hugofs.Os.MkdirAll(gendocdir, 0777)
+                       if err := hugofs.Os.MkdirAll(gendocdir, 0777); err != nil {
+                               return err
+                       }
                }
                now := time.Now().Format(time.RFC3339)
                prepender := func(filename string) string {
index f7a3a424df9ef37bb814557e70b867c7ca1ed0a4..7b26afeed443bc4843acd0f7d7db213148ff3fe2 100644 (file)
@@ -43,7 +43,9 @@ in the "man" directory under the current directory.`,
                }
                if found, _ := helpers.Exists(genmandir, hugofs.Os); !found {
                        jww.FEEDBACK.Println("Directory", genmandir, "does not exist, creating...")
-                       hugofs.Os.MkdirAll(genmandir, 0777)
+                       if err := hugofs.Os.MkdirAll(genmandir, 0777); err != nil {
+                               return err
+                       }
                }
                cmd.Root().DisableAutoGenTag = true
 
index 3d922deff96cea20c42af44ad0383fb884066646..7a7344c4c6f59f6dd4940572ca9030eb2379c9b4 100644 (file)
@@ -211,7 +211,7 @@ func initRootPersistentFlags() {
 
        // Set bash-completion
        validConfigFilenames := []string{"json", "js", "yaml", "yml", "toml", "tml"}
-       HugoCmd.PersistentFlags().SetAnnotation("config", cobra.BashCompFilenameExt, validConfigFilenames)
+       _ = HugoCmd.PersistentFlags().SetAnnotation("config", cobra.BashCompFilenameExt, validConfigFilenames)
 }
 
 // initHugoBuildCommonFlags initialize common flags related to the Hugo build.
@@ -249,10 +249,10 @@ func initHugoBuildCommonFlags(cmd *cobra.Command) {
 
        // Set bash-completion.
        // Each flag must first be defined before using the SetAnnotation() call.
-       cmd.Flags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{})
-       cmd.Flags().SetAnnotation("cacheDir", cobra.BashCompSubdirsInDir, []string{})
-       cmd.Flags().SetAnnotation("destination", cobra.BashCompSubdirsInDir, []string{})
-       cmd.Flags().SetAnnotation("theme", cobra.BashCompSubdirsInDir, []string{"themes"})
+       _ = cmd.Flags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{})
+       _ = cmd.Flags().SetAnnotation("cacheDir", cobra.BashCompSubdirsInDir, []string{})
+       _ = cmd.Flags().SetAnnotation("destination", cobra.BashCompSubdirsInDir, []string{})
+       _ = cmd.Flags().SetAnnotation("theme", cobra.BashCompSubdirsInDir, []string{"themes"})
 }
 
 func initBenchmarkBuildingFlags(cmd *cobra.Command) {
@@ -274,7 +274,7 @@ func init() {
        hugoCmdV = HugoCmd
 
        // Set bash-completion
-       HugoCmd.PersistentFlags().SetAnnotation("logFile", cobra.BashCompFilenameExt, []string{})
+       _ = HugoCmd.PersistentFlags().SetAnnotation("logFile", cobra.BashCompFilenameExt, []string{})
 }
 
 // InitializeConfig initializes a config file with sensible default configuration flags.
@@ -384,7 +384,9 @@ func InitializeConfig(subCmdVs ...*cobra.Command) (*deps.DepsCfg, error) {
                config.Set("cacheDir", helpers.GetTempDir("hugo_cache", fs.Source))
        }
 
-       c.initFs(fs)
+       if err := c.initFs(fs); err != nil {
+               return nil, err
+       }
 
        cfg.Logger.INFO.Println("Using config file:", viper.ConfigFileUsed())
 
@@ -682,17 +684,18 @@ func (c *commandeer) getDirList() []string {
                return nil
        }
 
-       helpers.SymbolicWalk(c.Fs.Source, dataDir, walker)
-       helpers.SymbolicWalk(c.Fs.Source, c.PathSpec().AbsPathify(c.Cfg.GetString("contentDir")), walker)
-       helpers.SymbolicWalk(c.Fs.Source, i18nDir, walker)
-       helpers.SymbolicWalk(c.Fs.Source, c.PathSpec().AbsPathify(c.Cfg.GetString("layoutDir")), walker)
+       // SymbolicWalk will log anny ERRORs
+       _ = helpers.SymbolicWalk(c.Fs.Source, dataDir, walker)
+       _ = helpers.SymbolicWalk(c.Fs.Source, c.PathSpec().AbsPathify(c.Cfg.GetString("contentDir")), walker)
+       _ = helpers.SymbolicWalk(c.Fs.Source, i18nDir, walker)
+       _ = helpers.SymbolicWalk(c.Fs.Source, c.PathSpec().AbsPathify(c.Cfg.GetString("layoutDir")), walker)
 
-       helpers.SymbolicWalk(c.Fs.Source, staticDir, walker)
+       _ = helpers.SymbolicWalk(c.Fs.Source, staticDir, walker)
        if c.PathSpec().ThemeSet() {
-               helpers.SymbolicWalk(c.Fs.Source, filepath.Join(themesDir, "layouts"), walker)
-               helpers.SymbolicWalk(c.Fs.Source, filepath.Join(themesDir, "static"), walker)
-               helpers.SymbolicWalk(c.Fs.Source, filepath.Join(themesDir, "i18n"), walker)
-               helpers.SymbolicWalk(c.Fs.Source, filepath.Join(themesDir, "data"), walker)
+               _ = helpers.SymbolicWalk(c.Fs.Source, filepath.Join(themesDir, "layouts"), walker)
+               _ = helpers.SymbolicWalk(c.Fs.Source, filepath.Join(themesDir, "static"), walker)
+               _ = helpers.SymbolicWalk(c.Fs.Source, filepath.Join(themesDir, "i18n"), walker)
+               _ = helpers.SymbolicWalk(c.Fs.Source, filepath.Join(themesDir, "data"), walker)
 
        }
 
@@ -824,7 +827,9 @@ func (c *commandeer) newWatcher(port int) error {
                                        walkAdder := func(path string, f os.FileInfo, err error) error {
                                                if f.IsDir() {
                                                        c.Logger.FEEDBACK.Println("adding created directory to watchlist", path)
-                                                       watcher.Add(path)
+                                                       if err := watcher.Add(path); err != nil {
+                                                               return err
+                                                       }
                                                }
                                                return nil
                                        }
@@ -833,7 +838,7 @@ func (c *commandeer) newWatcher(port int) error {
                                        // When mkdir -p is used, only the top directory triggers an event (at least on OSX)
                                        if ev.Op&fsnotify.Create == fsnotify.Create {
                                                if s, err := c.Fs.Source.Stat(ev.Name); err == nil && s.Mode().IsDir() {
-                                                       helpers.SymbolicWalk(c.Fs.Source, ev.Name, walkAdder)
+                                                       _ = helpers.SymbolicWalk(c.Fs.Source, ev.Name, walkAdder)
                                                }
                                        }
 
@@ -921,7 +926,7 @@ func (c *commandeer) newWatcher(port int) error {
                                                                        // If file doesn't exist in any static dir, remove it
                                                                        toRemove := filepath.Join(publishDir, relPath)
                                                                        logger.Println("File no longer exists in static dir, removing", toRemove)
-                                                                       c.Fs.Destination.RemoveAll(toRemove)
+                                                                       _ = c.Fs.Destination.RemoveAll(toRemove)
                                                                } else if err == nil {
                                                                        // If file still exists, sync it
                                                                        logger.Println("Syncing", relPath, "to", publishDir)
index 9ef2dc91e35d9e7c73f0682c11a494d64e507c29..b18e66bacc6beff57449469882a34d2cda98a60b 100644 (file)
@@ -551,7 +551,9 @@ func (s *Site) preparePagesForRender(cfg *BuildCfg) {
                                        p.Content = helpers.BytesToHTML(workContentCopy)
 
                                        if summaryContent == nil {
-                                               p.setAutoSummary()
+                                               if err := p.setAutoSummary(); err != nil {
+                                                       s.Log.ERROR.Printf("Failed to set user auto summary for page %q: %s", p.pathOrTitle(), err)
+                                               }
                                        }
 
                                } else {
index 7aec4d4d37a12e50961da54dbbd90d5d0687be7d..ec3e1a4bd3aeeefb9fdcc7839a74de8cf47b5b3d 100644 (file)
@@ -637,7 +637,7 @@ type whatChanged struct {
 func (s *Site) RegisterMediaTypes() {
        for _, mt := range s.mediaTypesConfig {
                // The last one will win if there are any duplicates.
-               mime.AddExtensionType("."+mt.Suffix, mt.Type()+"; charset=utf-8")
+               _ = mime.AddExtensionType("."+mt.Suffix, mt.Type()+"; charset=utf-8")
        }
 }
 
@@ -709,7 +709,9 @@ func (s *Site) reProcess(events []fsnotify.Event) (whatChanged, error) {
        }
 
        if len(dataChanged) > 0 {
-               s.readDataFromSourceFS()
+               if err := s.readDataFromSourceFS(); err != nil {
+                       s.Log.ERROR.Println(err)
+               }
        }
 
        // If a content file changes, we need to reload only it and re-render the entire site.
@@ -1895,7 +1897,10 @@ func (s *Site) renderAndWriteXML(name string, dest string, d interface{}, layout
                path = []byte(s)
        }
        transformer := transform.NewChain(transform.AbsURLInXML)
-       transformer.Apply(outBuffer, renderBuffer, path)
+       if err := transformer.Apply(outBuffer, renderBuffer, path); err != nil {
+               helpers.DistinctErrorLog.Println(err)
+               return nil
+       }
 
        return s.publish(dest, outBuffer)
 
@@ -1943,7 +1948,10 @@ func (s *Site) renderAndWritePage(name string, dest string, p *PageOutput, layou
        }
 
        transformer := transform.NewChain(transformLinks...)
-       transformer.Apply(outBuffer, renderBuffer, path)
+       if err := transformer.Apply(outBuffer, renderBuffer, path); err != nil {
+               helpers.DistinctErrorLog.Println(err)
+               return nil
+       }
 
        return s.publish(dest, outBuffer)
 }
index de8b28723dfdfc3168974707b78dec7c60b660ef..6f0cf3b996da0275a620af951958154254fe5558 100644 (file)
@@ -147,7 +147,9 @@ func (s *Site) renderPaginator(p *PageOutput) error {
 
                // TODO(bep) output do better
                link := newOutputFormat(p.Page, p.outputFormat).Permalink()
-               s.writeDestAlias(target, link, nil)
+               if err := s.writeDestAlias(target, link, nil); err != nil {
+                       return err
+               }
 
                pagers := p.paginator.Pagers()
 
index a73227de7f8a5b0408a5e2e5bd6ded40d82672bc..4c37a058d3086670dd4d77a72c58d5d58d4ed5d0 100644 (file)
@@ -27,7 +27,9 @@ var hugoGeneratorTag = fmt.Sprintf(`<meta name="generator" content="Hugo %s" />`
 // HugoGeneratorInject injects a meta generator tag for Hugo if none present.
 func HugoGeneratorInject(ct contentTransformer) {
        if metaTagsCheck.Match(ct.Content()) {
-               ct.Write(ct.Content())
+               if _, err := ct.Write(ct.Content()); err != nil {
+                       helpers.DistinctWarnLog.Println("Failed to inject Hugo generator tag:", err)
+               }
                return
        }
 
@@ -41,5 +43,8 @@ func HugoGeneratorInject(ct contentTransformer) {
                newcontent = bytes.Replace(ct.Content(), []byte(head), replace, 1)
        }
 
-       ct.Write(newcontent)
+       if _, err := ct.Write(newcontent); err != nil {
+               helpers.DistinctWarnLog.Println("Failed to inject Hugo generator tag:", err)
+       }
+
 }