From 6bbe00f0c7aae6c468b1a3e12983a74a170e92b8 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 8 Dec 2024 10:32:29 +0800 Subject: Handle send failures --- main.go | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 96b9b02..bd96e5d 100644 --- a/main.go +++ b/main.go @@ -65,28 +65,44 @@ messageLoop: case ErrEmptyMessage: continue messageLoop case ErrIllegalByte: - client.Send(MakeMsg(self, "ERROR", err.Error())) - break messageLoop + err := client.Send(MakeMsg(self, "ERROR", err.Error())) + if err != nil { + slog.Error("error while reporting illegal byte", "error", err, "client", client) + return + } + return case ErrTagsTooLong: fallthrough case ErrBodyTooLong: - client.Send(MakeMsg(self, ERR_INPUTTOOLONG, err.Error())) + err := client.Send(MakeMsg(self, ERR_INPUTTOOLONG, err.Error())) + if err != nil { + slog.Error("error while reporting body too long", "error", err, "client", client) + return + } continue messageLoop default: - client.Send(MakeMsg(self, "ERROR", err.Error())) - break messageLoop + err := client.Send(MakeMsg(self, "ERROR", err.Error())) + if err != nil { + slog.Error("error while reporting parser error", "error", err, "client", client) + } + return } } handler, ok := commandHandlers[msg.Command] if !ok { - client.Send(MakeMsg(self, ERR_UNKNOWNCOMMAND, msg.Command, "Unknown command")) + err := client.Send(MakeMsg(self, ERR_UNKNOWNCOMMAND, msg.Command, "Unknown command")) + if err != nil { + slog.Error("error while reporting unknown command", "error", err, "client", client) + return + } continue } - cont := handler(msg, client) - if !cont { - break + err = handler(msg, client) + if err != nil { + slog.Error("handler error", "error", err, "client", client) + return } } } -- cgit v1.2.3