herrors

package
Version: v0.101.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2022 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package herrors contains common Hugo errors and error related utilities.

Package herrors contains common Hugo errors and error related utilities.

Index

Constants

This section is empty.

Variables

View Source
var ErrFeatureNotAvailable = errors.New("this feature is not available in your current Hugo version, see https://goo.gl/YMrWcn for more information")

ErrFeatureNotAvailable denotes that a feature is unavailable.

We will, at least to begin with, make some Hugo features (SCSS with libsass) optional, and this error is used to signal those situations.

View Source
var NopLineMatcher = func(m LineMatcher) int {
	return 1
}

NopLineMatcher is a matcher that always returns 1. This will effectively give line 1, column 1.

View Source
var OffsetMatcher = func(m LineMatcher) int {
	if m.Offset+len(m.Line) >= m.Position.Offset {

		return 0
	}
	return -1
}

OffsetMatcher is a line matcher that matches by offset.

View Source
var SimpleLineMatcher = func(m LineMatcher) int {
	if m.Position.LineNumber == m.LineNumber {

		return 0
	}
	return -1
}

SimpleLineMatcher simply matches by line number.

Functions

func Cause added in v0.99.0

func Cause(err error) error

Cause returns the underlying error or itself if it does not implement Unwrap.

func GetGID added in v0.63.0

func GetGID() uint64

Get the current goroutine id. Used only for debugging.

func Must added in v0.91.0

func Must(err error)

Must panics if err != nil.

func PrintStackTrace

func PrintStackTrace(w io.Writer)

PrintStackTrace prints the current stacktrace to w.

func Recover added in v0.56.0

func Recover(args ...any)

Recover is a helper function that can be used to capture panics. Put this at the top of a method/function that crashes in a template:

defer herrors.Recover()

Types

type ErrorContext

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.
	LinesPos int

	// The position of the content in the file. Note that this may be different from the error's position set
	// in FileError.
	Position text.Position

	// The lexer to use for syntax highlighting.
	// https://gohugo.io/content-management/syntax-highlighting/#list-of-chroma-highlighting-languages
	ChromaLexer string
}

ErrorContext contains contextual information about an error. This will typically be the lines surrounding some problem in a file.

type ErrorSender added in v0.65.0

type ErrorSender interface {
	SendError(err error)
}

ErrorSender is a, typically, non-blocking error handler.

type FileError

type FileError interface {
	error

	// ErroContext holds some context information about the error.
	ErrorContext() *ErrorContext

	text.Positioner

	// UpdatePosition updates the position of the error.
	UpdatePosition(pos text.Position) FileError

	// UpdateContent updates the error with a new ErrorContext from the content of the file.
	UpdateContent(r io.Reader, linematcher LineMatcherFn) FileError
}

FileError represents an error when handling a file: Parsing a config file, execute a template etc.

func NewFileError

func NewFileError(err error) FileError

NewFileError creates a new FileError that wraps err. It will try to extract the filename and line number from err.

func NewFileErrorFromFile added in v0.99.0

func NewFileErrorFromFile(err error, filename string, fs afero.Fs, linematcher LineMatcherFn) FileError

NewFileErrorFromFile is a convenience method to create a new FileError from a file.

func NewFileErrorFromFileInErr added in v0.99.0

func NewFileErrorFromFileInErr(err error, fs afero.Fs, linematcher LineMatcherFn) FileError

func NewFileErrorFromFileInPos added in v0.99.0

func NewFileErrorFromFileInPos(err error, pos text.Position, fs afero.Fs, linematcher LineMatcherFn) FileError

func NewFileErrorFromName added in v0.99.0

func NewFileErrorFromName(err error, name string) FileError

NewFileErrorFromName creates a new FileError that wraps err. The value for name should identify the file, the best being the full filename to the file on disk.

func NewFileErrorFromPos added in v0.99.0

func NewFileErrorFromPos(err error, pos text.Position) FileError

NewFileErrorFromPos will use the filename and line number from pos to create a new FileError, wrapping err.

func UnwrapFileError

func UnwrapFileError(err error) FileError

UnwrapFileError tries to unwrap a FileError from err. It returns nil if this is not possible.

func UnwrapFileErrors added in v0.99.0

func UnwrapFileErrors(err error) []FileError

UnwrapFileErrors tries to unwrap all FileError.

func UnwrapFileErrorsWithErrorContext added in v0.99.0

func UnwrapFileErrorsWithErrorContext(err error) []FileError

UnwrapFileErrorsWithErrorContext tries to unwrap all FileError in err that has an ErrorContext.

type LineMatcher

type LineMatcher struct {
	Position text.Position
	Error    error

	LineNumber int
	Offset     int
	Line       string
}

LineMatcher contains the elements used to match an error to a line

type LineMatcherFn

type LineMatcherFn func(m LineMatcher) int

LineMatcherFn is used to match a line with an error. It returns the column number or 0 if the line was found, but column could not be determinde. Returns -1 if no line match.

type Unwrapper added in v0.99.0

type Unwrapper interface {
	Unwrap() error
}

Unwrapper can unwrap errors created with fmt.Errorf.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL