diff --git a/renderers/markdown.go b/renderers/markdown.go index 6ebe0c9..04b39db 100644 --- a/renderers/markdown.go +++ b/renderers/markdown.go @@ -2,10 +2,10 @@ package renderers import ( "bytes" - "fmt" "io" "regexp" + "github.com/osteele/gojekyll/utils" "github.com/russross/blackfriday" "golang.org/x/net/html" ) @@ -39,7 +39,7 @@ func renderMarkdown(md []byte) ([]byte, error) { ) html, err := renderInnerMarkdown(html) if err != nil { - return nil, fmt.Errorf("%s while rendering markdown", err) + return nil, utils.WrapError(err, "markdown") } return html, nil } diff --git a/utils/errors.go b/utils/errors.go index c8db807..594fa67 100644 --- a/utils/errors.go +++ b/utils/errors.go @@ -5,12 +5,40 @@ import ( "os" ) +// A WrappedError decorates an error with a message +type WrappedError interface { + error + Cause() error +} + +// WrapError returns an error decorated with a message. +// If the error is nil, it returns nil. +func WrapError(err error, m string) error { + if err == nil { + return nil + } + return &wrappedError{cause: err, message: m} +} + +type wrappedError struct { + cause error + message string +} + +func (we *wrappedError) Cause() error { + return we.cause +} + +func (we *wrappedError) Error() string { + return fmt.Sprintf("%s: %s", we.message, we.cause) +} + // 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 + WrappedError Path() string } @@ -19,6 +47,10 @@ type pathError struct { path string } +func (pe *pathError) Cause() error { + return pe.cause +} + func (pe *pathError) Error() string { return fmt.Sprintf("%s: %s", pe.path, pe.cause) }