]> git.proxmox.com Git - pmg-api.git/commitdiff
reinject mail: improve error logging
authorStoiko Ivanov <s.ivanov@proxmox.com>
Fri, 17 Mar 2023 18:44:52 +0000 (19:44 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 23 Mar 2023 16:25:27 +0000 (17:25 +0100)
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@proxmox.com>
Reviewed-by: Dominik Csapak <d.csapak@proxmox.com>
Tested-by:  Dominik Csapak <d.csapak@proxmox.com>
src/PMG/Utils.pm

index 37e76aa4407d6e8554cd5ee0ff2a07723beb99d6..6405934b63d9bfd83a9b4af1ae1e9ea56955ced9 100644 (file)
@@ -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 = $@;