Skip to content

Commit

Permalink
fix error bubbling
Browse files Browse the repository at this point in the history
  • Loading branch information
tudor-malene committed Apr 12, 2024
1 parent 257675f commit 8c2867f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 34 deletions.
16 changes: 8 additions & 8 deletions go/common/errutil/evm_serialisable.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package errutil

// EVMSerialisableError is an API error that encompasses an EVM error with a code and a reason
type EVMSerialisableError struct {
Err string
Reason interface{}
Code int
// DataError is an API error that encompasses an EVM error with a code and a reason
type DataError struct {
Code int `json:"code"`
Err string `json:"message"`
Reason interface{} `json:"data,omitempty"`
}

func (e EVMSerialisableError) Error() string {
func (e DataError) Error() string {
return e.Err
}

func (e EVMSerialisableError) ErrorCode() int {
func (e DataError) ErrorCode() int {
return e.Code
}

func (e EVMSerialisableError) ErrorData() interface{} {
func (e DataError) ErrorData() interface{} {
return e.Reason
}
4 changes: 2 additions & 2 deletions go/enclave/evm/evm_facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func initParams(storage storage.Storage, gethEncodingService gethencoding.Encodi
}

func newErrorWithReasonAndCode(err error) error {
result := &errutil.EVMSerialisableError{
result := &errutil.DataError{
Err: err.Error(),
}

Expand All @@ -316,7 +316,7 @@ func newRevertError(result *gethcore.ExecutionResult) error {
if errUnpack == nil {
err = fmt.Errorf("execution reverted: %v", reason)
}
return &errutil.EVMSerialisableError{
return &errutil.DataError{
Err: err.Error(),
Reason: hexutil.Encode(result.Revert()),
Code: 3, // todo - magic number, really needs thought around the value and made a constant
Expand Down
2 changes: 1 addition & 1 deletion go/enclave/rpc/EstimateGas.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func EstimateGasExecute(builder *CallBuilder[CallParamsWithBlock, hexutil.Uint64
}

// return EVM error
builder.Err = convertError(err)
builder.Err = err
return nil
}

Expand Down
13 changes: 1 addition & 12 deletions go/enclave/rpc/TenEthCall.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ten-protocol/go-ten/go/common/errutil"
"github.com/ten-protocol/go-ten/go/common/gethencoding"
"github.com/ten-protocol/go-ten/go/common/log"
"github.com/ten-protocol/go-ten/go/common/syserr"
Expand Down Expand Up @@ -59,8 +58,7 @@ func TenCallExecute(builder *CallBuilder[CallParamsWithBlock, string], rpc *Encr
return err
}

// extract the EVM error
builder.Err = convertError(err)
builder.Err = err
return nil
}

Expand All @@ -73,12 +71,3 @@ func TenCallExecute(builder *CallBuilder[CallParamsWithBlock, string], rpc *Encr
}
return nil
}

func convertError(err error) *errutil.EVMSerialisableError {
// check if it's a serialized error and handle any error wrapping that might have occurred
var e *errutil.EVMSerialisableError
if ok := errors.As(err, &e); ok {
return e
}
return &errutil.EVMSerialisableError{Err: err.Error()}
}
19 changes: 15 additions & 4 deletions go/responses/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package responses

import (
"encoding/json"
"errors"
"fmt"

"github.com/ten-protocol/go-ten/go/common/errutil"

"github.com/ten-protocol/go-ten/go/common/syserr"
)

Expand Down Expand Up @@ -116,9 +119,8 @@ func AsEncryptedEmptyResponse(encryptHandler Encryptor) *EnclaveResponse {

// AsEncryptedError - Encodes and encrypts an error to be returned for a concrete user.
func AsEncryptedError(err error, encrypt Encryptor) *EnclaveResponse {
errStr := err.Error()
userResp := UserResponse[string]{
ErrStr: &errStr,
Err: convertError(err),
}

encoded, err := json.Marshal(userResp)
Expand Down Expand Up @@ -161,9 +163,18 @@ func DecodeResponse[T any](encoded []byte) (*T, error) {
if err != nil {
return nil, err
}
if resp.ErrStr != nil {
return nil, fmt.Errorf(*resp.ErrStr)
if resp.Err != nil {
return nil, resp.Err
}

return resp.Result, nil
}

func convertError(err error) *errutil.DataError {
// check if it's a serialized error and handle any error wrapping that might have occurred
var e *errutil.DataError
if ok := errors.As(err, &e); ok {
return e
}
return &errutil.DataError{Err: err.Error()}
}
10 changes: 3 additions & 7 deletions go/responses/types.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package responses

import (
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ten-protocol/go-ten/go/common/errutil"
)

type ViewingKeyEncryptor func([]byte) ([]byte, error)
Expand All @@ -14,15 +13,12 @@ type ViewingKeyEncryptor func([]byte) ([]byte, error)
// which will be decoded only on the client side.
type UserResponse[T any] struct {
Result *T
ErrStr *string
Err *errutil.DataError
}

// Error - converts the encoded string in the response into a normal error and returns it.
func (ur *UserResponse[T]) Error() error {
if ur.ErrStr != nil {
return fmt.Errorf(*ur.ErrStr)
}
return nil
return ur.Err
}

// Responses
Expand Down

0 comments on commit 8c2867f

Please sign in to comment.