errorx

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2020 License: MIT Imports: 4 Imported by: 16

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var MaxStackDepth = 5

最大可纪录的调用堆栈的深度

Functions

func Errorf

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

Errorf 创建一个具有文本信息的错误

func New

func New(text string) error

New 创建一个具有文本信息的错误

Types

type CodedError

type CodedError interface {
	error
	Code() int       // 返回错误代码
	Message() string // 返回错误消息
}

CodedError 具有code的错误类型

Example
package main

import (
	"fmt"

	"github.com/recallsong/go-utils/errorx"
)

func main() {
	err := errorx.NewCodedError(400, "bad request")
	fmt.Println(err)
}
Output:

400, bad request

func NewCodedError

func NewCodedError(code int, msg string) CodedError

NewCodedError 创建一个具有编码的错误

type Errors

type Errors []error

Errors is a slice of errors implementing the error interface.

func (*Errors) Append

func (errs *Errors) Append(err error)

func (Errors) Error

func (errs Errors) Error() string

func (Errors) MaybeUnwrap

func (errs Errors) MaybeUnwrap() error

MaybeUnwrap returns nil if len(errs) is 0. It returns the first and only contained error as error if len(errs is 1). In all other cases, it returns the Errors directly. This is helpful for returning a Errors in a way that only uses the Errors if needed.

func (Errors) Num

func (errs Errors) Num() int

type MultiError

type MultiError interface {
	error
	Num() int // 返回错误的数量
	MaybeUnwrap() error
}

MultiError 多个错误的集合类型

Example
package main

import (
	"fmt"

	"github.com/recallsong/go-utils/errorx"
)

func main() {
	err0 := errorx.New("error 0")
	err1 := errorx.New("error 1")
	err2 := errorx.New("error 2")
	errs := errorx.NewMultiError(err0, err1, err2)

	fmt.Println(errs)

	var empty errorx.Errors
	fmt.Println(empty.MaybeUnwrap())
}
Output:

3 error(s) occurred:
* error 0
* error 1
* error 2
<nil>

func NewMultiError

func NewMultiError(errs ...error) MultiError

NewMultiError 创建错误集合

type StringError

type StringError string

StringError 简单的字符串型错误

Example
package main

import (
	"fmt"

	"github.com/recallsong/go-utils/errorx"
)

func main() {
	var err error = errorx.New("this is error message")
	fmt.Println(err)
}
Output:

this is error message

func (StringError) Error

func (err StringError) Error() string

type TracedError

type TracedError interface {
	error
	Callers() []uintptr // 返回堆栈调用信息
	Unpack() error      // 返回去掉堆栈调用信息的错误
}
Example
package main

import (
	"fmt"

	"github.com/recallsong/go-utils/errorx"
)

func main() {
	err := errorx.NewTracedError("this is error message")
	fmt.Println(err)
}
Output:

this is error message :
* [example_test.go:43]	errorx_test.ExampleTracedError
* [example.go:122]	testing.runExample
* [example.go:46]	testing.runExamples
* [testing.go:922]	testing.(*M).Run
* [_testmain.go:56]	main.main

func NewTracedError

func NewTracedError(err interface{}) TracedError

NewTracedError 创建一个具有堆栈调用信息的错误

Jump to

Keyboard shortcuts

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