git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH][GIT.PM 2/3] Getting rid of throwing Error::Simple objects in favour of simple Perl scalars which can be caught in eval{} blocks

From
Subho Sankar Banerjee <subs.zero@gmail.com>
Date
May 19, 2012, 07:08 UTC
Message-ID
<1337411317-14931-2-git-send-email-subs.zero@gmail.com>
In-Reply-To
<1337411317-14931-1-git-send-email-subs.zero@gmail.com>
Signed-off-by: Subho Sankar Banerjee <subs.zero@gmail.com>
---
 perl/Git.pm |   52 ++++++++++++++++++++++++++--------------------------
 1 file changed, 26 insertions(+), 26 deletions(-)
diff --git a/perl/Git.pm b/perl/Git.pm
index 497f420..52777d4 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -160,7 +160,7 @@ sub repository {
 	if (defined $args[0]) {
 		if ($#args % 2 != 1) {
 			# Not a hash.
-			$#args == 0 or throw Error::Simple("bad usage");
+			$#args == 0 or die "bad usage";
 			%opts = ( Directory => $args[0] );
 		} else {
 			%opts = @args;
@@ -173,7 +173,7 @@ sub repository {
 	}
 
 	if (defined $opts{Directory}) {
-		-d $opts{Directory} or throw Error::Simple("Directory not found: $opts{Directory} $!");
+		-d $opts{Directory} or die "Directory not found: $opts{Directory} $!";
 
 		my $search = Git->repository(WorkingCopy => $opts{Directory});
 		my $dir;
@@ -193,7 +193,7 @@ sub repository {
 			$dir = abs_path($opts{Directory}) . '/';
 			if ($prefix) {
 				if (substr($dir, -length($prefix)) ne $prefix) {
-					throw Error::Simple("rev-parse confused me - $dir does not have trailing $prefix");
+					die "rev-parse confused me - $dir does not have trailing $prefix";
 				}
 				substr($dir, -length($prefix)) = '';
 			}
@@ -206,14 +206,14 @@ sub repository {
 
 			unless (-d "$dir/refs" and -d "$dir/objects" and -e "$dir/HEAD") {
 				# Mimic git-rev-parse --git-dir error message:
-				throw Error::Simple("fatal: Not a git repository: $dir");
+				die "fatal: Not a git repository: $dir";
 			}
 			my $search = Git->repository(Repository => $dir);
 			try {
 				$search->command('symbolic-ref', 'HEAD');
 			} catch Git::Error::Command with {
 				# Mimic git-rev-parse --git-dir error message:
-				throw Error::Simple("fatal: Not a git repository: $dir");
+				die "fatal: Not a git repository: $dir";
 			}
 
 			$opts{Repository} = abs_path($dir);
@@ -469,7 +469,7 @@ sub command_noisy {
 
 	my $pid = fork;
 	if (not defined $pid) {
-		throw Error::Simple("fork failed: $!");
+		die "fork failed: $!";
 	} elsif ($pid == 0) {
 		_cmd_exec($self, $cmd, @args);
 	}
@@ -552,10 +552,10 @@ and the directory must exist.
 sub wc_chdir {
 	my ($self, $subdir) = @_;
 	$self->wc_path()
-		or throw Error::Simple("bare repository");
+		or die "bare repository";
 
 	-d $self->wc_path().'/'.$subdir
-		or throw Error::Simple("subdir not found: $subdir $!");
+		or die "subdir not found: $subdir $!";
 	# Of course we will not "hold" the subdirectory so anyone
 	# can delete it now and we will never know. But at least we tried.
 
@@ -825,13 +825,13 @@ sub hash_and_insert_object {
 
 	unless (print $out $filename, "\n") {
 		$self->_close_hash_and_insert_object();
-		throw Error::Simple("out pipe went bad");
+		die "out pipe went bad";
 	}
 
 	chomp(my $hash = <$in>);
 	unless (defined($hash)) {
 		$self->_close_hash_and_insert_object();
-		throw Error::Simple("in pipe went bad");
+		die "in pipe went bad";
 	}
 
 	return $hash;
@@ -873,7 +873,7 @@ sub cat_blob {
 
 	unless (print $out $sha1, "\n") {
 		$self->_close_cat_blob();
-		throw Error::Simple("out pipe went bad");
+		die "out pipe went bad";
 	}
 
 	my $description = <$in>;
@@ -900,7 +900,7 @@ sub cat_blob {
 		my $read = read($in, $blob, $bytesToRead, $bytesRead);
 		unless (defined($read)) {
 			$self->_close_cat_blob();
-			throw Error::Simple("in pipe went bad");
+			die "in pipe went bad";
 		}
 
 		$bytesRead += $read;
@@ -911,16 +911,16 @@ sub cat_blob {
 	my $read = read($in, $newline, 1);
 	unless (defined($read)) {
 		$self->_close_cat_blob();
-		throw Error::Simple("in pipe went bad");
+		die "in pipe went bad";
 	}
 	unless ($read == 1 && $newline eq "\n") {
 		$self->_close_cat_blob();
-		throw Error::Simple("didn't find newline after blob");
+		die "didn't find newline after blob";
 	}
 
 	unless (print $fh $blob) {
 		$self->_close_cat_blob();
-		throw Error::Simple("couldn't write to passed in filehandle");
+    die "couldn't write to passed in filehandle";
 	}
 
 	return $size;
@@ -1023,8 +1023,8 @@ sub _temp_cache {
 	my $temp_fd = \$TEMP_FILEMAP{$name};
 	if (defined $$temp_fd and $$temp_fd->opened) {
 		if ($TEMP_FILES{$$temp_fd}{locked}) {
-			throw Error::Simple("Temp file with moniker '" .
-				$name . "' already in use");
+			die "Temp file with moniker '" .
+				$name . "' already in use";
 		}
 	} else {
 		if (defined $$temp_fd) {
@@ -1041,7 +1041,7 @@ sub _temp_cache {
 
 		($$temp_fd, $fname) = File::Temp->tempfile(
 			'Git_XXXXXX', UNLINK => 1, DIR => $tmpdir,
-			) or throw Error::Simple("couldn't open new temp file");
+			) or die "couldn't open new temp file";
 
 		$$temp_fd->autoflush;
 		binmode $$temp_fd;
@@ -1052,7 +1052,7 @@ sub _temp_cache {
 
 sub _verify_require {
 	eval { require File::Temp; require File::Spec; };
-	$@ and throw Error::Simple($@);
+	$@ and die "$@";
 }
 
 =item temp_reset ( FILEHANDLE )
@@ -1065,11 +1065,11 @@ sub temp_reset {
 	my ($self, $temp_fd) = _maybe_self(@_);
 
 	truncate $temp_fd, 0
-		or throw Error::Simple("couldn't truncate file");
+		or die "couldn't truncate file";
 	sysseek($temp_fd, 0, SEEK_SET) and seek($temp_fd, 0, SEEK_SET)
-		or throw Error::Simple("couldn't seek to beginning of file");
+		or die "couldn't seek to beginning of file";
 	sysseek($temp_fd, 0, SEEK_CUR) == 0 and tell($temp_fd) == 0
-		or throw Error::Simple("expected file position to be reset");
+		or die "expected file position to be reset";
 }
 
 =item temp_path ( NAME )
@@ -1100,8 +1100,8 @@ sub END {
 =head1 ERROR HANDLING
 
 All functions are supposed to throw Perl exceptions in case of errors.
-See the L<Error> module on how to catch those. Most exceptions are mere
-L<Error::Simple> instances.
+These errors are perl scalars which can be caught in the $@ values in
+eval{} blocks.
 
 However, the C<command()>, C<command_oneline()> and C<command_noisy()>
 functions suite can throw C<Git::Error::Command> exceptions as well: those are
@@ -1227,7 +1227,7 @@ sub _maybe_self {
 # Check if the command id is something reasonable.
 sub _check_valid_cmd {
 	my ($cmd) = @_;
-	$cmd =~ /^[a-z0-9A-Z_-]+$/ or throw Error::Simple("bad command: $cmd");
+	$cmd =~ /^[a-z0-9A-Z_-]+$/ or die "bad command: $cmd";
 }
 
 # Common backend for the pipe creators.
@@ -1261,7 +1261,7 @@ sub _command_common_pipe {
 	} else {
 		my $pid = open($fh, $direction);
 		if (not defined $pid) {
-			throw Error::Simple("open failed: $!");
+			die "open failed: $!";
 		} elsif ($pid == 0) {
 			if (defined $opts{STDERR}) {
 				close STDERR;
-- 
1.7.9.5
Previous: Subho Sankar BanerjeeNext: Andrew Sayers
Message 18 of 24 in “Git.pm”
  1. Subho BanerjeeApr 26, 2012
  2. Randal L. SchwartzApr 26, 2012
  3. Tim HeniganApr 26, 2012
  4. Subho BanerjeeApr 26, 2012
  5. Jonathan NiederApr 26, 2012
  6. Subho BanerjeeMay 10, 2012
  7. Jonathan NiederMay 10, 2012
  8. demerphqMay 10, 2012
  9. Subho BanerjeeMay 10, 2012
  10. demerphqMay 10, 2012
  11. Junio C HamanoMay 10, 2012
  12. demerphqMay 10, 2012
  13. Andrew SayersMay 10, 2012
  14. demerphqMay 11, 2012
  15. Randal L. SchwartzMay 11, 2012
  16. Junio C HamanoMay 11, 2012
  17. [GIT.PM 1/3] Ignore files produced from exuberant-ctagsSubho Sankar Banerjee, May 19, 2012
  18. [GIT.PM 2/3] Getting rid of throwing Error::Simple objects in favour of simple Perl scalars which can be caught in eval{} blocksSubho Sankar Banerjee, May 19, 2012
  19. Andrew SayersMay 19, 2012
  20. Subho BanerjeeMay 23, 2012
  21. Andrew SayersMay 23, 2012
  22. [GIT.PM 3/3] Perl code uses eval{}/die instead of Error::Simple and Git::Error::CommandSubho Sankar Banerjee, May 19, 2012
  23. Junio C HamanoApr 26, 2012
  24. Sam VilainApr 26, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.