From c74de5d5536d8b20162daf23ddeaf89c0c2b46bc Mon Sep 17 00:00:00 2001 From: George Date: Tue, 20 Aug 2024 10:53:26 -0700 Subject: [PATCH] Generalize errors: string,omitempty always kicks in --- .../internal/methods/simulate_transaction.go | 16 +++++++--------- cmd/soroban-rpc/internal/xdr2json/conversion.go | 12 ++++++++---- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/cmd/soroban-rpc/internal/methods/simulate_transaction.go b/cmd/soroban-rpc/internal/methods/simulate_transaction.go index 0fc318c2..2a26b08a 100644 --- a/cmd/soroban-rpc/internal/methods/simulate_transaction.go +++ b/cmd/soroban-rpc/internal/methods/simulate_transaction.go @@ -398,15 +398,13 @@ func NewSimulateTransactionHandler(logger *log.Entry, ledgerEntryReader db.Ledge switch request.Format { case FormatJSON: - if len(result.TransactionData) > 0 { - simResp.TransactionDataJSON, err = xdr2json.ConvertBytes( - xdr.SorobanTransactionData{}, - result.TransactionData) - if err != nil { - return SimulateTransactionResponse{ - Error: err.Error(), - LatestLedger: latestLedger, - } + simResp.TransactionDataJSON, err = xdr2json.ConvertBytes( + xdr.SorobanTransactionData{}, + result.TransactionData) + if err != nil { + return SimulateTransactionResponse{ + Error: err.Error(), + LatestLedger: latestLedger, } } diff --git a/cmd/soroban-rpc/internal/xdr2json/conversion.go b/cmd/soroban-rpc/internal/xdr2json/conversion.go index 828b3145..7992ab9b 100644 --- a/cmd/soroban-rpc/internal/xdr2json/conversion.go +++ b/cmd/soroban-rpc/internal/xdr2json/conversion.go @@ -27,20 +27,24 @@ import ( // and returns the raw JSON-formatted serialization of that object. // It can be unmarshalled to a proper JSON structure, but the raw bytes are // returned to avoid unnecessary round-trips. If there is an -// error, it returns an empty JSON object. +// error, it returns an empty string. // // The `xdr` object does not need to actually be initialized/valid: // we only use it to determine the name of the structure. We could just // accept a string, but that would make mistakes likelier than passing the // structure itself (by reference). -func ConvertBytes(xdr interface{}, field []byte) ([]byte, error) { +func ConvertBytes(xdr interface{}, field []byte) (json.RawMessage, error) { + if len(field) == 0 { + return []byte(""), nil + } + xdrTypeName := reflect.TypeOf(xdr).Name() return convertAnyBytes(xdrTypeName, field) } // ConvertInterface takes a valid XDR object (`xdr`) and returns // the raw JSON-formatted serialization of that object. If there is an -// error, it returns an empty JSON object. +// error, it returns an empty string. // // Unlike `ConvertBytes`, the value here needs to be valid and // serializable. @@ -48,7 +52,7 @@ func ConvertInterface(xdr encoding.BinaryMarshaler) (json.RawMessage, error) { xdrTypeName := reflect.TypeOf(xdr).Name() data, err := xdr.MarshalBinary() if err != nil { - return []byte("{}"), errors.Wrapf(err, "failed to serialize XDR type '%s'", xdrTypeName) + return []byte(""), errors.Wrapf(err, "failed to serialize XDR type '%s'", xdrTypeName) } return convertAnyBytes(xdrTypeName, data)