Skip to content

Commit 7bee71e

Browse files
committed
[FAB-3548] Define error and component codes
This changeset defines the starter set of error and component codes as constants to use in error reporting. Change-Id: I8cf02f6fa76d2bc858e5c287da6d7cf9cc1c5fc1 Signed-off-by: Binh Q. Nguyen <[email protected]>
1 parent 2f55f4a commit 7bee71e

File tree

3 files changed

+121
-26
lines changed

3 files changed

+121
-26
lines changed

common/errors/codes.go

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Copyright Digital Asset Holdings, LLC 2016 All Rights Reserved.
3+
Copyright IBM Corp. 2017 All Rights Reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
package errors
19+
20+
// A set of constants for error reason codes, which is based on HTTP codes
21+
// http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
22+
const (
23+
// Invalid inputs on API calls
24+
BadRequest = "400"
25+
26+
// Forbidden due to access control issues
27+
Forbidden = "403"
28+
29+
// Not Found (eg chaincode not found)
30+
NotFound = "404"
31+
32+
// Request timeout (chaincode or ledger)
33+
Timeout = "408"
34+
35+
// Example, duplicate transactions or replay attacks
36+
Conflict = "409"
37+
38+
// Request for resource is not available. Example, a chaincode has
39+
// been upgraded and the request uses an old version
40+
Gone = "410"
41+
42+
// Payload of the request exceeds allowed size
43+
PayloadTooLarge = "413"
44+
45+
// Example, marshal/unmarshalling protobuf error
46+
UnprocessableEntity = "422"
47+
48+
// Protocol version is no longer supported
49+
UpgradeRequired = "426"
50+
51+
// Internal server errors that are not classified below
52+
Internal = "500"
53+
54+
// Requested chaincode function has not been implemented
55+
NotImplemented = "501"
56+
57+
// Requested chaincode is not available
58+
Unavailable = "503"
59+
60+
// File IO errors
61+
FileIO = "520"
62+
63+
// Network IO errors
64+
NetworkIO = "521"
65+
)
66+
67+
// A set of constants for component codes
68+
const (
69+
// BCCSP is fabic/BCCSP
70+
BCCSP = "CSP"
71+
72+
// Common is fabric/common
73+
Common = "CMN"
74+
75+
// Core is fabric/core
76+
Core = "COR"
77+
78+
// Event is fabric/events component
79+
Event = "EVT"
80+
81+
// Gossip is fabric/gossip
82+
Gossip = "GSP"
83+
84+
// Ledger is fabric/core/ledger
85+
Ledger = "LGR"
86+
87+
// Peer is fabric/peer
88+
Peer = "PER"
89+
90+
// Orderer is fabric/orderer
91+
Orderer = "ORD"
92+
93+
// MSP is fabric/msp
94+
MSP = "MSP"
95+
96+
// ChaincodeSupport is fabric/core/chaincode
97+
ChaincodeSupport = "CCS"
98+
99+
// DeliveryService is fabric/core/deliverservice
100+
DeliveryService = "CDS"
101+
102+
// SystemChaincode is fabric/core/scc (system chaincode)
103+
SystemChaincode = "SCC"
104+
)

common/errors/errors_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ func TestError(t *testing.T) {
3737
var tc []testCase
3838

3939
tc = append(tc,
40-
testCase{"UnknownErrorWithCallstack", []string{"UNK", "404", "An unknown error occurred."}, nil, nil, nil, []bool{true}},
41-
testCase{"UnknownError", []string{"UNK", "404", "An unknown error occurred."}, nil, nil, nil, []bool{false}},
42-
testCase{"UnknownErrorWithCallstackAndArg", []string{"UNK", "405", "An error occurred: %s"}, []string{"arg1"}, nil, nil, []bool{true}},
43-
testCase{"UnknownErrorWithArg", []string{"UNK", "405", "An error occurred: %s"}, []string{"arg1"}, nil, nil, []bool{false}},
44-
testCase{"CallStackErrorWrappedCallstackError", []string{"CHA", "404", "An unknown error occurred."}, nil, []string{"UNK", "404", "An unknown error occurred."}, nil, []bool{true, true}},
45-
testCase{"ErrorWrappedError", []string{"CHA", "404", "An unknown error occurred."}, nil, []string{"UNK", "404", "An unknown error occurred."}, nil, []bool{false, false}},
46-
testCase{"CallStackErrorWrappedError", []string{"CHA", "404", "An unknown error occurred."}, nil, []string{"UNK", "404", "An unknown error occurred."}, nil, []bool{true, false}},
47-
testCase{"ErrorWrappedCallStackError", []string{"CHA", "404", "An unknown error occurred."}, nil, []string{"UNK", "404", "An unknown error occurred."}, nil, []bool{false, true}},
48-
testCase{"ErrorWrappedStandardError", []string{"CHA", "404", "An unknown error occurred."}, nil, []string{"grpc timed out: %s"}, []string{"failed to connect to server"}, []bool{false, true}},
40+
testCase{"UnknownErrorWithCallstack", []string{Core, NotFound, "An unknown error occurred."}, nil, nil, nil, []bool{true}},
41+
testCase{"UnknownError", []string{MSP, Forbidden, "An unknown error occurred."}, nil, nil, nil, []bool{false}},
42+
testCase{"UnknownErrorWithCallstackAndArg", []string{Ledger, Conflict, "An error occurred: %s"}, []string{"arg1"}, nil, nil, []bool{true}},
43+
testCase{"UnknownErrorWithArg", []string{SystemChaincode, Internal, "An error occurred: %s"}, []string{"arg1"}, nil, nil, []bool{false}},
44+
testCase{"CallStackErrorWrappedCallstackError", []string{BCCSP, NotImplemented, "An unknown error occurred."}, nil, []string{Peer, UpgradeRequired, "An unknown error occurred."}, nil, []bool{true, true}},
45+
testCase{"ErrorWrappedError", []string{Common, Unavailable, "An unknown error occurred."}, nil, []string{SystemChaincode, Gone, "An unknown error occurred."}, nil, []bool{false, false}},
46+
testCase{"CallStackErrorWrappedError", []string{Event, Timeout, "An unknown error occurred."}, nil, []string{Orderer, NetworkIO, "An unknown error occurred."}, nil, []bool{true, false}},
47+
testCase{"ErrorWrappedCallStackError", []string{Orderer, UnprocessableEntity, "An unknown error occurred."}, nil, []string{"UNK", "404", "An unknown error occurred."}, nil, []bool{false, true}},
48+
testCase{"ErrorWrappedStandardError", []string{DeliveryService, Unavailable, "An unknown error occurred."}, nil, []string{"grpc timed out: %s"}, []string{"failed to connect to server"}, []bool{false, true}},
4949
)
5050

5151
assert := assert.New(t)

docs/source/error-handling.rst

+8-17
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,14 @@ A CallStackError consists of the following:
1111

1212
- Component code - a name for the general area of the code that is generating
1313
the error. Component codes should consist of three uppercase letters. Numerics
14-
and special characters are not allowed.
15-
16-
.. note:: Make sure to use a consistent component name across code in related
17-
files/packages.
18-
Examples of component codes (with their full component name in parentheses)
19-
are: CSP (bccsp), CMN (common), COR (core), CCS (core/chaincode), CDS
20-
(core/deliverservice), SCC (core/scc), EVT (events), GSP (gossip), LGR
21-
(ledger), PER (peer), ORD (orderer)
22-
23-
We may, in the future, add constants to allow searching for currently defined
24-
components for those using an editor with code completion capabilities.
25-
14+
and special characters are not allowed. A set of component codes is defined
15+
in common/errors/codes.go
2616
- Reason code - a short code to help identify the reason the error occurred.
2717
Reason codes should consist of three numeric values. Letters and special
28-
characters are not allowed.
18+
characters are not allowed. A set of reason codes is defined in
19+
common/error/codes.go
2920
- Error code - the component code and reason code separated by a colon,
30-
e.g. PER:404
21+
e.g. MSP:404
3122
- Error message - the text that describes the error. This is the same as the
3223
input provided to ``fmt.Errorf()`` and ``Errors.New()``. If an error has been
3324
wrapped into the current error, its message will be appended.
@@ -39,9 +30,9 @@ The CallStackError interface exposes the following functions:
3930

4031
- Error() - returns the error message with callstack appended
4132
- Message() - returns the error message (without callstack appended)
42-
- GetComponentCode()
43-
- GetReasonCode()
44-
- GetErrorCode()
33+
- GetComponentCode() - returns the 3-character component code
34+
- GetReasonCode() - returns the 3-digit reason code
35+
- GetErrorCode() - returns the error code, which is "component:reason"
4536
- GetStack() - returns just the callstack
4637
- WrapError(error) - wraps the provided error into the CallStackError
4738

0 commit comments

Comments
 (0)