[pmg-devel] [PATCH pmg-api v2 3/4] reinject mail: improve error logging

Stoiko Ivanov s.ivanov at proxmox.com
Wed Mar 8 15:52:33 CET 2023


this patch unifies the error handling for mail and rcpt with
the data command: all now die with sensible error (which gets logged
in the error-handling of the eval), and it sets the respose message
and code for those commands as well.

additionally it adds a '\n' to all die statements.

this makes it possible to provide information what went wrong at
call-sites (instead of only having it in syslog)

Signed-off-by: Stoiko Ivanov <s.ivanov at proxmox.com>
---
 src/PMG/Utils.pm | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm
index 37e76aa..6405934 100644
--- a/src/PMG/Utils.pm
+++ b/src/PMG/Utils.pm
@@ -288,8 +288,10 @@ sub reinject_mail {
 	}
 
 	if (!$smtp->_MAIL("FROM:" . $sender_addr . $mail_opts)) {
-	    syslog('err', "smtp error - got: %s %s", $smtp->code, scalar ($smtp->message));
-	    die "smtp from: ERROR";
+	    my @msgs = $smtp->message;
+	    $resmess = $msgs[$#msgs];
+	    $rescode = $smtp->code;
+	    die sprintf("smtp from error - got: %s %s\n", $rescode, $resmess);
 	}
 
 	foreach my $target (@$targets) {
@@ -304,8 +306,10 @@ sub reinject_mail {
 	    $rcpt_addr = encode('UTF-8', $smtp->_addr($target));
 
 	    if (!$smtp->_RCPT("TO:" . $rcpt_addr . $rcpt_opts)) {
-		syslog ('err', "smtp error - got: %s %s", $smtp->code, scalar($smtp->message));
-		die "smtp to: ERROR";
+		my @msgs = $smtp->message;
+		$resmess = $msgs[$#msgs];
+		$rescode = $smtp->code;
+		die sprintf("smtp to error - got: %s %s\n", $rescode, $resmess);
 	    }
 	}
 
@@ -326,13 +330,13 @@ sub reinject_mail {
 	    ($resid) = $resmess =~ m/Ok: queued as ([0-9A-Z]+)/;
 	    $rescode = $smtp->code;
 	    if (!$resid) {
-		die sprintf("unexpected SMTP result - got: %s %s : WARNING", $smtp->code, $resmess);
+		die sprintf("unexpected SMTP result - got: %s %s : WARNING\n", $smtp->code, $resmess);
 	    }
 	} else {
 	    my @msgs = $smtp->message;
 	    $resmess = $msgs[$#msgs];
 	    $rescode = $smtp->code;
-	    die sprintf("sending data failed - got: %s %s : ERROR", $smtp->code, $resmess);
+	    die sprintf("sending data failed - got: %s %s : ERROR\n", $smtp->code, $resmess);
 	}
     };
     my $err = $@;
-- 
2.30.2





More information about the pmg-devel mailing list