Skip to content

Commit

Permalink
api: Refactor transaction error
Browse files Browse the repository at this point in the history
  • Loading branch information
aefhm committed Mar 10, 2023
1 parent 57c02d4 commit 79b3e16
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
30 changes: 16 additions & 14 deletions api/spec/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1226,17 +1226,25 @@ components:
success:
type: boolean
description: Whether this transaction successfully executed.
error:
$ref: '#/components/schemas/TxError'
description: Error details of a failed transaction.
description: |
A consensus transaction.
TxError:
type: object
required: [code, module, message]
properties:
code:
type: integer
description: The status code of a failed call.
description: The status code of a failed transaction.
module:
type: string
description: The module of a failed call.
description: The module of a failed transaction.
message:
type: string
description: The message of a failed call.
description: |
A consensus transaction.
description: The message of a failed transaction.

ConsensusEventType:
type: string
Expand Down Expand Up @@ -2058,15 +2066,9 @@ components:
success:
type: boolean
description: Whether this transaction successfully executed.
code:
type: integer
description: The status code of a failed call.
module:
type: string
description: The module of a failed call.
message:
type: string
description: The message of a failed call.
error:
$ref: '#/components/schemas/TxError'
description: Error details of a failed transaction.
description: |
A runtime transaction.
Expand Down
9 changes: 5 additions & 4 deletions api/v1/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ func renderRuntimeTransaction(storageTransaction client.RuntimeTransaction) (api
Success: cr.IsSuccess(),
}
if !cr.IsSuccess() {
code := int(cr.Failed.Code)
apiTransaction.Code = &code
apiTransaction.Module = &cr.Failed.Module
apiTransaction.Message = &cr.Failed.Message
apiTransaction.Error = &apiTypes.TxError{
Code: int(cr.Failed.Code),
Module: cr.Failed.Module,
Message: cr.Failed.Message,
}
}
if err = uncategorized.VisitCall(&tx.Call, &cr, &uncategorized.CallHandler{
AccountsTransfer: func(body *accounts.Transfer) error {
Expand Down
24 changes: 16 additions & 8 deletions storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ func (c *StorageClient) Transactions(ctx context.Context, p apiTypes.GetConsensu
}
for res.rows.Next() {
var t Transaction
var code uint64
var module string
var message string
if err := res.rows.Scan(
Expand All @@ -272,18 +273,21 @@ func (c *StorageClient) Transactions(ctx context.Context, p apiTypes.GetConsensu
&t.Fee,
&t.Method,
&t.Body,
&t.Code,
&code,
&module,
&message,
&t.Timestamp,
); err != nil {
return nil, wrapError(err)
}
if *t.Code == oasisErrors.CodeNoError {
if code == oasisErrors.CodeNoError {
t.Success = true
} else {
t.Module = &module
t.Message = &message
t.Error = &apiTypes.TxError{
Code: int(code),
Module: module,
Message: message,
}
}

ts.Transactions = append(ts.Transactions, t)
Expand All @@ -301,6 +305,7 @@ func (c *StorageClient) Transaction(ctx context.Context, txHash string) (*Transa
}

var t Transaction
var code uint64
var module string
var message string
if err := c.db.QueryRow(
Expand All @@ -316,18 +321,21 @@ func (c *StorageClient) Transaction(ctx context.Context, txHash string) (*Transa
&t.Fee,
&t.Method,
&t.Body,
&t.Code,
&code,
&module,
&message,
&t.Timestamp,
); err != nil {
return nil, wrapError(err)
}
if *t.Code == oasisErrors.CodeNoError {
if code == oasisErrors.CodeNoError {
t.Success = true
} else {
t.Module = &module
t.Message = &message
t.Error = &apiTypes.TxError{
Code: int(code),
Module: module,
Message: message,
}
}

c.cacheTx(&t)
Expand Down

0 comments on commit 79b3e16

Please sign in to comment.