diff --git a/codec/abi_decoder.go b/codec/abi_decoder.go index e89326ad..ccca6991 100644 --- a/codec/abi_decoder.go +++ b/codec/abi_decoder.go @@ -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) diff --git a/eosws/marshal_test.go b/eosws/marshal_test.go new file mode 100644 index 00000000..5aa23db7 --- /dev/null +++ b/eosws/marshal_test.go @@ -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) +} diff --git a/eosws/wsconn.go b/eosws/wsconn.go index 3f3983ff..4c751293 100644 --- a/eosws/wsconn.go +++ b/eosws/wsconn.go @@ -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)