Skip to content

Commit

Permalink
Improve the error handing in the view web listener
Browse files Browse the repository at this point in the history
  • Loading branch information
zdrgeo committed Jan 2, 2025
1 parent df6119b commit 5cdd2c7
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions pkg/view/listenwebbrowserhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ func (handler *fileChangeHandler) Handle(writer http.ResponseWriter, request *ht
connection, err := websocket.Accept(writer, request, nil)

if err != nil {
log.Panic(err)
http.Error(writer, "Internal Server Error", http.StatusInternalServerError)

return
}

defer connection.CloseNow()
Expand All @@ -82,7 +84,9 @@ func (handler *fileChangeHandler) Handle(writer http.ResponseWriter, request *ht
oldStat, err := os.Stat(handler.fileName)

if err != nil {
log.Panic(err)
http.Error(writer, "Internal Server Error", http.StatusInternalServerError)

return
}

ticker := time.NewTicker(5 * time.Second)
Expand All @@ -95,21 +99,28 @@ func (handler *fileChangeHandler) Handle(writer http.ResponseWriter, request *ht
err = connection.Close(websocket.StatusNormalClosure, "")

if err != nil && websocket.CloseStatus(err) != websocket.StatusNormalClosure && websocket.CloseStatus(err) != websocket.StatusGoingAway {
log.Panic(err)
http.Error(writer, "Internal Server Error", http.StatusInternalServerError)

return
}

return
case <-ticker.C:
newStat, err := os.Stat(handler.fileName)

if err != nil {
log.Panic(err)
http.Error(writer, "Internal Server Error", http.StatusInternalServerError)

return
}

if oldStat.ModTime() != newStat.ModTime() {
err := connection.Close(websocket.StatusNormalClosure, "changed")

if err != nil && websocket.CloseStatus(err) != websocket.StatusNormalClosure && websocket.CloseStatus(err) != websocket.StatusGoingAway {
log.Panic(err)
http.Error(writer, "Internal Server Error", http.StatusInternalServerError)

return
}

return
Expand Down

0 comments on commit 5cdd2c7

Please sign in to comment.