OSDN Git Service

modify error response (#1039)
authoroysheng <33340252+oysheng@users.noreply.github.com>
Sat, 9 Jun 2018 10:25:18 +0000 (18:25 +0800)
committerPaladz <yzhu101@uottawa.ca>
Sat, 9 Jun 2018 10:25:18 +0000 (18:25 +0800)
* modify error response

* optimise

api/api.go
api/errors.go
api/hsm.go
errors/errors.go
errors/errors_test.go

index c919c21..243d7f9 100644 (file)
@@ -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 {
index 9d617a1..d18083e 100644 (file)
@@ -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"},
        },
index 4a9b6d6..7e47676 100644 (file)
@@ -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"`
index 0144287..26e137d 100644 (file)
@@ -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, "; ")
 }
 
index 2f1b657..88cf374 100644 (file)
@@ -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"},