From dfc7eb3882f06680a942551912671da8edb19a04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Fri, 19 May 2023 17:19:38 +0200 Subject: [PATCH] Fix noisy errors (#49) * send message error. * read message error. * close webrtc conn only if not closed yet. * file upload no files received. --- internal/api/room/upload.go | 12 +++++------- internal/webrtc/peer.go | 8 +++++++- internal/websocket/manager.go | 14 +++++++++++--- internal/websocket/peer.go | 4 +++- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/internal/api/room/upload.go b/internal/api/room/upload.go index bba7b989..046c6d94 100644 --- a/internal/api/room/upload.go +++ b/internal/api/room/upload.go @@ -95,15 +95,13 @@ func (h *RoomHandler) uploadDialogPost(w http.ResponseWriter, r *http.Request) e //nolint defer r.MultipartForm.RemoveAll() - if !h.desktop.IsFileChooserDialogOpened() { - return utils.HttpUnprocessableEntity("file chooser dialog is not open") - } - req_files := r.MultipartForm.File["files"] if len(req_files) == 0 { - return utils.HttpInternalServerError(). - WithInternalErr(err). - WithInternalMsg("unable to copy uploaded file to destination file") + return utils.HttpBadRequest("no files received") + } + + if !h.desktop.IsFileChooserDialogOpened() { + return utils.HttpUnprocessableEntity("file chooser dialog is not open") } dir, err := os.MkdirTemp("", "neko-dialog-*") diff --git a/internal/webrtc/peer.go b/internal/webrtc/peer.go index 590b598e..5d0ae2a9 100644 --- a/internal/webrtc/peer.go +++ b/internal/webrtc/peer.go @@ -109,7 +109,13 @@ func (peer *WebRTCPeerCtx) Destroy() { peer.mu.Lock() defer peer.mu.Unlock() - err := peer.connection.Close() + var err error + + // if peer connection is not closed, close it + if peer.connection.ConnectionState() != webrtc.PeerConnectionStateClosed { + err = peer.connection.Close() + } + peer.logger.Err(err).Msg("peer connection destroyed") } diff --git a/internal/websocket/manager.go b/internal/websocket/manager.go index 5ad9a0f8..03da86b2 100644 --- a/internal/websocket/manager.go +++ b/internal/websocket/manager.go @@ -2,6 +2,7 @@ package websocket import ( "encoding/json" + "errors" "net/http" "sync" "time" @@ -253,19 +254,26 @@ func (manager *WebSocketManagerCtx) connect(connection *websocket.Conn, r *http. Str("agent", r.UserAgent()). Msg("connection ended") + if err == nil { + logger.Debug().Msg("websocket close") + session.DisconnectWebSocketPeer(peer, false) + return + } + delayedDisconnect := false e, ok := err.(*websocket.CloseError) if !ok { - logger.Err(err).Msg("read message error") + err = errors.Unwrap(err) // unwrap if possible + logger.Warn().Err(err).Msg("read message error") // client is expected to reconnect soon delayedDisconnect = true } else { switch e.Code { case websocket.CloseNormalClosure: - logger.Info().Str("reason", e.Text).Msg("websocket close") + logger.Debug().Str("reason", e.Text).Msg("websocket close") case websocket.CloseGoingAway: - logger.Info().Str("reason", "going away").Msg("websocket close") + logger.Debug().Str("reason", "going away").Msg("websocket close") default: logger.Warn().Err(err).Msg("websocket close") // abnormal websocket closure: diff --git a/internal/websocket/peer.go b/internal/websocket/peer.go index 203caa83..8722acb6 100644 --- a/internal/websocket/peer.go +++ b/internal/websocket/peer.go @@ -2,6 +2,7 @@ package websocket import ( "encoding/json" + "errors" "sync" "github.com/gorilla/websocket" @@ -42,7 +43,8 @@ func (peer *WebSocketPeerCtx) Send(event string, payload any) { }) if err != nil { - peer.logger.Err(err).Str("event", event).Msg("send message error") + err = errors.Unwrap(err) // unwrap if possible + peer.logger.Warn().Err(err).Str("event", event).Msg("send message error") return }