hugolib: Improve errors in /data handlling

See #5324
This commit is contained in:
Bjørn Erik Pedersen 2018-10-22 16:47:23 +02:00
parent d1661b823a
commit 9f74dc2a52
No known key found for this signature in database
GPG Key ID: 330E6E2BD4859D8F
3 changed files with 26 additions and 5 deletions

View 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

View 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)

View 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 {