mirror of
https://github.com/hanzoai/team.git
synced 2026-09-30 01:57:35 +00:00
* fix(mmctl): prevent nil pointer panic in websocket command on connection failure When the WebSocket connection fails immediately, Listen() closes EventChannel via defer. Reading from a closed channel with a plain receive returns nil, causing a panic in ToJSON(). Switch to range so the loop exits cleanly, add a nil guard, and surface ListenError to the caller. Fixes MM-68351 * test(mmctl): add unit tests for websocket nil event and ListenError handling Extracts the event-processing loop into processWebSocketEvents to enable unit testing, and adds tests covering the nil-event skip and error surfacing. * test(mmctl): add happy-path subtest for processWebSocketEvents --------- Co-authored-by: Hanzo Dev <[email protected]>
57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package commands
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var WebsocketCmd = &cobra.Command{
|
|
Use: "websocket",
|
|
Short: "Display websocket in a human-readable format",
|
|
RunE: websocketCmdF,
|
|
}
|
|
|
|
func init() {
|
|
RootCmd.AddCommand(WebsocketCmd)
|
|
}
|
|
|
|
func websocketCmdF(cmd *cobra.Command, args []string) error {
|
|
c, err := InitWebSocketClient()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
appErr := c.Connect()
|
|
if appErr != nil {
|
|
return errors.New(appErr.Error())
|
|
}
|
|
|
|
c.Listen()
|
|
fmt.Fprintln(os.Stderr, "Press CTRL+C to exit")
|
|
return processWebSocketEvents(c)
|
|
}
|
|
|
|
func processWebSocketEvents(c *model.WebSocketClient) error {
|
|
for event := range c.EventChannel {
|
|
if event == nil {
|
|
continue
|
|
}
|
|
data, err := event.ToJSON()
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
} else {
|
|
fmt.Println(string(data))
|
|
}
|
|
}
|
|
if c.ListenError != nil {
|
|
return errors.New(c.ListenError.Error())
|
|
}
|
|
return nil
|
|
}
|