Go study notes (72)-Go third-party library errors with stack error handling

Pack github.com/pkg/errorsallows developers to easily in the errorbelt stack information on the error messages, faster and more accurate positioning errors, such as line number and other information.

If the item code is more complex, and often need to track Bug, recommend the use of github.com/pkg/errorsthis error-handling package.

installation

go get -v github.com/pkg/errors

The standard library package and errorspackage the same name, and has New()a function, error handling from the standard library into stack up with error handling is quite easy. The following code uses the errors.Wrap()function error information packaging:

package main

import (
    "fmt"

    "github.com/pkg/errors"
)

var ErrNameEmpty = errors.New("Name can't be empty!")

func (s *Student) SetName(newName string) (err error) {
    
    
    if newName == "" || s.Name == "" {
    
    
        return ErrNameEmpty
    } else {
    
    
        s.Name = newName
        return nil
    }
}

type Student struct {
    
    
    Name string
    Age  int
}

func NewStu() (err error) {
    
    
    stu := &Student{
    
    Age: 19}
    e := stu.SetName("")

    if e != nil {
    
    
        return errors.Wrap(e, "set name failed!")
    } else {
    
    
        return nil
    }

}

func main() {
    
    
    e := NewStu()
    fmt.Printf("%+v\n", e)
}

Finally Printf(), setting the placeholder %+verror message is displayed and the stack information, such as file name of the function is running, lines, etc., while the placeholder %sonly display error messages.

The following error message with a stack of information makes it easy for programmers to locate a problem position, for example main.go:30, show that in the main.go30 lines of the file.

Name can't be empty!
main.init
    main.go:9
runtime.main
    /src/runtime/proc.go:189
runtime.goexit
    /src/runtime/asm_amd64.s:1333
set name failed!
main.NewStu
    main.go:30
main.main
    main.go:38
runtime.main
    /src/runtime/proc.go:201
runtime.goexit
    /src/runtime/asm_amd64.s:1333
  • Wrap() The function appends stack information and new prompt information at the same time on the basis of existing errors,
  • WithMessage() The function appends new prompt information on the basis of existing errors,
  • WithStack()The function appends stack information on the basis of existing errors. In practice, you can choose to use it according to the situation.

This package is relatively simple and practical, and may be included in the standard library, so it is recommended to use it in actual production.
Further environmental variables GOTRACEBACKhave a greater impact on the output information of the stack information, different values are output information varying levels of detail.

  • GOTRACEBACK = none
  • GOTRACEBACK = single (default value)
  • GOTRACEBACK = all
  • GOTRACEBACK = system
  • GOTRACEBACK = crash

In the program debug.SetTraceback (level string)function can be provided the variable.

Guess you like

Origin blog.csdn.net/wohu1104/article/details/113795455