Skip to content

Commit

Permalink
Generalize errors: string,omitempty always kicks in
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic committed Aug 20, 2024
1 parent 323c21f commit c74de5d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
16 changes: 7 additions & 9 deletions cmd/soroban-rpc/internal/methods/simulate_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
12 changes: 8 additions & 4 deletions cmd/soroban-rpc/internal/xdr2json/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,32 @@ 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.
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)
Expand Down

0 comments on commit c74de5d

Please sign in to comment.