-
Notifications
You must be signed in to change notification settings - Fork 3
Error Code
iltoga edited this page Sep 10, 2019
·
3 revisions
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.
-
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
orgo 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 messageerr.Error()
-
-
BlockErr
: error related to block creation, fetch, or deletion, the details will be define in theMessage
for more readable version. -
DBErr
: error related to persistance storage access.
-