errpos

package
v0.0.0-...-8de7dcd Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

package errpos provides position/syntax and context path based errors, compatible with linters and humans.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddContext

func AddContext(err error, ctx ...string) error

WithContext adds context elements to an error. If err is nil, nil is returned If the errors.As matches `*Error` (not an interface), the input is added to the *start* of the existing context. Otherwise a new Error is returned.

func AddFilename

func AddFilename(err error, filename string) error

func AddPosition

func AddPosition(err error, pos Position) error

AddPosition adds a source position to an error. If the error is nil, returns nil. If the error already has a position (implements Position), it is returned unmodified, as the existing value is likely more specific and useful.

func AddSource

func AddSource(err error, fileData string) error

func AddSourceFile

func AddSourceFile(err error, filename string, fileData string) error

Types

type Context

type Context []string

Context is the location of an error within a schema or other 'walkable' structure. The elements are arbitrary and defined by the producer.

func (Context) String

func (c Context) String() string

type Err

type Err struct {
	Pos *Position
	Ctx Context
	Err error
}

Error wraps it all together. Short names are annoying but - duck typing.

func AsError

func AsError(err error) (*Err, bool)

func (*Err) Error

func (e *Err) Error() string

func (*Err) ErrorPosition

func (e *Err) ErrorPosition() *Position

func (*Err) Unwrap

func (e *Err) Unwrap() error

Unwrap implements errors.Wrapper, but is also useful to get the 'context free' error message.

type Errors

type Errors []*Err

Errors allows a list of errors to be treated as a single error passing through the tree, but split out at the end, e.g. multiple syntax errors in a file.

func AsErrors

func AsErrors(err error) (Errors, bool)

func (Errors) Append

func (e Errors) Append(err error) Errors

func (Errors) Error

func (e Errors) Error() string

type ErrorsWithSource

type ErrorsWithSource struct {
	Errors Errors
	// contains filtered or unexported fields
}

func AsErrorsWithSource

func AsErrorsWithSource(err error) (*ErrorsWithSource, bool)

func MustAddSource

func MustAddSource(err error, fileSource string) (*ErrorsWithSource, error)

func (ErrorsWithSource) Error

func (e ErrorsWithSource) Error() string

func (ErrorsWithSource) HumanString

func (e ErrorsWithSource) HumanString(contextLines int) string

type HasPosition

type HasPosition interface {
	error
	ErrorPosition() *Position
}

type Point

type Point struct {
	Line   int
	Column int
}

func (Point) String

func (p Point) String() string

type Position

type Position struct {
	// Optional filename. Nothing special about the value but
	// it is printed where a filename would usually be printed.
	Filename *string

	Start Point
	End   Point
}

Position represents a position within a file-like string

func GetErrorPosition

func GetErrorPosition(err error) *Position

func (Position) Position

func (p Position) Position() Position

func (Position) String

func (p Position) String() string

Jump to

Keyboard shortcuts

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