Skip to content

Commit

Permalink
[BLOCK-2310] Modified to catch panic from json.Marshal function (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
keisuke-kanao authored May 27, 2024
1 parent c2eae20 commit 48dd273
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion codec/abi_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ type InjectDataWrite struct {
InjectData
}

//ultra-andrey-bezrukov --- BLOCK-178 Dfuse cannot produce JSON data for migration
// ultra-andrey-bezrukov --- BLOCK-178 Dfuse cannot produce JSON data for migration
func DecodeTableInject(data []byte, abi *eos.ABI) ([]byte, error) {
dataRd := InjectDataRead{}
err := json.Unmarshal(data, &dataRd)
Expand Down
34 changes: 34 additions & 0 deletions eosws/marshal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package eosws

import (
"encoding/json"
"fmt"
"testing"

"github.com/dfuse-io/dfuse-eosio/eosws/wsmsg"
"github.com/stretchr/testify/assert"
)

// TestMarshalOutgoingMessager is a code snippet extracted from eosws.(*WSConn).Emit() function.
// Here, SetReqID() is called to set an invalid string, because another function for OutgoingMessager interface, SetType() is called
// and the result is validated in Emit().
func TestMarshalOutgoingMessager(t *testing.T) {
var err error
defer func() {
if r := recover(); r != nil {
switch v := r.(type) {
case error:
err = fmt.Errorf("unexpected error marshalling message: %w", v)
case string, fmt.Stringer:
err = fmt.Errorf("unexpected error marshalling message: %s", v)
default:
err = fmt.Errorf("unexpected error marshalling message: %v", v)
}
fmt.Printf("%s\n", err)
assert.Error(t, err)
}
}()
var msg wsmsg.OutgoingMessager
msg.SetReqID("")
_, err = json.Marshal(msg)
}
15 changes: 15 additions & 0 deletions eosws/wsconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,21 @@ func (ws *WSConn) handleMessage(rawMsg []byte) {
}

func (ws *WSConn) Emit(ctx context.Context, msg wsmsg.OutgoingMessager) {
var err error
defer func() {
if r := recover(); r != nil {
switch v := r.(type) {
case error:
err = fmt.Errorf("unexpected error marshalling message: %w", v)
case string, fmt.Stringer:
err = fmt.Errorf("unexpected error marshalling message: %s", v)
default:
err = fmt.Errorf("unexpected error marshalling message: %v", v)
}
ws.Shutdown(err)
}
}()

zlogger := logging.Logger(ctx, zlog)

msgType, err := wsmsg.GetType(msg)
Expand Down

0 comments on commit 48dd273

Please sign in to comment.