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.

Hyperledger Fabric代码可以使用第三方的包 github.com/pkg/errors 来GO语言提供的标准错误类型。 这个包可以简单的生成和展示堆栈追踪中的错误。

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.

github.com/pkg/errors 可以在所有的调用中替换 fmt.Errorf() 或者 errors.New() 。使用这个包可以生成一个调用栈附加在错误信息后面。

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

使用这个包很简单并且只需要对代码进行简单的微调。

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

首先,你要引入 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().

接下来,用错误产生函数 (errors.New(), errors.Errorf(), errors.WithMessage(), errors.Wrap(), errors.Wrapf())来更新所有从你的代码中产生的错误。

注解

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.

注解

参考 https://godoc.org/github.com/pkg/errors 完整的文档中可得到的错误生成函数。并且,参考后面的常规指南章节中更多在Fabric中使用这个包的具体的指南。

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.

最后,为logger或者fmt.Printf() 修改格式命令,调用``%s`` 为 %+v 在错误信息后打印调用栈。

General guidelines for error handling in Hyperledger Fabric

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.
  • 如果你是服务于一个用户请求,你应该记录错误并返回它
  • 如果错误来自外部源,比如一个Go库或者第三方的包,用errors.Wrap()来封装错误来为错误产生一个调用栈
  • 如果错误来自另外的Fabric函数,如果需要,用errors.WithMessage()对错误信息增加进一步的上下文来,这个函数对调用栈并不产生影响
  • 一个非常严重不可恢复的错误不应该允许传播到其他包

Example program

示例程序

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

下面的例子提供了一个很清晰的使用包 github.com/pkg/errors 的示例:

package main

import (
  "fmt"

  "github.com/pkg/errors"
)

func wrapWithStack() error {
  err := createError()
  // do this when error comes from external source (go lib or vendor)
  return errors.Wrap(err, "wrapping an error with stack")
}
func wrapWithoutStack() error {
  err := createError()
  // do this when error comes from internal Fabric since it already has stack trace
  return errors.WithMessage(err, "wrapping an error without stack")
}
func createError() error {
  return errors.New("original error")
}

func main() {
  err := createError()
  fmt.Printf("print error without stack: %s\n\n", err)
  fmt.Printf("print error with stack: %+v\n\n", err)
  err = wrapWithoutStack()
  fmt.Printf("%+v\n\n", err)
  err = wrapWithStack()
  fmt.Printf("%+v\n\n", err)
}