diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-10-21 12:20:21 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-10-22 20:46:14 +0200 |
commit | d1661b823af25c50d3bbe5366ea40a3cdd52e237 (patch) | |
tree | cd84d18229fb9c294ff1be56d7c0ce92a8f46761 /common/herrors/file_error.go | |
parent | 7930d2132a3c36c1aaca20f16f56978c84656b0a (diff) |
hugolib: Continue the file context/line number errors work
See #5324
Diffstat (limited to 'common/herrors/file_error.go')
-rw-r--r-- | common/herrors/file_error.go | 41 |
1 files changed, 18 insertions, 23 deletions
diff --git a/common/herrors/file_error.go b/common/herrors/file_error.go index f29f91fcc..86ccfcefb 100644 --- a/common/herrors/file_error.go +++ b/common/herrors/file_error.go @@ -13,10 +13,6 @@ package herrors -import ( - "fmt" -) - var _ causer = (*fileError)(nil) // FileError represents an error when handling a file: Parsing a config file, @@ -27,6 +23,8 @@ type FileError interface { // LineNumber gets the error location, starting at line 1. LineNumber() int + ColumnNumber() int + // A string identifying the type of file, e.g. JSON, TOML, markdown etc. Type() string } @@ -34,9 +32,9 @@ type FileError interface { var _ FileError = (*fileError)(nil) type fileError struct { - lineNumber int - fileType string - msg string + lineNumber int + columnNumber int + fileType string cause error } @@ -45,32 +43,28 @@ func (e *fileError) LineNumber() int { return e.lineNumber } +func (e *fileError) ColumnNumber() int { + return e.columnNumber +} + func (e *fileError) Type() string { return e.fileType } func (e *fileError) Error() string { - return e.msg + if e.cause == nil { + return "" + } + return e.cause.Error() } func (f *fileError) Cause() error { return f.cause } -func (e *fileError) Format(s fmt.State, verb rune) { - switch verb { - case 'v': - fallthrough - case 's': - fmt.Fprintf(s, "%s:%d: %s:%s", e.fileType, e.lineNumber, e.msg, e.cause) - case 'q': - fmt.Fprintf(s, "%q:%d: %q:%q", e.fileType, e.lineNumber, e.msg, e.cause) - } -} - // NewFileError creates a new FileError. -func NewFileError(fileType string, lineNumber int, msg string, err error) FileError { - return &fileError{cause: err, fileType: fileType, lineNumber: lineNumber, msg: msg} +func NewFileError(fileType string, lineNumber, columnNumber int, err error) FileError { + return &fileError{cause: err, fileType: fileType, lineNumber: lineNumber, columnNumber: columnNumber} } // UnwrapFileError tries to unwrap a FileError from err. @@ -101,9 +95,10 @@ func ToFileError(fileType string, err error) error { // If will fall back to returning the original error if a line number cannot be extracted. func ToFileErrorWithOffset(fileType string, err error, offset int) error { for _, handle := range lineNumberExtractors { - lno, msg := handle(err, offset) + + lno, col := handle(err) if lno > 0 { - return NewFileError(fileType, lno, msg, err) + return NewFileError(fileType, lno+offset, col, err) } } // Fall back to the original. |