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 (
|
|
|
|
"io"
|
2018-10-23 02:54:10 -04:00
|
|
|
"io/ioutil"
|
2018-11-15 03:28:02 -05:00
|
|
|
"path/filepath"
|
2018-10-03 08:58:09 -04:00
|
|
|
"strings"
|
|
|
|
|
2018-11-01 06:28:30 -04:00
|
|
|
"github.com/gohugoio/hugo/common/text"
|
2018-10-21 06:20:21 -04:00
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
// LineMatcher contains the elements used to match an error to a line
|
|
|
|
type LineMatcher struct {
|
2018-11-01 06:28:30 -04:00
|
|
|
Position text.Position
|
|
|
|
Error error
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
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 {
|
2018-11-01 06:28:30 -04:00
|
|
|
return m.Position.LineNumber == m.LineNumber
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2018-11-01 06:28:30 -04:00
|
|
|
var _ text.Positioner = ErrorContext{}
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
// 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.
|
2018-11-01 06:28:30 -04:00
|
|
|
LinesPos int
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2018-11-01 06:28:30 -04:00
|
|
|
position text.Position
|
2018-10-21 06:20:21 -04:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-11-01 06:28:30 -04:00
|
|
|
// Position returns the text position of this error.
|
|
|
|
func (e ErrorContext) Position() text.Position {
|
|
|
|
return e.position
|
|
|
|
}
|
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
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-11-01 06:28:30 -04:00
|
|
|
pos := e.Position()
|
|
|
|
if pos.IsValid() {
|
|
|
|
return pos.String() + ": " + e.cause.Error()
|
|
|
|
}
|
|
|
|
return 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
|
|
|
|
|
2018-11-01 06:28:30 -04:00
|
|
|
posle := le.Position()
|
|
|
|
|
|
|
|
if posle.Offset != -1 {
|
2018-10-23 02:54:10 -04:00
|
|
|
errCtx = locateError(r, le, func(m LineMatcher) bool {
|
2018-11-01 06:28:30 -04:00
|
|
|
if posle.Offset >= m.Offset && posle.Offset < m.Offset+len(m.Line) {
|
|
|
|
lno := posle.LineNumber - m.Position.LineNumber + m.LineNumber
|
|
|
|
m.Position = text.Position{LineNumber: lno}
|
2018-10-23 02:54:10 -04:00
|
|
|
}
|
|
|
|
return matcher(m)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
errCtx = locateError(r, le, matcher)
|
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2018-11-01 06:28:30 -04:00
|
|
|
pos := &errCtx.position
|
|
|
|
|
|
|
|
if pos.LineNumber == -1 {
|
2018-10-03 08:58:09 -04:00
|
|
|
return e, false
|
|
|
|
}
|
|
|
|
|
2018-11-01 06:28:30 -04:00
|
|
|
pos.Filename = realFilename
|
2018-10-23 02:54:10 -04:00
|
|
|
|
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-11-15 03:28:02 -05:00
|
|
|
func extNoDelimiter(filename string) string {
|
2019-02-02 02:19:12 -05:00
|
|
|
return strings.TrimPrefix(filepath.Ext(filename), ".")
|
2018-11-15 03:28:02 -05:00
|
|
|
}
|
|
|
|
|
2018-10-21 06:20:21 -04:00
|
|
|
func chromaLexerFromFilename(filename string) string {
|
|
|
|
if strings.Contains(filename, "layouts") {
|
|
|
|
return "go-html-template"
|
|
|
|
}
|
|
|
|
|
2018-11-15 03:28:02 -05:00
|
|
|
ext := extNoDelimiter(filename)
|
2018-10-21 06:20:21 -04:00
|
|
|
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-11-01 06:28:30 -04:00
|
|
|
errCtx := ErrorContext{position: text.Position{LineNumber: -1, ColumnNumber: 1, Offset: -1}, LinesPos: -1}
|
2018-10-23 02:54:10 -04:00
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return errCtx
|
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2018-11-01 06:28:30 -04:00
|
|
|
pos := &errCtx.position
|
|
|
|
lepos := le.Position()
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
lines := strings.Split(string(b), "\n")
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2019-08-02 11:20:36 -04:00
|
|
|
if lepos.ColumnNumber >= 0 {
|
2018-11-01 06:28:30 -04:00
|
|
|
pos.ColumnNumber = lepos.ColumnNumber
|
2018-10-23 02:54:10 -04:00
|
|
|
}
|
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{
|
2018-11-01 06:28:30 -04:00
|
|
|
Position: le.Position(),
|
|
|
|
Error: le,
|
2018-10-23 02:54:10 -04:00
|
|
|
LineNumber: lineNo,
|
|
|
|
Offset: posBytes,
|
|
|
|
Line: line,
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
2018-11-01 06:28:30 -04:00
|
|
|
if errCtx.LinesPos == -1 && matches(m) {
|
|
|
|
pos.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-11-01 06:28:30 -04:00
|
|
|
if pos.LineNumber != -1 {
|
|
|
|
low := pos.LineNumber - 3
|
2018-10-23 02:54:10 -04:00
|
|
|
if low < 0 {
|
|
|
|
low = 0
|
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2018-11-01 06:28:30 -04:00
|
|
|
if pos.LineNumber > 2 {
|
|
|
|
errCtx.LinesPos = 2
|
2018-10-23 02:54:10 -04:00
|
|
|
} else {
|
2018-11-01 06:28:30 -04:00
|
|
|
errCtx.LinesPos = pos.LineNumber - 1
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2018-11-01 06:28:30 -04:00
|
|
|
high := pos.LineNumber + 2
|
2018-10-23 02:54:10 -04:00
|
|
|
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
|
|
|
|
}
|