send-email: capture errors in an eval {} block

Auth relied solely on return values without catching errors. This misjudges non-credential errors as auth failure without error info. Patch wraps the entire auth process in an eval {} block to catch all exceptions, including non-credential errors. It adds a new $error var, uses 'or do' to prevent flow break, and returns $result ? 1 : 0. And merges if/else branches, integrates SASL and basic auth, with comments for future status code handling. Signed-off-by: Zheng Yuting <05ZYT30@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Zheng Yuting committed Mar 26, 2025 at 15:52 UTC ce20dec4a4de8a75cbc9735538c9430e68d3c1f8
1 file changed +27 -16
git-send-email.perl
+27 -16
@@ -1419,7 +1419,7 @@ sub smtp_auth_maybe {
1419 die "invalid smtp auth: '${smtp_auth}'";
1420 }
1421
1422 - # TODO: Authentication may fail not because credentials were
1422 + # Authentication may fail not because credentials were
1423 # invalid but due to other reasons, in which we should not
1424 # reject credentials.
1425 $auth = Git::credential({
@@ -1431,21 +1431,32 @@ sub smtp_auth_maybe {
1431 'password' => $smtp_authpass
1432 }, sub {
1433 my $cred = shift;
1434 -
1435 - if ($smtp_auth) {
1436 - my $sasl = Authen::SASL->new(
1437 - mechanism => $smtp_auth,
1438 - callback => {
1439 - user => $cred->{'username'},
1440 - pass => $cred->{'password'},
1441 - authname => $cred->{'username'},
1442 - }
1443 - );
1444 -
1445 - return !!$smtp->auth($sasl);
1446 - }
1447 -
1448 - return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1434 + my $result;
1435 + my $error;
1436 +
1437 + # catch all SMTP auth error in a unified eval block
1438 + eval {
1439 + if ($smtp_auth) {
1440 + my $sasl = Authen::SASL->new(
1441 + mechanism => $smtp_auth,
1442 + callback => {
1443 + user => $cred->{'username'},
1444 + pass => $cred->{'password'},
1445 + authname => $cred->{'username'},
1446 + }
1447 + );
1448 + $result = $smtp->auth($sasl);
1449 + } else {
1450 + $result = $smtp->auth($cred->{'username'}, $cred->{'password'});
1451 + }
1452 + 1; # ensure true value is returned if no exception is thrown
1453 + } or do {
1454 + $error = $@ || 'Unknown error';
1455 + };
1456 +
1457 + # NOTE: SMTP status code handling will be added in a subsequent commit,
1458 + # return 1 when failed due to non-credential reasons
1459 + return $error ? 1 : ($result ? 1 : 0);
1460 });
1461
1462 return $auth;