Help us shape the future of Go! Take the 2026 Go Developer Survey

zerrors

package module
v0.0.0-...-170ee7a Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2020 License: Apache-2.0 Imports: 7 Imported by: 6

README

能保留调用链的错误包


获得

go get -u github.com/zlyuancn/zerrors

部分代码非原创

文档

godoc

示例

package main

import (
    "fmt"
    "github.com/zlyuancn/zerrors"
)

func f2() error {
    return zerrors.New("f2")
}

func f1() error {
    err := f2()
    err = zerrors.Wrap(err, "f1")
    return err
}

func main() {
    err := f1()
    fmt.Println(zerrors.ToString(err))
    fmt.Println(zerrors.ToDetailString(err))
}

方法描述

完整调用链

创建一个包含完整调用链的错误
    New(message string) error 
    Newf(format string, args ...interface{}) error
让一个error持有当前完整调用链
    WithStack(err error) error
对一个错误进行描述并包含当前完整调用链
    Wrap(err error, message string) error
    Wrapf(err error, format string, args ...interface{}) error

简单调用信息

创建一个包含当前调用信息的错误
    NewSimple(message string) error
    NewSimplef(format string, args ...interface{}) error
让一个error持有当前调用信息
    WithSimple(err error) error
对一个错误进行描述并包含当前调用信息
    WrapSimple(err error, message string) error
    WrapSimplef(err error, format string, args ...interface{}) error

不包含调用信息

对一个错误进行描述, 不包含调用信息
    WithMessage(err error, message string) error
    WithMessagef(err error, format string, args ...interface{}) error

错误列表

创建一个error列表
    NewErrors() *Errors

工具

获取错误原因, 它会透过zerrors模块的包装获取最开始的错误
    Cause(err error) error
获取详细的错误描述
    ToDetailString(err error) string
获取简要的错误描述
    ToString(err error) string

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cause

func Cause(err error) error

获取错误原因, 它会透过zerrors模块的包装获取最开始的错误

func Errorf

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

创建一个包含完整调用链的错误

func New

func New(message string) error

创建一个包含完整调用链的错误

func NewSimple

func NewSimple(message string) error

创建一个包含当前调用信息的错误

func NewSimplef

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

创建一个包含当前调用信息的错误

func Newf

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

创建一个包含完整调用链的错误

func ToDetailString

func ToDetailString(err error) string

获取详细的错误描述

func ToString

func ToString(err error) string

获取简要的错误描述

func WithMessage

func WithMessage(err error, message string) error

对一个错误进行描述, 不包含调用信息

func WithMessagef

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

对一个错误进行描述, 不包含调用信息

func WithSimple

func WithSimple(err error) error

让一个error持有当前调用信息

func WithStack

func WithStack(err error) error

让一个error持有当前完整调用链

func Wrap

func Wrap(err error, message string) error

对一个错误进行描述并包含当前完整调用链

func WrapSimple

func WrapSimple(err error, message string) error

对一个错误进行描述并包含当前调用信息

func WrapSimplef

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

对一个错误进行描述并包含当前调用信息

func Wrapf

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

对一个错误进行描述并包含当前完整调用链

Types

type Errors

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

error列表

func NewErrors

func NewErrors() *Errors

创建一个error列表

func (*Errors) Add

func (m *Errors) Add(texts ...string)

添加一些错误文本, 自动包含当前调用信息

func (*Errors) AddErrs

func (m *Errors) AddErrs(errs ...error)

添加一些错误

func (*Errors) AddNoStack

func (m *Errors) AddNoStack(texts ...string)

添加一些错误文本, 不需要调用信息

func (*Errors) Addf

func (m *Errors) Addf(format string, a ...interface{})

添加一个格式化错误文本, 自动包含当前调用信息

func (*Errors) AddfNoStack

func (m *Errors) AddfNoStack(format string, a ...interface{})

添加一个格式化错误文本, 不需要调用信息

func (*Errors) Err

func (m *Errors) Err() error

转为error

func (*Errors) Error

func (m *Errors) Error() string

实现error接口

func (*Errors) Errs

func (m *Errors) Errs() []error

获取所有的err

func (*Errors) FirstErr

func (m *Errors) FirstErr() error

返回第一个error

func (*Errors) Format

func (m *Errors) Format(s fmt.State, verb rune)

func (*Errors) String

func (m *Errors) String() string

type Frame

type Frame uintptr

func (Frame) Format

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

type StackTrace

type StackTrace []Frame

func (StackTrace) Format

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

Jump to

Keyboard shortcuts

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