From 3d79f3b5a74af073dabc46463658f444f1f2916a Mon Sep 17 00:00:00 2001 From: Kostas Drakontidis Date: Sat, 26 Jul 2025 00:54:59 +0300 Subject: [PATCH] Updated error handling & error messages --- client/backend/peer.go | 28 ++++++++++++++++++++-------- client/backend/types.go | 2 -- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/client/backend/peer.go b/client/backend/peer.go index f7c22c3..30eb145 100644 --- a/client/backend/peer.go +++ b/client/backend/peer.go @@ -261,6 +261,7 @@ func peerInit(id *uuid.UUID, p2p **webrtc.PeerConnection, offer *webrtc.SessionD (*p2p).OnICECandidate(func(candidate *webrtc.ICECandidate) { // Checking if ICE gathering has finished if candidate != nil { + fmt.Print(config_t.Messages.WEBRTC.ON_CANDIDATE) // Constructing candidate, encoding to JSON and sending to websocket candidate_t := Candidate{ Type: "candidate", @@ -395,11 +396,22 @@ func answer(id *uuid.UUID, p2p **webrtc.PeerConnection, offer *webrtc.SessionDes func candidate(id *uuid.UUID, p2p **webrtc.PeerConnection, raw []byte) { // Decoding JSON with candidate in it var candidate_t Candidate - json.Unmarshal(raw, &candidate_t) + message_unmarshal_error := json.Unmarshal(raw, &candidate_t) + if message_unmarshal_error != nil { + fmt.Fprintf(os.Stderr, config_t.Messages.JSON.UNMARSHAL.ERROR, message_unmarshal_error) + os.Exit(-1) + } else { + fmt.Print(config_t.Messages.JSON.UNMARSHAL.SUCCESS) + } // Checking if id matches and adding candidate if *id == candidate_t.Id { - (*p2p).AddICECandidate(candidate_t.Candidate) + add_ice_error := (*p2p).AddICECandidate(candidate_t.Candidate) + if add_ice_error != nil { + fmt.Fprint(os.Stderr, config_t.Messages.WEBRTC.ADD_CANDIDATE.ERROR, add_ice_error) + } else { + fmt.Print(config_t.Messages.WEBRTC.ADD_CANDIDATE.SUCCESS) + } } } @@ -408,14 +420,14 @@ func main() { raw_config, config_error := os.ReadFile("config.yml") if config_error != nil { fmt.Fprintf(os.Stderr, "Could not read config file.\n%v\n", config_error) - return + os.Exit(-1) } // Loading config file to config struct config_error = yaml.Unmarshal(raw_config, &config_t) if config_error != nil { fmt.Fprintf(os.Stderr, "Could not load config file.\n%v\n", config_error) - return + os.Exit(-1) } // Starting program @@ -428,7 +440,7 @@ func main() { if len(os.Args) != 2 { fmt.Fprint(os.Stderr, config_t.Messages.MISSING_PORT) - return + os.Exit(-1) } else { http_port = os.Args[1] } @@ -437,7 +449,7 @@ func main() { conn, _, dial_error := websocket.DefaultDialer.Dial(config_t.Config.SIGNALING_SERVER, nil) if dial_error != nil { fmt.Fprintf(os.Stderr, config_t.Messages.WS.CONNECT.ERROR, dial_error) - return + os.Exit(-1) } else { fmt.Print(config_t.Messages.WS.CONNECT.SUCCESS) } @@ -483,10 +495,10 @@ func main() { // Registering to the signaling server as normal user ws_write_error := conn.WriteMessage(websocket.TextMessage, register_msg) if ws_write_error != nil { - fmt.Fprintf(os.Stderr, config_t.Messages.WS.WRITE.ERROR, ws_write_error) + fmt.Fprintf(os.Stderr, config_t.Messages.WS.REGISTER.ERROR, ws_write_error) return } else { - fmt.Print(config_t.Messages.WS.WRITE.SUCCESS) + fmt.Print(config_t.Messages.WS.REGISTER.SUCCESS) } // WebSocket loop for incomming messages diff --git a/client/backend/types.go b/client/backend/types.go index e884a82..c300f9a 100644 --- a/client/backend/types.go +++ b/client/backend/types.go @@ -115,7 +115,5 @@ type Config struct { ENCODE Message `yaml:"ENCODE"` DECODE Message `yaml:"DECODE"` } `yaml:"GOB"` - WS_CONNECT_ERROR string `yaml:"WS_CONNECT_ERROR"` - WS_CLOSE_ERROR string `yaml:"WS_CLOSE_ERROR"` } `yaml:"messages"` }