rpc: add custom errors to sendrawtransaction

In case of unsuccessful relyReason sendRawTransaction should return
errors with codes [-500, ..., -505] instead of error with code -32603
This commit is contained in:
Anna Shaleva 2020-03-05 20:04:05 +03:00
parent a746d8e6e6
commit 2031d8d103

View file

@ -854,28 +854,24 @@ func (s *Server) sendrawtransaction(reqParams request.Params) (interface{}, erro
tx := &transaction.Transaction{} tx := &transaction.Transaction{}
tx.DecodeBinary(r) tx.DecodeBinary(r)
if r.Err != nil { if r.Err != nil {
err = errors.Wrap(r.Err, "transaction DecodeBinary failed") return nil, response.ErrInvalidParams
} else { }
relayReason := s.coreServer.RelayTxn(tx) relayReason := s.coreServer.RelayTxn(tx)
switch relayReason { switch relayReason {
case network.RelaySucceed: case network.RelaySucceed:
results = true results = true
case network.RelayAlreadyExists: case network.RelayAlreadyExists:
err = errors.New("block or transaction already exists and cannot be sent repeatedly") resultsErr = response.ErrAlreadyExists
case network.RelayOutOfMemory: case network.RelayOutOfMemory:
err = errors.New("the memory pool is full and no more transactions can be sent") resultsErr = response.ErrOutOfMemory
case network.RelayUnableToVerify: case network.RelayUnableToVerify:
err = errors.New("the block cannot be validated") resultsErr = response.ErrUnableToVerify
case network.RelayInvalid: case network.RelayInvalid:
err = errors.New("block or transaction validation failed") resultsErr = response.ErrValidationFailed
case network.RelayPolicyFail: case network.RelayPolicyFail:
err = errors.New("one of the Policy filters failed") resultsErr = response.ErrPolicyFail
default: default:
err = errors.New("unknown error") resultsErr = response.ErrUnknown
}
}
if err != nil {
resultsErr = response.NewInternalServerError(err.Error(), err)
} }
} }