Merge pull request #1773 from nspcc-dev/detailed-rpc-error

rpc: detalize `submit*` RPC validation error
This commit is contained in:
Roman Khimov 2021-02-26 17:03:18 +03:00 committed by GitHub
commit 50e330a9c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 6 deletions

View file

@ -93,3 +93,9 @@ func NewSubmitError(code int64, message string) *Error {
func (e *Error) Error() string {
return fmt.Sprintf("%s (%d) - %s - %s", e.Message, e.Code, e.Data, e.Cause)
}
// WrapErrorWithData returns copy of the given error with specified data and cause.
// It does not modify the source error.
func WrapErrorWithData(e *Error, data error) *Error {
return NewError(e.Code, e.HTTPCode, e.Message, data.Error(), data)
}

View file

@ -1235,9 +1235,9 @@ func (s *Server) submitBlock(reqParams request.Params) (interface{}, *response.E
if err != nil {
switch {
case errors.Is(err, core.ErrInvalidBlockIndex) || errors.Is(err, core.ErrAlreadyExists):
return nil, response.ErrAlreadyExists
return nil, response.WrapErrorWithData(response.ErrAlreadyExists, err)
default:
return nil, response.ErrValidationFailed
return nil, response.WrapErrorWithData(response.ErrValidationFailed, err)
}
}
return &result.RelayResult{
@ -1273,13 +1273,13 @@ func getRelayResult(err error, hash util.Uint256) (interface{}, *response.Error)
Hash: hash,
}, nil
case errors.Is(err, core.ErrAlreadyExists):
return nil, response.ErrAlreadyExists
return nil, response.WrapErrorWithData(response.ErrAlreadyExists, err)
case errors.Is(err, core.ErrOOM):
return nil, response.ErrOutOfMemory
return nil, response.WrapErrorWithData(response.ErrOutOfMemory, err)
case errors.Is(err, core.ErrPolicy):
return nil, response.ErrPolicyFail
return nil, response.WrapErrorWithData(response.ErrPolicyFail, err)
default:
return nil, response.ErrValidationFailed
return nil, response.WrapErrorWithData(response.ErrValidationFailed, err)
}
}