Skip to content

Commit 823ea13

Browse files
committed
Add documentation for error handling framework
This change adds in documentation and guidelines for use of the error handling framework. https://jira.hyperledger.org/browse/FAB-1571 Change-Id: I1168f610928007432d9c48338d3f1959b9f0f14a Signed-off-by: Will Lahti <[email protected]>
1 parent f7c19f8 commit 823ea13

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

docs/tech/error-handling.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Hyperledger Fabric - Error handling
2+
3+
## Usage instructions
4+
5+
The new error handling framework should be used in place of all calls to fmt.Errorf() or Errors.new(). Using this framework will provide error codes to check against as well as the option to generate a callstack that will be appended to the error message when logging.error is set to debug in peer/core.yaml.
6+
7+
Using the framework is simple and will only require an easy tweak to your code. First, you'll need to import “github.com/hyperledger/fabric/core/errors” in any code that uses this framework.
8+
9+
Let's take the following as an example: fmt.Errorf(“Error trying to connect to local peer: %s”, err.Error())
10+
11+
For this error, we will simply call the constructor for Error and pass a component name, error message name, followed by the error message and any arguments to format into the message (note the component and message names are case insensitive and will be converted to uppercase):
12+
13+
err = errors.Error(“Peer”, “ConnectionError”, “Error trying to connect to local peer: %s”, err.Error())
14+
15+
For more critical errors for which a callstack would be beneficial, the error can be created as follows:
16+
err = errors.ErrorWithCallstack(“Peer”, “ConnectionError”, “Error trying to connect to local peer: %s”, err.Error())
17+
18+
Note: Make sure to use a consistent component name across code in related files/packages.
19+
20+
Examples of components of the Peer are: Peer, Orderer, Gossip, Ledger, MSP, Chaincode, Comm
21+
Examples of components of the Orderer are: Orderer, SBFT, Kafka, Solo
22+
23+
We may, in the future, add constants to allow searching for currently defined components for those using an editor with code completion capabilities; we are avoiding this for now to avoid merge conflict issues.
24+
25+
## Displaying error messages
26+
27+
Once the error has been created using the framework, displaying the error message is as simple as
28+
29+
logger.Warningf("%s", err) or fmt.Printf("%s\n", err)
30+
31+
which, for the example above, would display the error message:
32+
33+
PEER_CONNECTIONERROR - Error trying to connect to local peer: grpc: timed out when dialing
34+
35+
## Setting stack trace to display for any CallStackError
36+
37+
The display of the stack trace with an error is tied to the logging level for the “error” module, which is initialized using logging.error in core.yaml. It can also be set dynamically for code running on the peer via CLI using “peer logging setlevel error <log-level>”. The default level of “warning” will not display the stack trace; setting it to “debug” will display the stack trace for all errors that use this error handling framework.
38+
39+
## General guidelines for error handling in Fabric
40+
41+
- If it is some sort of best effort thing you are doing, you should log the error and ignore it.
42+
- If you are servicing a user request, you should log the error and return it.
43+
- If the error comes from elsewhere in the Fabric, do not stack the error (errors.Error(“I am stacking errors: %s”, original_error)), simply return the original error.
44+
- A panic should be handled within the same layer by throwing an internal error code/start a recovery process and should not be allowed to propagate to other packages.

0 commit comments

Comments
 (0)