Error Code - zoobc/zoobc-core GitHub Wiki

Error code

In zoobc we defined some known internal errors in a struct form represented in common/blocker package.

Blocker struct {
    Type    TypeBlocker
    Message string
}

Every internal error returned will be propagated to the function caller. The error is defined as Type the type of errors such as [BlockErr, DBErr, ...]. The Message will be used to define the custom message for the error.

  • Usages

    Use the defined custom error type by constructing from the NewBlocker(type, message).

    import "github.com/zoobc/zoobc-core/common/blocker"
    
    ...
    func Foo() (*model.Transaction, error) {
        return nil, blocker.NewBlocker(
            blocker.BlockErr,
            "invalid block ID"
        )
    }
    ...
    

    In other case we need to know if the error is Blocker or go buildin. So we can casting this:

    if _, ok := err.(Blocker); ok { // should be Blocker
      if err.TypeBlocker == DBErr {
          // must be db error
      }
    } else {
      // must be go buildin error
    }
    fmt.Println(err.Error())
    

    Blocker implements the error interface, so Error() method can be used to get the message err.Error()

  • Error codes

    • BlockErr: error related to block creation, fetch, or deletion, the details will be define in the Message for more readable version.
    • DBErr: error related to persistance storage access.