From 4c920880da47f771ad695e980f5bae807c1ac4e9 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 13 May 2023 17:44:42 +0200 Subject: Rework error management Error handling was not consistent and quite bad to be honest ... --- gui.go | 2 +- limox.go | 4 ++-- xmpp.go | 45 ++++++++++++++++++++++++++++++++++----------- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/gui.go b/gui.go index 8e1f30d..e6e4c31 100644 --- a/gui.go +++ b/gui.go @@ -70,7 +70,7 @@ func (l *Limox) buttonLabel() string { case Connected: label = "Disconnect" default: - log.Fatalf("Unknown Limox state '%d'\n", l.State) + log.Printf("Unknown Limox state '%d'\n", l.State) } return label } diff --git a/limox.go b/limox.go index 98652cb..89a5916 100644 --- a/limox.go +++ b/limox.go @@ -72,10 +72,10 @@ func (l *Limox) run() error { case XmppConnect: l.State = Connected default: - log.Fatalf("Unknown XmppEvent '%d'\n", ev) + log.Printf("Unknown XmppEvent '%d'\n", ev) } default: - log.Fatalf("Unknown event type '%s'.\n", reflect.TypeOf(ev)) + log.Printf("Unknown event type '%s'.\n", reflect.TypeOf(ev)) } l.Window.Invalidate() } diff --git a/xmpp.go b/xmpp.go index d588f8d..ae38682 100644 --- a/xmpp.go +++ b/xmpp.go @@ -20,7 +20,7 @@ func xmpp(rxChan chan GuiEvent, txChan chan any, jid string, pwd string) { conn, err := setupConn(jid) if err != nil { - txChan <- err + log.Print(err) return } defer conn.Close() @@ -31,7 +31,9 @@ func xmpp(rxChan chan GuiEvent, txChan chan any, jid string, pwd string) { defer func() { termination <- true }() enc := xml.NewEncoder(conn) + defer enc.Close() dbg := xml.NewEncoder(os.Stdout) + defer dbg.Close() end := sendStreamStart(enc, dbg, jid) defer sendStreamEnd(enc, dbg, end) @@ -45,7 +47,7 @@ func xmpp(rxChan chan GuiEvent, txChan chan any, jid string, pwd string) { case Disconnect: return default: - log.Fatalf("Unknown GuiEvent '%d'!\n", ev) + log.Printf("Unknown GuiEvent '%d'!\n", ev) } case rx := <-receiver: dbg.Indent("S: ", " ") @@ -68,12 +70,16 @@ func setupConn(jid string) (*tls.Conn, error) { func rxRoutine(conn *tls.Conn, tokens chan xml.Token, terminator chan bool) { dec := xml.NewDecoder(conn) + for { select { case <-terminator: return default: - t, _ := dec.Token() // TODO error handling + t, err := dec.Token() + if err != nil { + log.Print(err) + } if t != nil { tokens <- t } @@ -95,35 +101,52 @@ func sendStreamStart(enc *xml.Encoder, dbg *xml.Encoder, jid string) xml.EndElem err := enc.EncodeToken(start) if err != nil { - log.Fatal("Could not encode stream start!") + log.Println("Could not encode stream start to TCP channel!") } err = enc.Flush() if err != nil { - log.Fatal("Could not flush XML encoder!") + log.Println("Could not flush TCP channel XML encoder!") } dbg.Indent("C: ", " ") - dbg.EncodeToken(start) - dbg.Flush() + + err = dbg.EncodeToken(start) + if err != nil { + log.Println("Could not encode stream start to debug output!") + } + + err = dbg.Flush() + if err != nil { + log.Println("Could not flush debug XML encoder!") + } return start.End() } func sendStreamEnd(enc *xml.Encoder, dbg *xml.Encoder, end xml.EndElement) { + err := enc.EncodeToken(end) if err != nil { - log.Fatal("Could not encode stream end!") + log.Println("Could not encode stream end to TCP channel!") } err = enc.Flush() if err != nil { - log.Fatal("Could not flush XML encoder!") + log.Println("Could not flush TCP channel XML encoder!") } dbg.Indent("C: ", " ") - dbg.EncodeToken(end) - dbg.Flush() + + err = dbg.EncodeToken(end) + if err != nil { + log.Println("Could not encode stream end to debug output!") + } + + err = dbg.Flush() + if err != nil { + log.Println("Could not flush debug XML encoder!") + } } // domainpart extracts the domain name from a JID / XMPP address. See -- cgit v1.2.3-70-g09d2