1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 09:59:03 +01:00
gojekyll/utils/errors.go
2017-07-12 08:17:19 -04:00

41 lines
785 B
Go

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}
}
}