Skip to content

Commit

Permalink
Improves handling connection errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Oct 23, 2023
1 parent a57c6ce commit b9670a0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 5 additions & 1 deletion pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ func (c *CLI) Run(outputFile *os.File) error {
continue
}

case msg := <-c.wsConn.Messages:
case msg, ok := <-c.wsConn.Messages:
if !ok {
return nil
}

output, err := c.formater.FormatMessage(msg)
if err != nil {
log.Printf("Fail to format message: %s, %s\n", err, msg.Data)
Expand Down
12 changes: 10 additions & 2 deletions pkg/ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package ws

import (
"crypto/tls"
"log"

"github.com/fatih/color"
"golang.org/x/net/websocket"
)

Expand Down Expand Up @@ -56,12 +56,20 @@ func NewWS(url string, opts Options) (*Connection, error) {
messages := make(chan Message, WSMessageBufferSize)

go func(messages chan Message) {
defer close(messages)

for {
var msg string

err = websocket.Message.Receive(ws, &msg)
if err != nil {
log.Fatal("Fail to read from WS connection:", err)
if err.Error() == "EOF" {
color.New(color.FgRed).Println("Connection closed by the server")
} else {
color.New(color.FgRed).Println("Fail read from connection: ", err)
}

return
}

messages <- Message{Type: Response, Data: msg}
Expand Down

0 comments on commit b9670a0

Please sign in to comment.