Skip to content

Commit

Permalink
hugolib: Improve errors in /data handlling
Browse files Browse the repository at this point in the history
See #5324
  • Loading branch information
bep committed Oct 22, 2018
1 parent d1661b8 commit 9f74dc2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
14 changes: 12 additions & 2 deletions hugofs/rootmapping_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions hugofs/rootmapping_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 13 additions & 3 deletions hugolib/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 9f74dc2

Please sign in to comment.