tracerr

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2024 License: MIT Imports: 6 Imported by: 4

README

相对官方版本做了一点新增

至少需要go1.18及以上版本, 因为加了泛型.

新增内容

  • Wrap1()用于一些函数返回多值的场景当中, 例如下面这个例子.
func ParseTime(layout, v string) (time.Time, error) {
	return Wrap1(time.Parse(layout, v))
}

Documentation

Overview

Package tracerr makes error output more informative. It adds stack trace to error and can display error with source fragments.

Check example of output here https://github.com/ztrue/tracerr

Index

Constants

This section is empty.

Variables

View Source
var DefaultCap = 20

DefaultCap is a default cap for frames array. It can be changed to number of expected frames for purpose of performance optimisation.

View Source
var DefaultLinesAfter = 2

DefaultLinesAfter is number of source lines after traced line to display.

View Source
var DefaultLinesBefore = 3

DefaultLinesBefore is number of source lines before traced line to display.

Functions

func Print

func Print(err error)

Print prints error message with stack trace.

func PrintSource

func PrintSource(err error, nums ...int)

PrintSource prints error message with stack trace and source fragments.

By default, 6 lines of source code will be printed, see DefaultLinesAfter and DefaultLinesBefore.

Pass a single number to specify a total number of source lines.

Pass two numbers to specify exactly how many lines should be shown before and after traced line.

func PrintSourceColor

func PrintSourceColor(err error, nums ...int)

PrintSourceColor prints error message with stack trace and source fragments, which are in color. Output rules are the same as in PrintSource.

func Sprint

func Sprint(err error) string

Sprint returns error output by the same rules as Print.

func SprintSource

func SprintSource(err error, nums ...int) string

SprintSource returns error output by the same rules as PrintSource.

func SprintSourceColor

func SprintSourceColor(err error, nums ...int) string

SprintSourceColor returns error output by the same rules as PrintSourceColor.

func Unwrap

func Unwrap(err error) error

Unwrap returns the original error.

Types

type Error

type Error interface {
	Error() string
	StackTrace() []Frame
	Unwrap() error
}

Error is an error with stack trace.

func CustomError

func CustomError(err error, frames []Frame) Error

CustomError creates an error with provided frames.

func Errorf

func Errorf(message string, args ...interface{}) Error

Errorf creates new error with stacktrace and formatted message. Formatting works the same way as in fmt.Errorf.

func New

func New(message string) Error

New creates new error with stacktrace.

func Wrap

func Wrap(err error) Error

Wrap adds stacktrace to existing error.

func Wrap1

func Wrap1[T any](v T, err error) (T, Error)

Wrap1 adds stacktrace to existing error.

type Frame

type Frame struct {
	// Func contains a function name.
	Func string
	// Line contains a line number.
	Line int
	// Path contains a file path.
	Path string
}

Frame is a single step in stack trace.

func StackTrace

func StackTrace(err error) []Frame

StackTrace returns stack trace of an error. It will be empty if err is not of type Error.

func (Frame) String

func (f Frame) String() string

String formats Frame to string.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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