errors

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2022 License: AGPL-3.0 Imports: 9 Imported by: 2

Documentation

Overview

Package errors

使用[github.com/juju/errors,github.com/pkg/errors]时加入一些当前项目的需求

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Annotate

func Annotate(other error, message string, args ...interface{}) error

Annotate 为错误添加注释信息(包含调用栈信息)

func Annotatef deprecated

func Annotatef(other error, format string, args ...interface{}) error

Annotatef 兼容旧代码中使用 github.com/juju/errors.Annotatef

Deprecated: 不应该被使用,应该使用 Annotate

func As

func As(err error, target interface{}) bool

As 兼容标准库

func Cause

func Cause(err error) error

Cause 通过循环 Unwrap ,它将找到最初的错误. 除非提供的 err 是 nil, 否则 Unwrap 不应该返回 nil

func Chain

func Chain(err error) string

Chain 会循环 Unwrap ,找到 error 的每层包装, 并格式化(包含调用栈信息和注释信息)到单行

go run ./errors/_examples/chain/main.go
                                                             : EOF
4980726 .../smallsung/gopkg/errors/_examples/chain/main.go:12:
4980741 .../smallsung/gopkg/errors/_examples/chain/main.go:13: 注释信息1
4980805 .../smallsung/gopkg/errors/_examples/chain/main.go:14:
4980830 .../smallsung/gopkg/errors/_examples/chain/main.go:15: 注释信息2

func Details

func Details(err error) string

Details 展示完整的错误信息. 它包含 最初错误信息,错误包装信息,最内部实现 Callers 的 error 的堆栈

go run ./errors/_examples/details/main.go
error:
EOF

errors:
                                                                            : EOF
7867990 .../smallsung/gopkg/errors/_examples/details/main.go:12:
7868005 .../smallsung/gopkg/errors/_examples/details/main.go:13: 注释信息1
7868069 .../smallsung/gopkg/errors/_examples/details/main.go:14:
7868094 .../smallsung/gopkg/errors/_examples/details/main.go:15: 注释信息2

stacks:
7867990 .../smallsung/gopkg/errors/_examples/details/main.go:12: main.main
7377781 .../src/runtime/proc.go:225: runtime.main
7563840 .../src/runtime/asm_amd64.s:1371: runtime.goexit

func ErrorStack deprecated

func ErrorStack(err error) string

ErrorStack 兼容旧代码中使用 github.com/juju/errors.ErrorStack

Deprecated: 不应该被使用,应该使用 Chain

func Errorf deprecated

func Errorf(format string, args ...interface{}) error

Errorf 兼容旧代码中使用 github.com/pkg/errors.Errorf github.com/juju/errors.Errorf

Deprecated: 不应该被使用,应该使用 Format

func Format

func Format(format string, args ...interface{}) error

Format 如 fmt.Errorf 一样.但是返回的错误包含调用栈信息.

func Is

func Is(err, target error) bool

Is 兼容标准库

func New

func New(args ...interface{}) error

New 是标准库的替代品.它返回一个包含调用栈信息的错误.

New()
New(message string)
New(format string, args ...interface{})

func PC

func PC(pc uintptr) runtime.Frame

func Position

func Position() error

Position 起名字最麻烦了,能定位抛出就好。

func Stacks

func Stacks(err error) string

Stacks 返回最内部实现 Callers 的 error 的堆栈

func Trace

func Trace(other error) error

Trace 为错误添加调用栈信息

func Unwrap

func Unwrap(err error) error

Unwrap 兼容标准库

func WithMessage deprecated

func WithMessage(err error, message string, args ...interface{}) error

WithMessage 兼容旧代码中使用 github.com/pkg/errors.WithMessage

Deprecated: 不应该被使用,应该使用 Annotate

func WithMessagef deprecated

func WithMessagef(err error, format string, args ...interface{}) error

WithMessagef 兼容旧代码中使用 github.com/pkg/errors.WithMessagef

Deprecated: 不应该被使用,应该使用 Annotate

func WithStack deprecated

func WithStack(err error) error

Deprecated: 不应该被使用,应该使用 Trace

Types

type Annotator

type Annotator interface {
	//Annotate 返回错误注释信息
	Annotate() string
}

type Caller

type Caller interface {
	// Caller 返回包装错误调用栈
	Caller() uintptr
}

type Callers

type Callers interface {
	// Callers 返回包装错误完整调用栈
	Callers() []uintptr
}

type Err

type Err = LocatorError

Err github.com/juju/errors.Err

func NewErr

func NewErr(format string, args ...interface{}) Err

NewErr github.com/juju/errors.NewErr

func NewErrWithCause

func NewErrWithCause(other error, format string, args ...interface{}) Err

NewErrWithCause github.com/juju/errors.NewErrWithCause

type Location

type Location interface {
	// Location 返回调用包装错误的文件和行
	Location() (string, int)
}

type Locator

type Locator interface {
	//SetLocation 设置调用包装的文件和行.供 locator 调用
	SetLocation(skip int)
}

func NewLocator

func NewLocator() Locator

type LocatorError

type LocatorError interface {
	error
	Locator

	// Formatter
	// %s	输出最内层错误消息
	// %v	输出包含注释(如果有)错误消息。 annotate: error
	// %+v	输出错误链 pc file:line: error
	fmt.Formatter

	Caller
	Callers
	Locator
	Annotator
	UnWrapper
	// contains filtered or unexported methods
}

func NewCauserAnnotatorLocatorError

func NewCauserAnnotatorLocatorError(cause error, format string, args ...interface{}) LocatorError

func NewCauserLocatorError

func NewCauserLocatorError(cause error) LocatorError

func NewLocatorError

func NewLocatorError(format string, args ...interface{}) LocatorError

type Messenger

type Messenger interface {
	Message() string
}

Messenger github.com/juju/errors

type UnWrapper

type UnWrapper interface {
	//Unwrap go1.13支持. 不建议返回 nil
	Unwrap() error
}

Directories

Path Synopsis
_examples
chain command
details command

Jump to

Keyboard shortcuts

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