From 2031d8d1031be2b293be9fd8c5a337c4ed21cf89 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Thu, 5 Mar 2020 20:04:05 +0300 Subject: [PATCH] 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 --- pkg/rpc/server/server.go | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index b7adfdac1..c174edc09 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -854,28 +854,24 @@ func (s *Server) sendrawtransaction(reqParams request.Params) (interface{}, erro tx := &transaction.Transaction{} tx.DecodeBinary(r) if r.Err != nil { - err = errors.Wrap(r.Err, "transaction DecodeBinary failed") - } else { - relayReason := s.coreServer.RelayTxn(tx) - switch relayReason { - case network.RelaySucceed: - results = true - case network.RelayAlreadyExists: - err = errors.New("block or transaction already exists and cannot be sent repeatedly") - case network.RelayOutOfMemory: - err = errors.New("the memory pool is full and no more transactions can be sent") - case network.RelayUnableToVerify: - err = errors.New("the block cannot be validated") - case network.RelayInvalid: - err = errors.New("block or transaction validation failed") - case network.RelayPolicyFail: - err = errors.New("one of the Policy filters failed") - default: - err = errors.New("unknown error") - } + return nil, response.ErrInvalidParams } - if err != nil { - resultsErr = response.NewInternalServerError(err.Error(), err) + relayReason := s.coreServer.RelayTxn(tx) + switch relayReason { + case network.RelaySucceed: + results = true + case network.RelayAlreadyExists: + resultsErr = response.ErrAlreadyExists + case network.RelayOutOfMemory: + resultsErr = response.ErrOutOfMemory + case network.RelayUnableToVerify: + resultsErr = response.ErrUnableToVerify + case network.RelayInvalid: + resultsErr = response.ErrValidationFailed + case network.RelayPolicyFail: + resultsErr = response.ErrPolicyFail + default: + resultsErr = response.ErrUnknown } }