From 385825d010b7616244b0f42fb0222651b5997cb6 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 19 Aug 2020 23:35:12 +0200 Subject: [PATCH] Improve registration error messages - Don't print the raw IRC message, since we already show the original error message - Avoid double-printing "registration failed" --- upstream.go | 17 ++++++++++++++--- user.go | 8 ++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/upstream.go b/upstream.go index 29613af..4ec704d 100644 --- a/upstream.go +++ b/upstream.go @@ -32,6 +32,12 @@ var permanentUpstreamCaps = map[string]bool{ "server-time": true, } +type registrationError string + +func (err registrationError) Error() string { + return fmt.Sprintf("registration error: %v", string(err)) +} + type upstreamChannel struct { Name string conn *upstreamConn @@ -1357,7 +1363,8 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error { return fmt.Errorf("fatal server error: %v", text) case irc.ERR_PASSWDMISMATCH, irc.ERR_ERRONEUSNICKNAME, irc.ERR_NICKNAMEINUSE, irc.ERR_NICKCOLLISION, irc.ERR_UNAVAILRESOURCE, irc.ERR_NOPERMFORHOST, irc.ERR_YOUREBANNEDCREEP: if !uc.registered { - return fmt.Errorf("registration failed: %v", msg.Params[len(msg.Params)-1]) + text := msg.Params[len(msg.Params)-1] + return registrationError(text) } fallthrough default: @@ -1526,8 +1533,12 @@ func (uc *upstreamConn) runUntilRegistered() error { } if err := uc.handleMessage(msg); err != nil { - msg.Tags = nil // prevent message tags from cluttering logs - return fmt.Errorf("failed to handle message %q: %v", msg, err) + if _, ok := err.(registrationError); ok { + return err + } else { + msg.Tags = nil // prevent message tags from cluttering logs + return fmt.Errorf("failed to handle message %q: %v", msg, err) + } } } diff --git a/user.go b/user.go index 6b9512a..1febc43 100644 --- a/user.go +++ b/user.go @@ -140,8 +140,12 @@ func (net *network) run() { uc.register() if err := uc.runUntilRegistered(); err != nil { - uc.logger.Printf("failed to register: %v", err) - net.user.events <- eventUpstreamConnectionError{net, fmt.Errorf("failed to register: %v", err)} + text := err.Error() + if regErr, ok := err.(registrationError); ok { + text = string(regErr) + } + uc.logger.Printf("failed to register: %v", text) + net.user.events <- eventUpstreamConnectionError{net, fmt.Errorf("failed to register: %v", text)} uc.Close() continue }