Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BLOCK-2310] Modified to catch panic from json.Marshal function #46

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading