errorx

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2020 License: MIT Imports: 8 Imported by: 0

README

errorx

Build Status codecov Go Report Card
GoDoc

golang combine error package

go get github.com/goapt/errorx  

使用

errorx包定义了全局的基本错误如下

  • ErrSystem 系统错误,最顶层的错误定义,所有全局定义的错误必须包裹这个错误
  • ErrDatabase 数据库错误
  • ErrRedis Redis错误
  • ErrNetwork 网络错误

其中分别提供了四种错误包裹的快捷方法

  • errorx.System(err) 系统错误包裹
  • errorx.Database(err) 数据库错误包裹
  • errorx.Redis(err) Redis错误包裹
  • errorx.Network(err) 网络错误包裹

四种错误包裹都提供额外的msg参数来重置错误信息如下

errorx.Database(err, "订单不存在")

同样我们保留了DbPrettyNoMoreRowsDbFilterNoMoreRows两个方法,来辅助快捷的重置无数据错误信息,以及过滤无数据错误

errorx.DbPrettyNoMoreRows(err, "订单不存在")
errorx.DbFilterNoMoreRows(err)

New一个普通错误,带调用栈

err := errorx.New("这是一个错误")

包裹一个普通错误,希望他拥有调用栈

err := errors.New("普通的错误")
errorx.Wrap(err)

判定错误

有了包裹错误的方式,我们就可以使用官方的errors.Is来判定错误是否是系统错误,或者数据库错误以及官方包的错误等

errors.Is(err,errorx.System)
errors.Is(err,errorx.Database)
errors.Is(err,sql.ErrNoRows)

打印调用堆栈

有时候我们不仅仅只想获取错误信息,还希望知道错误的调用堆栈,我们可以使用%+v来打印如下

fmt.Sprintf("%+v",err)

输出如下

sql: no rows in result set
github.com/goapt/errorx.TestCombErr_Error.func1
	/Users/fifsky/wwwroot/go/github.com/goapt/errorx/error_test.go:57
github.com/goapt/errorx.TestCombErr_Error.func1
	/Users/fifsky/wwwroot/go/github.com/goapt/errorx/error_test.go:58
github.com/goapt/errorx.TestCombErr_Error.func1
	/Users/fifsky/wwwroot/go/github.com/goapt/errorx/error_test.go:59
github.com/goapt/errorx.TestCombErr_Error.func1
	/Users/fifsky/wwwroot/go/github.com/goapt/errorx/error_test.go:60

业务层错误

由于我们业务层统一规范结构如下

{
    "code":"InvalidEmail",
    "msg": "无效的邮箱"
}

因此我们提供了辅助方法来定义这种结构的错误如下

var ErrInvalidEmail = errorx.NewCode("InvalidEmail","无效的邮箱")

第二个参数,支持string,error,因此你可以轻松的包裹一个错误如下

var err := errorx.NewCode("ConnectFail",errorx.Database(sql.ErrNoRows))
//下面都是成立的
errors.Is(err,errorx.ErrSystem)
errors.Is(err,errorx.ErrDatabase)
errors.Is(err,sql.ErrNoRows)

然后在业务层的response就可以轻松的断言出错误是否为 err.(errorx.CodeError)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrGeneral  = errors.New("错误")
	ErrSystem   = errors.New("系统异常")
	ErrDatabase = fmt.Errorf("数据库异常:%w", ErrSystem)
	ErrRedis    = fmt.Errorf("redis异常:%w", ErrSystem)
	ErrNetwork  = fmt.Errorf("网络异常:%w", ErrSystem)
)

Functions

func Database

func Database(err error, msg ...string) error

组合数据库错误

func DbFilterNoMoreRows

func DbFilterNoMoreRows(err error) error

DbFilterNoMoreRows 过滤sql.ErrNoRows错误

func DbPrettyNoMoreRows

func DbPrettyNoMoreRows(err error, msg string) error

DbPrettyNoMoreRows 优化查无记录的错误 调用方无需额外判断是否为空记录,只需要指定遇到空记录时,替换的错误提示

func Network

func Network(err error, msg ...string) error

组合一个HTTP错误

func New

func New(msg string) error

func Redis

func Redis(err error, msg ...string) error

组合一个Redis错误

func System

func System(err error, msg ...string) error

组合系统错误,使得这个错误可以使用errors.Is判别出这两个错误

func Wrap

func Wrap(err error, msg ...string) error

包裹普通错误,让其拥有调用栈信息

Types

type CodeError

type CodeError struct {
	Code string
	Msg  string
	// contains filtered or unexported fields
}

func NewCode

func NewCode(code string, msg interface{}) *CodeError

func (CodeError) Error

func (e CodeError) Error() string

func (*CodeError) Format

func (e *CodeError) Format(s fmt.State, verb rune)

func (*CodeError) Is

func (e *CodeError) Is(err error) bool

func (CodeError) StackTrace

func (s CodeError) StackTrace() StackTrace

func (*CodeError) Unwrap

func (e *CodeError) Unwrap() error

type Frame

type Frame uintptr

Frame represents a program counter inside a stack frame. For historical reasons if Frame is interpreted as a uintptr its value represents the program counter + 1.

func (Frame) Format

func (f Frame) Format(s fmt.State, verb rune)

Format formats the frame according to the fmt.Formatter interface.

%s    source file
%d    source line
%n    function name
%v    equivalent to %s:%d

Format accepts flags that alter the printing of some verbs, as follows:

%+s   function name and path of source file relative to the compile time
      GOPATH separated by \n\t (<funcname>\n\t<path>)
%+v   equivalent to %+s:%d

func (Frame) MarshalText

func (f Frame) MarshalText() ([]byte, error)

MarshalText formats a stacktrace Frame as a text string. The output is the same as that of fmt.Sprintf("%+v", f), but without newlines or tabs.

type StackTrace

type StackTrace []Frame

StackTrace is stack of Frames from innermost (newest) to outermost (oldest).

func (StackTrace) Format

func (st StackTrace) Format(s fmt.State, verb rune)

Format formats the stack of Frames according to the fmt.Formatter interface.

%s	lists source files for each Frame in the stack
%v	lists the source file and line number for each Frame in the stack

Format accepts flags that alter the printing of some verbs, as follows:

%+v   Prints filename, function, and line number for each Frame in the stack.

Jump to

Keyboard shortcuts

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