start logging received json on decode error

This commit is contained in:
Alex Grintsvayg 2019-05-01 15:39:14 -04:00
parent 2cacdaf22d
commit fa2ec38d3e
No known key found for this signature in database
GPG key ID: AEB3F089F86A22B5
2 changed files with 27 additions and 1 deletions

View file

@ -6,6 +6,7 @@ import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"io" "io"
"io/ioutil"
"net" "net"
"strconv" "strconv"
"time" "time"
@ -355,7 +356,16 @@ func (s *Server) read(conn net.Conn, v interface{}) error {
return errors.Err(err) return errors.Err(err)
} }
return errors.Err(json.NewDecoder(conn).Decode(v)) dec := json.NewDecoder(conn)
err = dec.Decode(v)
if err != nil {
data, _ := ioutil.ReadAll(dec.Buffered())
if len(data) > 0 {
return errors.Err("%s. Data: %s", err.Error(), hex.EncodeToString(data))
}
return errors.Err(err)
}
return nil
} }
func (s *Server) readRawBlob(conn net.Conn, blobSize int) ([]byte, error) { func (s *Server) readRawBlob(conn net.Conn, blobSize int) ([]byte, error) {

View file

@ -144,6 +144,22 @@ func TestServer_Timeout(t *testing.T) {
} }
} }
//func TestServer_InvalidJSONHandshake(t *testing.T) {
// srv, port := startServerOnRandomPort(t)
// defer srv.Shutdown()
//
// c := Client{}
// err := c.Connect(":" + strconv.Itoa(port))
// if err != nil {
// t.Fatal("error connecting client to server", err)
// }
//
// _, err = c.conn.Write([]byte(`{"stuff":4,tf}"`))
// if err == nil {
// t.Error("expected an error")
// }
//}
type mockPartialStore struct { type mockPartialStore struct {
store.MemoryBlobStore store.MemoryBlobStore
missing []string missing []string