hugolib: Improve errors in /data handlling
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 22 Oct 2018 14:47:23 +0000 (16:47 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 22 Oct 2018 18:46:14 +0000 (20:46 +0200)
See #5324

hugofs/rootmapping_fs.go
hugofs/rootmapping_fs_test.go
hugolib/site.go

index 176edaa070aacf8bd2574b6f83725a91989e0d71..2b8b8d2c012d6225a9450419af427a3d5c54adbc 100644 (file)
@@ -101,7 +101,14 @@ func (fs *RootMappingFs) Stat(name string) (os.FileInfo, error) {
                return newRootMappingDirFileInfo(name), nil
        }
        realName := fs.realName(name)
-       return fs.Fs.Stat(realName)
+
+       fi, err := fs.Fs.Stat(realName)
+       if rfi, ok := fi.(RealFilenameInfo); ok {
+               return rfi, err
+       }
+
+       return &realFilenameInfo{FileInfo: fi, realFilename: realName}, err
+
 }
 
 func (fs *RootMappingFs) isRoot(name string) bool {
@@ -126,12 +133,15 @@ func (fs *RootMappingFs) Open(name string) (afero.File, error) {
 // It attempts to use Lstat if supported or defers to the os.  In addition to
 // the FileInfo, a boolean is returned telling whether Lstat was called.
 func (fs *RootMappingFs) LstatIfPossible(name string) (os.FileInfo, bool, error) {
+
        if fs.isRoot(name) {
                return newRootMappingDirFileInfo(name), false, nil
        }
        name = fs.realName(name)
+
        if ls, ok := fs.Fs.(afero.Lstater); ok {
-               return ls.LstatIfPossible(name)
+               fi, b, err := ls.LstatIfPossible(name)
+               return &realFilenameInfo{FileInfo: fi, realFilename: name}, b, err
        }
        fi, err := fs.Stat(name)
        return fi, false, err
index a84f411516d8d28a46bd94769de50d04b27fb9b6..d76e00d2e4dea8e48826d2126b16d79844ba3f4a 100644 (file)
@@ -50,6 +50,7 @@ func TestRootMappingFsDirnames(t *testing.T) {
        fif, err := rfs.Stat(filepath.Join("cf2", testfile))
        assert.NoError(err)
        assert.Equal("myfile.txt", fif.Name())
+       assert.Equal("f2t/myfile.txt", fif.(RealFilenameInfo).RealFilename())
 
        root, err := rfs.Open(filepathSeparator)
        assert.NoError(err)
index 78a0070ee912e40c544b39e792cb4a1cb144b87d..e3f6d8f8cbc1f02cd99b34c2f8d6a900d18b6779 100644 (file)
@@ -28,6 +28,10 @@ import (
        "strings"
        "time"
 
+       "github.com/gohugoio/hugo/hugofs"
+
+       "github.com/gohugoio/hugo/common/herrors"
+
        _errors "github.com/pkg/errors"
 
        "github.com/gohugoio/hugo/common/maps"
@@ -776,7 +780,7 @@ func (s *Site) processPartial(events []fsnotify.Event) (whatChanged, error) {
 
        if len(dataChanged) > 0 {
                if err := s.readDataFromSourceFS(); err != nil {
-                       s.Log.ERROR.Println(err)
+                       return whatChanged{}, err
                }
        }
 
@@ -884,8 +888,14 @@ func (s *Site) handleDataFile(r source.ReadableFile) error {
 
        data, err := s.readData(r)
        if err != nil {
-               s.Log.ERROR.Printf("Failed to read data from %s: %s", filepath.Join(r.Path(), r.LogicalName()), err)
-               return nil
+               realFilename := r.FileInfo().(hugofs.RealFilenameInfo).RealFilename()
+               err, _ = herrors.WithFileContextForFile(
+                       _errors.Wrapf(err, "failed to read data file"),
+                       realFilename,
+                       realFilename,
+                       s.SourceSpec.Fs.Source,
+                       herrors.SimpleLineMatcher)
+               return err
        }
 
        if data == nil {