2018-10-03 08:58:09 -04:00
|
|
|
// Copyright 2018 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Package errors contains common Hugo errors and error related utilities.
|
|
|
|
package herrors
|
|
|
|
|
|
|
|
import (
|
2018-10-21 06:20:21 -04:00
|
|
|
"fmt"
|
2018-10-03 08:58:09 -04:00
|
|
|
"io"
|
2018-10-23 02:54:10 -04:00
|
|
|
"io/ioutil"
|
2018-10-24 05:14:51 -04:00
|
|
|
"os"
|
2018-10-03 08:58:09 -04:00
|
|
|
"strings"
|
|
|
|
|
2018-10-24 05:14:51 -04:00
|
|
|
"github.com/gohugoio/hugo/common/terminal"
|
2018-10-21 06:20:21 -04:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
2018-10-27 11:19:36 -04:00
|
|
|
var fileErrorFormatFunc func(e ErrorContext) string
|
2018-10-21 06:20:21 -04:00
|
|
|
|
2018-10-27 11:19:36 -04:00
|
|
|
func createFileLogFormatter(formatStr string) func(e ErrorContext) string {
|
|
|
|
|
|
|
|
if formatStr == "" {
|
|
|
|
formatStr = "\":file::line::col\""
|
|
|
|
}
|
|
|
|
|
|
|
|
var identifiers = []string{":file", ":line", ":col"}
|
|
|
|
var identifiersFound []string
|
|
|
|
|
|
|
|
for i := range formatStr {
|
|
|
|
for _, id := range identifiers {
|
|
|
|
if strings.HasPrefix(formatStr[i:], id) {
|
|
|
|
identifiersFound = append(identifiersFound, id)
|
|
|
|
}
|
|
|
|
}
|
2018-10-24 05:14:51 -04:00
|
|
|
}
|
2018-10-27 11:19:36 -04:00
|
|
|
|
|
|
|
replacer := strings.NewReplacer(":file", "%s", ":line", "%d", ":col", "%d")
|
|
|
|
format := replacer.Replace(formatStr)
|
|
|
|
|
|
|
|
f := func(e ErrorContext) string {
|
|
|
|
args := make([]interface{}, len(identifiersFound))
|
|
|
|
for i, id := range identifiersFound {
|
|
|
|
switch id {
|
|
|
|
case ":file":
|
|
|
|
args[i] = e.Filename
|
|
|
|
case ":line":
|
|
|
|
args[i] = e.LineNumber
|
|
|
|
case ":col":
|
|
|
|
args[i] = e.ColumnNumber
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := fmt.Sprintf(format, args...)
|
|
|
|
|
|
|
|
if terminal.IsTerminal(os.Stdout) {
|
|
|
|
return terminal.Notice(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
fileErrorFormatFunc = createFileLogFormatter(os.Getenv("HUGO_FILE_LOG_FORMAT"))
|
2018-10-24 05:14:51 -04:00
|
|
|
}
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
// LineMatcher contains the elements used to match an error to a line
|
|
|
|
type LineMatcher struct {
|
|
|
|
FileError FileError
|
|
|
|
LineNumber int
|
|
|
|
Offset int
|
|
|
|
Line string
|
|
|
|
}
|
|
|
|
|
|
|
|
// LineMatcherFn is used to match a line with an error.
|
|
|
|
type LineMatcherFn func(m LineMatcher) bool
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
// SimpleLineMatcher simply matches by line number.
|
|
|
|
var SimpleLineMatcher = func(m LineMatcher) bool {
|
|
|
|
return m.FileError.LineNumber() == m.LineNumber
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorContext contains contextual information about an error. This will
|
|
|
|
// typically be the lines surrounding some problem in a file.
|
|
|
|
type ErrorContext struct {
|
2018-10-21 06:20:21 -04:00
|
|
|
// The source filename.
|
|
|
|
Filename string
|
2018-10-03 08:58:09 -04:00
|
|
|
|
|
|
|
// If a match will contain the matched line and up to 2 lines before and after.
|
|
|
|
// Will be empty if no match.
|
|
|
|
Lines []string
|
|
|
|
|
|
|
|
// The position of the error in the Lines above. 0 based.
|
|
|
|
Pos int
|
|
|
|
|
|
|
|
// The linenumber in the source file from where the Lines start. Starting at 1.
|
|
|
|
LineNumber int
|
|
|
|
|
2018-10-21 06:20:21 -04:00
|
|
|
// The column number in the source file. Starting at 1.
|
|
|
|
ColumnNumber int
|
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
// The lexer to use for syntax highlighting.
|
|
|
|
// https://gohugo.io/content-management/syntax-highlighting/#list-of-chroma-highlighting-languages
|
|
|
|
ChromaLexer string
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ causer = (*ErrorWithFileContext)(nil)
|
|
|
|
|
|
|
|
// ErrorWithFileContext is an error with some additional file context related
|
|
|
|
// to that error.
|
|
|
|
type ErrorWithFileContext struct {
|
|
|
|
cause error
|
|
|
|
ErrorContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ErrorWithFileContext) Error() string {
|
2018-10-27 11:19:36 -04:00
|
|
|
return fileErrorFormatFunc(e.ErrorContext) + ": " + e.cause.Error()
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ErrorWithFileContext) Cause() error {
|
|
|
|
return e.cause
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithFileContextForFile will try to add a file context with lines matching the given matcher.
|
|
|
|
// If no match could be found, the original error is returned with false as the second return value.
|
2018-10-23 02:54:10 -04:00
|
|
|
func WithFileContextForFile(e error, realFilename, filename string, fs afero.Fs, matcher LineMatcherFn) (error, bool) {
|
2018-10-03 08:58:09 -04:00
|
|
|
f, err := fs.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return e, false
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2018-10-21 06:20:21 -04:00
|
|
|
return WithFileContext(e, realFilename, f, matcher)
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithFileContextForFile will try to add a file context with lines matching the given matcher.
|
|
|
|
// If no match could be found, the original error is returned with false as the second return value.
|
2018-10-23 02:54:10 -04:00
|
|
|
func WithFileContext(e error, realFilename string, r io.Reader, matcher LineMatcherFn) (error, bool) {
|
2018-10-03 08:58:09 -04:00
|
|
|
if e == nil {
|
|
|
|
panic("error missing")
|
|
|
|
}
|
|
|
|
le := UnwrapFileError(e)
|
2018-10-23 02:54:10 -04:00
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
if le == nil {
|
|
|
|
var ok bool
|
2018-10-21 06:20:21 -04:00
|
|
|
if le, ok = ToFileError("", e).(FileError); !ok {
|
2018-10-03 08:58:09 -04:00
|
|
|
return e, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
var errCtx ErrorContext
|
|
|
|
|
|
|
|
if le.Offset() != -1 {
|
|
|
|
errCtx = locateError(r, le, func(m LineMatcher) bool {
|
|
|
|
if le.Offset() >= m.Offset && le.Offset() < m.Offset+len(m.Line) {
|
|
|
|
fe := m.FileError
|
|
|
|
m.FileError = ToFileErrorWithOffset(fe, -fe.LineNumber()+m.LineNumber)
|
|
|
|
}
|
|
|
|
return matcher(m)
|
|
|
|
})
|
|
|
|
|
|
|
|
} else {
|
|
|
|
errCtx = locateError(r, le, matcher)
|
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
|
|
|
|
if errCtx.LineNumber == -1 {
|
|
|
|
return e, false
|
|
|
|
}
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
errCtx.Filename = realFilename
|
|
|
|
|
2018-10-21 06:20:21 -04:00
|
|
|
if le.Type() != "" {
|
2018-10-03 08:58:09 -04:00
|
|
|
errCtx.ChromaLexer = chromaLexerFromType(le.Type())
|
2018-10-21 06:20:21 -04:00
|
|
|
} else {
|
|
|
|
errCtx.ChromaLexer = chromaLexerFromFilename(realFilename)
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return &ErrorWithFileContext{cause: e, ErrorContext: errCtx}, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnwrapErrorWithFileContext tries to unwrap an ErrorWithFileContext from err.
|
|
|
|
// It returns nil if this is not possible.
|
|
|
|
func UnwrapErrorWithFileContext(err error) *ErrorWithFileContext {
|
|
|
|
for err != nil {
|
|
|
|
switch v := err.(type) {
|
|
|
|
case *ErrorWithFileContext:
|
|
|
|
return v
|
|
|
|
case causer:
|
|
|
|
err = v.Cause()
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func chromaLexerFromType(fileType string) string {
|
2018-10-21 06:20:21 -04:00
|
|
|
switch fileType {
|
|
|
|
case "html", "htm":
|
|
|
|
return "go-html-template"
|
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
return fileType
|
|
|
|
}
|
|
|
|
|
2018-10-21 06:20:21 -04:00
|
|
|
func chromaLexerFromFilename(filename string) string {
|
|
|
|
if strings.Contains(filename, "layouts") {
|
|
|
|
return "go-html-template"
|
|
|
|
}
|
|
|
|
|
|
|
|
ext := helpers.ExtNoDelimiter(filename)
|
|
|
|
return chromaLexerFromType(ext)
|
|
|
|
}
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
func locateErrorInString(src string, matcher LineMatcherFn) ErrorContext {
|
|
|
|
return locateError(strings.NewReader(src), &fileError{}, matcher)
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
func locateError(r io.Reader, le FileError, matches LineMatcherFn) ErrorContext {
|
|
|
|
if le == nil {
|
|
|
|
panic("must provide an error")
|
2018-10-21 06:20:21 -04:00
|
|
|
}
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
errCtx := ErrorContext{LineNumber: -1, ColumnNumber: 1, Pos: -1}
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return errCtx
|
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
lines := strings.Split(string(b), "\n")
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
if le != nil && le.ColumnNumber() >= 0 {
|
|
|
|
errCtx.ColumnNumber = le.ColumnNumber()
|
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
lineNo := 0
|
|
|
|
posBytes := 0
|
|
|
|
|
|
|
|
for li, line := range lines {
|
|
|
|
lineNo = li + 1
|
|
|
|
m := LineMatcher{
|
|
|
|
FileError: le,
|
|
|
|
LineNumber: lineNo,
|
|
|
|
Offset: posBytes,
|
|
|
|
Line: line,
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
2018-10-23 02:54:10 -04:00
|
|
|
if errCtx.Pos == -1 && matches(m) {
|
2018-10-21 06:20:21 -04:00
|
|
|
errCtx.LineNumber = lineNo
|
2018-10-23 02:54:10 -04:00
|
|
|
break
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
posBytes += len(line)
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
if errCtx.LineNumber != -1 {
|
|
|
|
low := errCtx.LineNumber - 3
|
|
|
|
if low < 0 {
|
|
|
|
low = 0
|
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
if errCtx.LineNumber > 2 {
|
|
|
|
errCtx.Pos = 2
|
|
|
|
} else {
|
|
|
|
errCtx.Pos = errCtx.LineNumber - 1
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
high := errCtx.LineNumber + 2
|
|
|
|
if high > len(lines) {
|
|
|
|
high = len(lines)
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
errCtx.Lines = lines[low:high]
|
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return errCtx
|
|
|
|
}
|