git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Zheng Yuting <05zyt30@gmail.com>
To: git@vger.kernel.org
Cc: 05zyt30@gmail.com, meetsoni3017@gmail.com, gitster@pobox.com,
	Zheng Yuting <05ZYT30@gmail.com>
Subject: [GSoC PATCH v6 1/2] send-email: capture errors in an eval {} block
Date: Fri, 21 Mar 2025 10:51:27 +0800	[thread overview]
Message-ID: <20250321025128.68463-2-05ZYT30@gmail.com> (raw)
In-Reply-To: <20250321025128.68463-1-05ZYT30@gmail.com>

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>
---
 git-send-email.perl | 45 ++++++++++++++++++++++++++-------------------
 1 file changed, 26 insertions(+), 19 deletions(-)

diff --git a/git-send-email.perl b/git-send-email.perl
index 798d59b84f..8feb43e9f7 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -1419,7 +1419,7 @@ sub smtp_auth_maybe {
 		die "invalid smtp auth: '${smtp_auth}'";
 	}

-	# TODO: Authentication may fail not because credentials were
+	# Authentication may fail not because credentials were
 	# invalid but due to other reasons, in which we should not
 	# reject credentials.
 	$auth = Git::credential({
@@ -1431,25 +1431,32 @@ sub smtp_auth_maybe {
 		'password' => $smtp_authpass
 	}, sub {
 		my $cred = shift;
+		my $result;
+		my $error;
+
+		# catch all SMTP auth error in a unified eval block
+		eval {
+			if ($smtp_auth) {
+				my $sasl = Authen::SASL->new(
+					mechanism => $smtp_auth,
+					callback => {
+						user     => $cred->{'username'},
+						pass     => $cred->{'password'},
+						authname => $cred->{'username'},
+					}
+				);
+				$result = $smtp->auth($sasl);
+			} else {
+				$result = $smtp->auth($cred->{'username'}, $cred->{'password'});
+			}
+			1; # ensure true value is returned if no exception is thrown
+		} or do {
+			$error = $@ || 'Unknown error';
+		};

-		if ($smtp_auth) {
-			my $sasl = Authen::SASL->new(
-				mechanism => $smtp_auth,
-				callback => {
-					user => $cred->{'username'},
-					pass => $cred->{'password'},
-					authname => $cred->{'username'},
-				}
-			);
-
-			return !!$smtp->auth($sasl);
-		}
-
-		return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
-	});
-
-	return $auth;
-}
+		# NOTE: SMTP status code handling will be added in a subsequent commit
+		return $result ? 1 : 0;
+	}

 sub ssl_verify_params {
 	eval {
--
2.48.1

  reply	other threads:[~2025-03-21  2:51 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-12  6:46 [GSoC PATCH v3 0/1] Refactor SMTP Auth Error Handling Zheng Yuting
2025-03-12  6:46 ` [GSoC PATCH v3 1/1] Unify SMTP auth error handling Zheng Yuting
2025-03-13 19:58   ` Junio C Hamano
2025-03-14 12:55     ` Yuting Zheng
2025-03-16  5:09     ` [GSoC PATCH v4 0/2] smtp_auth_maybe: unified error capture and status code processing optimization Zheng Yuting
2025-03-16  5:09       ` [GSoC PATCH v4 1/2] Unify capture of SMTP errors Zheng Yuting
2025-03-16  5:09       ` [GSoC PATCH v4 2/2] Error handling for SMTP status codes Zheng Yuting
2025-03-17 23:01       ` [GSoC PATCH v4 0/2] smtp_auth_maybe: unified error capture and status code processing optimization Junio C Hamano
2025-03-19  2:02       ` [GSoC PATCH v5 0/2] sendemail: improve error capture and status code handling Zheng Yuting
2025-03-19  2:02         ` [GSoC PATCH v5 1/2] sendemail: capture errors in an eval {} block Zheng Yuting
2025-03-19  2:02         ` [GSoC PATCH v5 2/2] sendemail: finer-grained SMTP error handling Zheng Yuting
2025-03-19  6:35         ` [GSoC PATCH v5 0/2] sendemail: improve error capture and status code handling Meet Soni
2025-03-21  2:51         ` [GSoC PATCH v6 0/2] send-email: " Zheng Yuting
2025-03-21  2:51           ` Zheng Yuting [this message]
2025-03-21  2:51           ` [GSoC PATCH v6 2/2] send-email: finer-grained SMTP error handling Zheng Yuting
2025-03-21 15:38           ` [GSoC PATCH v6 0/2] send-email: improve error capture and status code handling Junio C Hamano
2025-03-23  2:21           ` [GSoC PATCH v7 " Zheng Yuting
2025-03-23  2:21             ` [GSoC PATCH v7 1/2] send-email: capture errors in an eval {} block Zheng Yuting
2025-03-23  2:21             ` [GSoC PATCH v7 2/2] send-email: finer-grained SMTP error handling Zheng Yuting
2025-03-24  6:00               ` Junio C Hamano
2025-03-24 14:53           ` [GSoC PATCH v8 0/2] send-email: improve error capture and status code handling Zheng Yuting
2025-03-24 14:53             ` [GSoC PATCH v8 1/2] send-email: capture errors in an eval {} block Zheng Yuting
2025-03-24 14:53             ` [GSoC PATCH v8 2/2] send-email: finer-grained SMTP error handling Zheng Yuting
2025-03-25 15:34               ` Junio C Hamano
2025-03-26  7:52             ` [GSoC PATCH v9 0/2] send-email: improve error capture and status code handling Zheng Yuting
2025-03-26  7:52               ` [GSoC PATCH v9 1/2] send-email: capture errors in an eval {} block Zheng Yuting
2025-03-26  7:52               ` [GSoC PATCH v9 2/2] send-email: finer-grained SMTP error handling Zheng Yuting

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250321025128.68463-2-05ZYT30@gmail.com \
    --to=05zyt30@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=meetsoni3017@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).