From: oysheng <33340252+oysheng@users.noreply.github.com> Date: Sat, 9 Jun 2018 10:25:18 +0000 (+0800) Subject: modify error response (#1039) X-Git-Tag: v1.0.5~11^2~1 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=61cdf13383000baae15ba81f598d0618d8c12d71;p=bytom%2Fbytom.git modify error response (#1039) * modify error response * optimise --- diff --git a/api/api.go b/api/api.go index c919c21d..243d7f9e 100644 --- a/api/api.go +++ b/api/api.go @@ -43,9 +43,11 @@ const ( // Response describes the response standard. type Response struct { - Status string `json:"status,omitempty"` - Msg string `json:"msg,omitempty"` - Data interface{} `json:"data,omitempty"` + Status string `json:"status,omitempty"` + Code string `json:"code,omitempty"` + Msg string `json:"msg,omitempty"` + ErrorDetail string `json:"error_detail,omitempty"` + Data interface{} `json:"data,omitempty"` } //NewSuccessResponse success response @@ -55,7 +57,20 @@ func NewSuccessResponse(data interface{}) Response { //NewErrorResponse error response func NewErrorResponse(err error) Response { - return Response{Status: FAIL, Msg: err.Error()} + root := errors.Root(err) + if info, ok := respErrFormatter[root]; ok { + return Response{ + Status: FAIL, + Code: info.ChainCode, + Msg: info.Message, + ErrorDetail: errors.Detail(err), + } + } + return Response{ + Status: FAIL, + Msg: errors.Detail(err), + ErrorDetail: errors.Detail(err), + } } type waitHandler struct { diff --git a/api/errors.go b/api/errors.go index 9d617a1c..d18083e6 100644 --- a/api/errors.go +++ b/api/errors.go @@ -3,9 +3,7 @@ package api import ( "context" - "github.com/bytom/account" - "github.com/bytom/blockchain/query" - "github.com/bytom/blockchain/query/filter" + "github.com/bytom/blockchain/pseudohsm" "github.com/bytom/blockchain/rpc" "github.com/bytom/blockchain/signers" "github.com/bytom/blockchain/txbuilder" @@ -35,6 +33,26 @@ func isTemporary(info httperror.Info, err error) bool { } } +var respErrFormatter = map[error]httperror.Info{ + // Signers error namespace (2xx) + signers.ErrBadQuorum: {400, "BTM200", "Quorum must be greater than 1 and less than or equal to the length of xpubs"}, + signers.ErrBadXPub: {400, "BTM201", "Invalid xpub format"}, + signers.ErrNoXPubs: {400, "BTM202", "At least one xpub is required"}, + signers.ErrBadType: {400, "BTM203", "Retrieved type does not match expected type"}, + signers.ErrDupeXPub: {400, "BTM204", "Root XPubs cannot contain the same key more than once"}, + + // Transaction error namespace (7xx) + // Build error namespace (70x) + txbuilder.ErrBadAmount: {400, "BTM704", "Invalid asset amount"}, + + //Error code 050 represents alias of key duplicated + pseudohsm.ErrDuplicateKeyAlias: {400, "BTM050", "Alias already exists"}, + //Error code 801 represents query request format error + pseudohsm.ErrInvalidAfter: httperror.Info{400, "BTM801", "Invalid `after` in query"}, + //Error code 802 represents query reponses too many + pseudohsm.ErrTooManyAliasesToList: {400, "BTM802", "Too many aliases to list"}, +} + // Map error values to standard bytom error codes. Missing entries // will map to internalErrInfo. // @@ -51,39 +69,6 @@ var errorFormatter = httperror.Formatter{ rpc.ErrWrongNetwork: {502, "BTM104", "A peer core is operating on a different blockchain network"}, protocol.ErrTheDistantFuture: {400, "BTM105", "Requested height is too far ahead"}, - // Signers error namespace (2xx) - signers.ErrBadQuorum: {400, "BTM200", "Quorum must be greater than 1 and less than or equal to the length of xpubs"}, - signers.ErrBadXPub: {400, "BTM201", "Invalid xpub format"}, - signers.ErrNoXPubs: {400, "BTM202", "At least one xpub is required"}, - signers.ErrBadType: {400, "BTM203", "Retrieved type does not match expected type"}, - signers.ErrDupeXPub: {400, "BTM204", "Root XPubs cannot contain the same key more than once"}, - - // Query error namespace (6xx) - query.ErrBadAfter: {400, "BTM600", "Malformed pagination parameter `after`"}, - query.ErrParameterCountMismatch: {400, "BTM601", "Incorrect number of parameters to filter"}, - filter.ErrBadFilter: {400, "BTM602", "Malformed query filter"}, - - // Transaction error namespace (7xx) - // Build error namespace (70x) - txbuilder.ErrBadRefData: {400, "BTM700", "Reference data does not match previous transaction's reference data"}, - txbuilder.ErrBadAmount: {400, "BTM704", "Invalid asset amount"}, - txbuilder.ErrBlankCheck: {400, "BTM705", "Unsafe transaction: leaves assets to be taken without requiring payment"}, - txbuilder.ErrAction: {400, "BTM706", "One or more actions had an error: see attached data"}, - - // Submit error namespace (73x) - txbuilder.ErrMissingRawTx: {400, "BTM730", "Missing raw transaction"}, - txbuilder.ErrBadInstructionCount: {400, "BTM731", "Too many signing instructions in template for transaction"}, - txbuilder.ErrBadTxInputIdx: {400, "BTM732", "Invalid transaction input index"}, - txbuilder.ErrBadWitnessComponent: {400, "BTM733", "Invalid witness component"}, - txbuilder.ErrRejected: {400, "BTM735", "Transaction rejected"}, - txbuilder.ErrNoTxSighashCommitment: {400, "BTM736", "Transaction is not final, additional actions still allowed"}, - txbuilder.ErrTxSignatureFailure: {400, "BTM737", "Transaction signature missing, client may be missing signature key"}, - txbuilder.ErrNoTxSighashAttempt: {400, "BTM738", "Transaction signature was not attempted"}, - - // account action error namespace (76x) - account.ErrInsufficient: {400, "BTM760", "Insufficient funds for tx"}, - account.ErrReserved: {400, "BTM761", "Some outputs are reserved; try again"}, - //accesstoken authz err namespace (86x) errNotAuthenticated: {401, "BTM860", "Request could not be authenticated"}, }, diff --git a/api/hsm.go b/api/hsm.go index 4a9b6d63..7e476763 100644 --- a/api/hsm.go +++ b/api/hsm.go @@ -5,21 +5,10 @@ import ( log "github.com/sirupsen/logrus" - "github.com/bytom/blockchain/pseudohsm" "github.com/bytom/blockchain/txbuilder" "github.com/bytom/crypto/ed25519/chainkd" - "github.com/bytom/net/http/httperror" ) -func init() { - //Error code 050 represents alias of key duplicated - errorFormatter.Errors[pseudohsm.ErrDuplicateKeyAlias] = httperror.Info{400, "BTM050", "Alias already exists"} - //Error code 801 represents query request format error - errorFormatter.Errors[pseudohsm.ErrInvalidAfter] = httperror.Info{400, "BTM801", "Invalid `after` in query"} - //Error code 802 represents query reponses too many - errorFormatter.Errors[pseudohsm.ErrTooManyAliasesToList] = httperror.Info{400, "BTM802", "Too many aliases to list"} -} - func (a *API) pseudohsmCreateKey(ctx context.Context, in struct { Alias string `json:"alias"` Password string `json:"password"` diff --git a/errors/errors.go b/errors/errors.go index 0144287d..26e137d9 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -113,7 +113,10 @@ func WithDetailf(err error, format string, v ...interface{}) error { // An error has a detail message if it was made by WithDetail // or WithDetailf. func Detail(err error) string { - wrapper, _ := err.(wrapperError) + wrapper, ok := err.(wrapperError) + if !ok { + return err.Error() + } return strings.Join(wrapper.detail, "; ") } diff --git a/errors/errors_test.go b/errors/errors_test.go index 2f1b6572..88cf374e 100644 --- a/errors/errors_test.go +++ b/errors/errors_test.go @@ -78,7 +78,7 @@ func TestDetail(t *testing.T) { detail string message string }{ - {root, "", "foo"}, + {root, "foo", "foo"}, {WithDetail(root, "bar"), "bar", "bar: foo"}, {WithDetail(WithDetail(root, "bar"), "baz"), "bar; baz", "baz: bar: foo"}, {Wrap(WithDetail(root, "bar"), "baz"), "bar", "baz: bar: foo"},