Better error handling when rendering error found when in watch mode
authorspf13 <steve.francia@gmail.com>
Fri, 25 Oct 2013 22:03:14 +0000 (18:03 -0400)
committerspf13 <steve.francia@gmail.com>
Fri, 25 Oct 2013 22:03:14 +0000 (18:03 -0400)
In watch mode it should continue to watch for changes, in any other mode it should exit with a -1 error code so can check for success when scripting

commands/hugo.go
commands/server.go
hugolib/site.go

index bdd1171c522bae4505b40759577dd3ede8a912ea..935328b73dafa42d6d2beb061275c8e31449901b 100644 (file)
@@ -84,9 +84,13 @@ func InitializeConfig() {
        }
 }
 
-func build() {
+func build(watches ...bool) {
        utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", Config.GetAbsPath(Config.PublishDir)))
-       utils.StopOnErr(buildSite())
+       watch := false
+       if len(watches) > 0 && watches[0] {
+               watch = true
+       }
+       utils.StopOnErr(buildSite(BuildWatch || watch))
 
        if BuildWatch {
                fmt.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir))
@@ -121,9 +125,12 @@ func getDirList() []string {
        return a
 }
 
-func buildSite() (err error) {
+func buildSite(watching ...bool) (err error) {
        startTime := time.Now()
        site := &hugolib.Site{Config: *Config}
+       if len(watching) > 0 && watching[0] {
+               site.RunMode.Watching = true
+       }
        err = site.Build()
        if err != nil {
                return
@@ -185,7 +192,7 @@ func watchChange(ev *fsnotify.FileEvent) {
                        // Ignoring temp files created by editors (vim)
                        if !strings.HasSuffix(ev.Name, "~") && !strings.HasSuffix(ev.Name, ".swp") {
                                fmt.Println("Change detected, rebuilding site\n")
-                               utils.StopOnErr(buildSite())
+                               utils.StopOnErr(buildSite(true))
                        }
                }
        }
index ce1c5f9e03db2ad5527e477321d9dc109c6c31ca..a933117595f9cd664f0a1681511ab096a39a286f 100644 (file)
@@ -45,7 +45,7 @@ func server(cmd *cobra.Command, args []string) {
                Config.BaseUrl = "http://localhost:" + strconv.Itoa(serverPort)
        }
 
-       build()
+       build(serverWatch)
 
        // Watch runs its own server as part of the routine
        if serverWatch {
index c9ab1a26213ae100af0e7aff3d1e7ff9b3925910..c97419416e33b5133723759e904c9ad310148af5 100644 (file)
@@ -68,6 +68,7 @@ type Site struct {
        Target      target.Output
        Alias       target.AliasPublisher
        Completed   chan bool
+       RunMode     runmode
 }
 
 type SiteInfo struct {
@@ -79,6 +80,14 @@ type SiteInfo struct {
        Config     *Config
 }
 
+type runmode struct {
+       Watching bool
+}
+
+func (s *Site) Running() bool {
+       return s.RunMode.Watching
+}
+
 func init() {
        DefaultTimer = nitro.Initalize()
 }
@@ -576,7 +585,11 @@ func (s *Site) render(d interface{}, out string, layouts ...string) (err error)
        go func() {
                err = s.renderThing(d, layout, renderWriter)
                if err != nil {
-                       panic(err)
+                       // Behavior here should be dependent on if running in server or watch mode.
+                       fmt.Println(fmt.Errorf("Rendering error: %v", err))
+                       if !s.Running() {
+                               os.Exit(-1)
+                       }
                }
        }()