errstack

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultWrapperFunctions = []PkgFuncs{
		{Pkg: "github.com/pkg/errors", Funcs: []string{"New", "Errorf", "Wrap", "Wrapf", "WithStack"}},
	}
	DefaultCleanFunctions = []PkgFuncs{
		{Pkg: "github.com/pkg/errors", Funcs: []string{"WithMessage", "WithMessagef"}},
	}
	DefaultThreshold     = .5
	DefaultMaxStackDepth = 5
)
View Source
var False = false
View Source
var True = true

Functions

func New

func New(settings any) (register.LinterPlugin, error)

func NewAnalyzer

func NewAnalyzer(config Config) *analysis.Analyzer

Types

type Assignment

type Assignment struct {
	Stmt *ast.AssignStmt
	LPos int
	RPos int
}

type Config added in v0.0.3

type Config struct {
	// WrapperFunctions - list of functions that are considered to wrap errors.
	// If you're using some fancy error wrapping library like github.com/pkg/errors,
	// you may want to add it to this list.
	// If you want to ignore some functions, simply don't add them to the list.
	WrapperFunctions []PkgFuncs `mapstructure:"wrapperFunctions" yaml:"wrapperFunctions"`
	// CleanFunctions - list of functions that are considered to clean errors without stacktrace.
	CleanFunctions []PkgFuncs `mapstructure:"cleanFunctions" yaml:"cleanFunctions"`
	// Threshold in percentage for the number of branches returning wrapped errors to be considered a violation.
	// Default value is 50%. Max is 100%.
	// That means that if there are 3 sources of error that are non-wrapped and one that is wrapped, ErrStack will report an error.
	// On the other hand, if there are 4 wrapped sources and only one non-wrapped source, ErrStack will not report an error.
	Threshold float64 `mapstructure:"threshold" yaml:"threshold"`
	// MaxStackDepth - how many stack frames to check for before giving up.
	// May impact performance on large codebases and high value.
	// Default value is 5. Max is 50.
	MaxStackDepth int `mapstructure:"maxStackDepth" yaml:"maxStackDepth"`
}

func NewDefaultConfig

func NewDefaultConfig() Config

type Ctx

type Ctx struct {
	Path   string
	Info   *types.Info
	Fset   *token.FileSet
	Syntax []*ast.File
}

func (*Ctx) Pos

func (c *Ctx) Pos(pos token.Pos) string

type ErrStack

type ErrStack struct {
	// contains filtered or unexported fields
}

func NewErrStack

func NewErrStack(config Config) *ErrStack

func (*ErrStack) NewCtx added in v0.0.3

func (es *ErrStack) NewCtx(pkgPath string) *Ctx

func (*ErrStack) NewFunc added in v0.0.3

func (es *ErrStack) NewFunc(callCtx *Ctx, call *ast.CallExpr, name, method string, pkg *types.Package) *Func

func (*ErrStack) Run

func (es *ErrStack) Run(pass *analysis.Pass) (any, error)

type Func

type Func struct {
	Ctx     *Ctx
	CallCtx *Ctx
	Name    string
	Method  string
	Pkg     *types.Package
	Call    *ast.CallExpr
	Decl    *ast.FuncDecl
	// contains filtered or unexported fields
}

func (*Func) IsBuiltin added in v0.0.3

func (fn *Func) IsBuiltin() bool

func (*Func) LoadDecl added in v0.0.3

func (fn *Func) LoadDecl()

LoadDecl assigns *ast.FuncDecl for function type.

type FunctionInfo

type FunctionInfo map[string][]string

func (FunctionInfo) Contains

func (f FunctionInfo) Contains(fn *Func) bool

type PkgFuncs added in v0.0.3

type PkgFuncs struct {
	Pkg   string   `mapstructure:"pkg"   yaml:"pkg"`
	Funcs []string `mapstructure:"funcs" yaml:"funcs"`
}

type Plugin

type Plugin struct {
	// contains filtered or unexported fields
}

func (*Plugin) BuildAnalyzers

func (p *Plugin) BuildAnalyzers() ([]*analysis.Analyzer, error)

func (*Plugin) GetLoadMode

func (p *Plugin) GetLoadMode() string

type Settings

type Settings struct {
	WrappedFunctions FunctionInfo
	CleanFunctions   FunctionInfo
	Threshold        float64
	MaxStackDepth    int
}

Jump to

Keyboard shortcuts

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