You are viewing an old version of this page. View the current version.
Compare with Current View Page History
« Previous Version 32 Next »
GoFrame框架提供了强大、丰富的错误处理能力,由gerror组件实现。
GoFrame
gerror
使用方式:
import "github.com/gogf/gf/errors/gerror"
接口文档:
https://godoc.org/github.com/gogf/gf/errors/gerror
New/Newf
func New(text string) error func Newf(format string, args ...interface{}) error
用于创建一个自定义错误信息的error对象,并包含堆栈信息。
error
Wrap/Wrapf
func Wrap(err error, text string) error func Wrapf(err error, format string, args ...interface{}) error
用于包裹其他错误error对象,构造成多级的错误信息,包含堆栈信息。
NewSkip/NewSkipf
func NewSkip(skip int, text string) error func NewSkipf(skip int, format string, args ...interface{}) error
用于创建一个自定义错误信息的error对象,并且忽略部分堆栈信息(按照当前调用方法位置往上忽略)。高级功能,一般开发者很少用得到。
func NewCode(code int, text string) error func NewCodef(code int, format string, args ...interface{}) error func NewCodeSkip(code, skip int, text string) error func NewCodeSkipf(code, skip int, format string, args ...interface{}) error func WrapCode(code int, err error, text string) error func WrapCodef(code int, err error, format string, args ...interface{}) error
具体介绍请参考后续 错误处理-错误码特性 章节。
常用方法的基准性能测试:https://github.com/gogf/gf/blob/master/errors/gerror/gerror_z_bench_test.go
$ go test *.go -bench=".*" -benchmem goos: linux goarch: amd64 Benchmark_New-4 1890454 589 ns/op 256 B/op 1 allocs/op Benchmark_Newf-4 1569258 762 ns/op 324 B/op 3 allocs/op Benchmark_Wrap-4 2012910 600 ns/op 256 B/op 1 allocs/op Benchmark_Wrapf-4 1600197 749 ns/op 324 B/op 3 allocs/op Benchmark_NewSkip-4 2009928 579 ns/op 256 B/op 1 allocs/op Benchmark_NewSkipf-4 1578370 752 ns/op 324 B/op 3 allocs/op Benchmark_NewCode-4 2060835 591 ns/op 256 B/op 1 allocs/op Benchmark_NewCodef-4 1603306 752 ns/op 324 B/op 3 allocs/op Benchmark_NewCodeSkip-4 2047794 584 ns/op 256 B/op 1 allocs/op Benchmark_NewCodeSkipf-4 1524495 779 ns/op 324 B/op 3 allocs/op Benchmark_WrapCode-4 1972147 603 ns/op 256 B/op 1 allocs/op Benchmark_WrapCodef-4 1651316 735 ns/op 324 B/op 3 allocs/op