项目作者: bdlm

项目描述 :
Simple, concise error handling and annotation for Go
高级语言: Go
项目地址: git://github.com/bdlm/errors.git
创建时间: 2018-05-17T07:01:30Z
项目社区:https://github.com/bdlm/errors

开源协议:MIT License

下载


errors

Mature This project adheres to Semantic Versioning. This package is considered mature, you should expect package stability in Minor and Patch version releases

  • Major: backwards incompatible package updates
  • Minor: feature additions, removal of deprecated features
  • Patch: bug fixes, backward compatible model and function changes, etc.

CHANGELOG

Release
GoDoc
Build status
Coverage status
Go Report Card
Github issues
Github pull requests
MIT

errors provides simple, concise, useful error handling and annotation. This package aims to implement the Error Inspection and Error Values Go2 draft designs.

One of the biggest frustrations with Go error handling is the lack of forensic and meta information errors should provide. By default errors are just a string and possibly a type. They can’t tell you where they occurred or the path through the call stack they followed. The error implementation in Go is robust enough to control program flow but it’s not very efficient for troubleshooting or analysis.

Since the idom in Go is that we pass the error back up the stack anyway:

  1. if nil != err {
  2. return err
  3. }

it’s trivial to make errors much more informative with a simple error package. bdlm/errors makes this easy and supports tracing the call stack and the error callers with relative ease. Custom error types are also fully compatible with this package and can be used freely.

Install

  1. go get github.com/bdlm/errors/v2

Quick start

See the documentation for more examples. All package methods work with any error type as well as nil values, and error instances implement the Unwrap, Is, Marshaler, and Formatter interfaces as well as the github.com/bdlm/std/errors interfaces.

Create an error

  1. var MyError = errors.New("My error")

Create an error using formatting verbs

  1. var MyError = errors.Errorf("My error #%d", 1)

Wrap an error

  1. if nil != err {
  2. return errors.Wrap(err, "the operation failed")
  3. }

Wrap an error with another error

  1. err := try1()
  2. if nil != err {
  3. err2 := try2()
  4. if nil != err2 {
  5. return errors.WrapE(err, err2)
  6. }
  7. return err
  8. }

Get the previous error, if any

  1. err := doWork()
  2. if prevErr := errors.Unwrap(err); nil != prevErr {
  3. ...
  4. }

Test to see if a specific error type exists anywhere in an error stack

  1. var MyError = errors.New("My error")
  2. func main() {
  3. err := doWork()
  4. if errors.Is(err, MyError) {
  5. ...
  6. }
  7. }

Iterate through an error stack

  1. err := doWork()
  2. for nil != err {
  3. fmt.Println(err)
  4. err = errors.Unwrap(err)
  5. }

Formatting verbs

errors implements the %s and %v fmt.Formatter formatting verbs and several modifier flags:

Verbs
  • %s - Returns the error string of the last error added.
  • %v - Alias for %s
Flags
  • # - JSON formatted output, useful for logging
  • - - Output caller details, useful for troubleshooting
  • + - Output full error stack details, useful for debugging
  • - (space) Add whitespace formatting for readability, useful for development
Examples

fmt.Printf("%s", err)

  1. An error occurred

fmt.Printf("%v", err)

  1. An error occurred

fmt.Printf("%-v", err)

  1. #0 stack_test.go:40 (github.com/bdlm/error_test.TestErrors) - An error occurred

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

  1. #0 stack_test.go:40 (github.com/bdlm/error_test.TestErrors) - An error occurred #1 stack_test.go:39 (github.com/bdlm/error_test.TestErrors) - An error occurred

fmt.Printf("%#v", err)

  1. {"error":"An error occurred"}

fmt.Printf("%#-v", err)

  1. {"caller":"#0 stack_test.go:40 (github.com/bdlm/error_test.TestErrors)","error":"An error occurred"}

fmt.Printf("%#+v", err)

  1. [{"caller":"#0 stack_test.go:40 (github.com/bdlm/error_test.TestErrors)","error":"An error occurred"},{"caller":"#1 stack_test.go:39 (github.com/bdlm/error_test.TestErrors)","error":"An error occurred"}]

fmt.Printf("% #-v", err)

  1. {
  2. "caller": "#0 stack_test.go:40 (github.com/bdlm/error_test.TestErrors)",
  3. "error": "An error occurred"
  4. }

fmt.Printf("% #+v", err)

  1. [
  2. {
  3. "caller": "#0 stack_test.go:40 (github.com/bdlm/error_test.TestErrors)",
  4. "error": "An error occurred"
  5. },
  6. {
  7. "caller": "#1 stack_test.go:39 (github.com/bdlm/error_test.TestErrors)",
  8. "error": "An error occurred"
  9. }
  10. ]

#

See the documentation for more examples.