Browse Source

implement error interface for ResponseDeliverTx/CheckTx/Commit

pull/1780/head
Anton Kaliaev 7 years ago
parent
commit
5fd83b3eee
No known key found for this signature in database GPG Key ID: 7B6881D965918214
2 changed files with 47 additions and 0 deletions
  1. +23
    -0
      types/errors.go
  2. +24
    -0
      types/result.go

+ 23
- 0
types/errors.go View File

@ -23,4 +23,27 @@ var (
ErrBaseUnknownAddress = NewError(CodeType_BaseUnknownAddress, "Error (base) unknown address")
ErrBaseUnknownPlugin = NewError(CodeType_BaseUnknownPlugin, "Error (base) unknown plugin")
ErrBaseUnknownPubKey = NewError(CodeType_BaseUnknownPubKey, "Error (base) unknown pubkey")
code2string = map[CodeType]string{
CodeType_InternalError: "Internal error",
CodeType_EncodingError: "Encoding error",
CodeType_BadNonce: "Error bad nonce",
CodeType_Unauthorized: "Unauthorized",
CodeType_InsufficientFunds: "Insufficient funds",
CodeType_UnknownRequest: "Unknown request",
CodeType_BaseDuplicateAddress: "Error (base) duplicate address",
CodeType_BaseEncodingError: "Error (base) encoding error",
CodeType_BaseInsufficientFees: "Error (base) insufficient fees",
CodeType_BaseInsufficientFunds: "Error (base) insufficient funds",
CodeType_BaseInsufficientGasPrice: "Error (base) insufficient gas price",
CodeType_BaseInvalidInput: "Error (base) invalid input",
CodeType_BaseInvalidOutput: "Error (base) invalid output",
CodeType_BaseInvalidPubKey: "Error (base) invalid pubkey",
CodeType_BaseInvalidSequence: "Error (base) invalid sequence",
CodeType_BaseInvalidSignature: "Error (base) invalid signature",
CodeType_BaseUnknownAddress: "Error (base) unknown address",
CodeType_BaseUnknownPlugin: "Error (base) unknown plugin",
CodeType_BaseUnknownPubKey: "Error (base) unknown pubkey",
}
)

+ 24
- 0
types/result.go View File

@ -111,6 +111,11 @@ func (r ResponseCheckTx) IsErr() bool {
return r.Code != CodeType_OK
}
// Error implements error interface by formatting response as string.
func (r ResponseCheckTx) Error() string {
return fmtError(r.Code, r.Log)
}
// Convert ResponseDeliverTx to standard Result
func (r *ResponseDeliverTx) Result() Result {
return Result{
@ -126,6 +131,11 @@ func (r ResponseDeliverTx) IsErr() bool {
return r.Code != CodeType_OK
}
// Error implements error interface by formatting response as string.
func (r ResponseDeliverTx) Error() string {
return fmtError(r.Code, r.Log)
}
type ResultQuery struct {
Code CodeType `json:"code"`
Index int64 `json:"index"`
@ -152,3 +162,17 @@ func (r *ResponseQuery) Result() *ResultQuery {
func (r ResponseCommit) IsErr() bool {
return r.Code != CodeType_OK
}
// Error implements error interface by formatting response as string.
func (r ResponseCommit) Error() string {
return fmtError(r.Code, r.Log)
}
func fmtError(code CodeType, log string) string {
codeAsStr, ok := code2string[code]
if ok {
return fmt.Sprintf("%s (%v): %s", codeAsStr, code, log)
} else {
return fmt.Sprintf("Unknown error (%v): %s", code, log)
}
}

Loading…
Cancel
Save