Fix Message Marshalling for nil data

This commit is contained in:
speatzle 2024-05-21 16:06:44 +02:00
parent 313257a1ab
commit 24ec424901
2 changed files with 6 additions and 2 deletions

View file

@ -10,13 +10,15 @@ import (
func respondError(ctx context.Context, c *websocket.Conn, id string, code int64, err error, data any) {
slog.ErrorContext(ctx, "Responding to Websocket Request With Error", "err", err, "id", id, "code", code, "data", data)
rData := []byte{}
var rData []byte
if data != nil {
rData, err = json.Marshal(data)
if err != nil {
slog.ErrorContext(ctx, "Error Marshalling Error Data", "err", err)
return
}
} else {
rData = []byte("\"\"")
}
raw := json.RawMessage(rData)

View file

@ -37,7 +37,7 @@ func (s *server) handleRequest(ctx context.Context, c *websocket.Conn, data []by
return
}
rData := json.RawMessage{}
var rData json.RawMessage
if data != nil {
rData, err = json.Marshal(result)
if err != nil {
@ -45,6 +45,8 @@ func (s *server) handleRequest(ctx context.Context, c *websocket.Conn, data []by
slog.ErrorContext(ctx, "Marshalling Response Data", "err", err)
return
}
} else {
rData = []byte("\"\"")
}
slog.InfoContext(ctx, "response data", "rdata", rData)