1
0
mirror of https://github.com/danog/gojekyll.git synced 2025-01-22 12:31:18 +01:00

New WrapError recognizes any type with Path()

This commit is contained in:
Oliver Steele 2017-07-12 07:27:46 -04:00
parent 09aac70d66
commit 695867b7f5
5 changed files with 45 additions and 20 deletions

View File

@ -82,12 +82,12 @@ func (p *Pipeline) Render(w io.Writer, b []byte, filename string, lineNo int, e
func (p *Pipeline) renderTemplate(src []byte, b map[string]interface{}, filename string, lineNo int) ([]byte, error) {
tpl, err := p.liquidEngine.ParseTemplate(src)
if err != nil {
return nil, utils.WrapPathError(err, "Liquid Error", filename)
return nil, utils.WrapPathError(err, filename)
}
tpl.SetSourceLocation(filename, lineNo)
out, err := tpl.Render(b)
if err != nil {
return nil, utils.WrapPathError(err, "Liquid Error", filename)
return nil, utils.WrapPathError(err, filename)
}
return out, err
}
@ -106,7 +106,7 @@ func (p *Pipeline) ApplyLayout(name string, data []byte, e map[string]interface{
})
data, err = tpl.Render(b)
if err != nil {
return nil, utils.WrapPathError(err, "render template", name)
return nil, utils.WrapPathError(err, name)
}
name = templates.VariableMap(lfm).String("layout", "")
}

View File

@ -35,7 +35,7 @@ func (s *Site) readDataFiles() error {
var d interface{} // map or slice
err = utils.UnmarshalYAMLInterface(b, &d)
if err != nil {
return utils.WrapPathError(err, "read YAML", filename)
return utils.WrapPathError(err, filename)
}
basename := utils.TrimExt(filepath.Base(f.Name()))
s.data[basename] = d

View File

@ -74,7 +74,7 @@ func (s *Site) readFiles() error {
defaultFrontmatter := s.config.GetFrontMatterDefaults("", relname)
p, err := pages.NewFile(s, filename, filepath.ToSlash(relname), defaultFrontmatter)
if err != nil {
return utils.WrapPathError(err, "read", filename)
return utils.WrapPathError(err, filename)
}
s.AddDocument(p, true)
return nil

40
utils/errors.go Normal file
View File

@ -0,0 +1,40 @@
package utils
import (
"fmt"
"os"
)
// A PathError is an error with a source path.
//
// An os.PathError is unfortunately not a PathError, but this is still
// useful for deciding whether to wrap other errors.
type PathError interface {
error
Path() string
}
type pathError struct {
cause error
path string
}
func (p *pathError) Error() string {
return fmt.Sprintf("%s: %s", p.path, p.cause)
}
// WrapPathError returns an error that will print with a path.\
// It wraps its argument if it is not nil and does not already provide a path.
func WrapPathError(err error, path string) error {
if err == nil {
return nil
}
switch err := err.(type) {
case PathError:
return err
case *os.PathError:
return err
default:
return &pathError{path: path, cause: err}
}
}

View File

@ -81,21 +81,6 @@ func NewPathError(op, name, text string) *os.PathError {
return &os.PathError{Op: op, Path: name, Err: errors.New(text)}
}
// WrapPathError returns an instance of *os.WrapPathError, by wrapping its argument
// if it is not already an instance.
// WrapPathError returns nil for a nil argument.
func WrapPathError(err error, op, name string) *os.PathError {
if err == nil {
return nil
}
switch err := err.(type) {
case *os.PathError:
return err
default:
return &os.PathError{Op: op, Path: name, Err: err}
}
}
// RemoveEmptyDirectories recursively removes empty directories.
func RemoveEmptyDirectories(root string) error {
walkFn := func(name string, info os.FileInfo, err error) error {