Error handling

General Overview

Hyperledger Fabric code should use the vendored package github.com/pkg/errors in place of the standard error type provided by Go. This package allows easy generation and display of stack traces with error messages.

Usage Instructions

github.com/pkg/errors should be used in place of all calls to fmt.Errorf() or errors.New(). Using this package will generate a call stack that will be appended to the error message.

Using this package is simple and will only require easy tweaks to your code.

First, you’ll need to import github.com/pkg/errors.

Next, update all errors that are generated by your code to use one of the error creation functions (errors.New(), errors.Errorf(), errors.WithMessage(), errors.Wrap(), errors.Wrapf().

Note

See https://godoc.org/github.com/pkg/errors for complete documentation of the available error creation function. Also, refer to the General guidelines section below for more specific guidelines for using the package for Fabric code.

Finally, change the formatting directive for any logger or fmt.Printf() calls from %s to %+v to print the call stack along with the error message.

General guidelines for error handling in Hyperledger Fabric

  • If you are servicing a user request, you should log the error and return it.

  • If the error comes from an external source, such as a Go library or vendored package, wrap the error using errors.Wrap() to generate a call stack for the error.

  • If the error comes from another Fabric function, add further context, if desired, to the error message using errors.WithMessage() while leaving the call stack unaffected.

  • A panic should not be allowed to propagate to other packages.

Example program

The following example program provides a clear demonstration of using the package:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/pkg/errors"
  5. )
  6. func wrapWithStack() error {
  7. err := createError()
  8. // do this when error comes from external source (go lib or vendor)
  9. return errors.Wrap(err, "wrapping an error with stack")
  10. }
  11. func wrapWithoutStack() error {
  12. err := createError()
  13. // do this when error comes from internal Fabric since it already has stack trace
  14. return errors.WithMessage(err, "wrapping an error without stack")
  15. }
  16. func createError() error {
  17. return errors.New("original error")
  18. }
  19. func main() {
  20. err := createError()
  21. fmt.Printf("print error without stack: %s\n\n", err)
  22. fmt.Printf("print error with stack: %+v\n\n", err)
  23. err = wrapWithoutStack()
  24. fmt.Printf("%+v\n\n", err)
  25. err = wrapWithStack()
  26. fmt.Printf("%+v\n\n", err)
  27. }