# [PATCH] Git.pm: better error message

5 messages from 2010-06-14 to 2010-06-17. Participants: Philippe Bruhat (BooK), Jeff King.
Thread: https://gitlist.dev/t/24102

## Philippe Bruhat (BooK), 2010-06-14 01:00

Subject: [PATCH] Git.pm: better error message
Message-ID: <1276477222-21465-1-git-send-email-book@cpan.org>
URL: https://gitlist.dev/e/1276477222-21465-1-git-send-email-book%40cpan.org

```
-d doesn't set $! if the directory doesn't exist

Signed-off-by: Philippe Bruhat (BooK) <book@cpan.org>
---
 perl/Git.pm |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/perl/Git.pm b/perl/Git.pm
index 1926dc9..2a3920b 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -172,7 +172,7 @@ sub repository {
 	}
 
 	if (defined $opts{Directory}) {
-		-d $opts{Directory} or throw Error::Simple("Directory not found: $!");
+		-d $opts{Directory} or throw Error::Simple("Directory not found: $opts{Directory}");
 
 		my $search = Git->repository(WorkingCopy => $opts{Directory});
 		my $dir;
@@ -545,7 +545,7 @@ sub wc_chdir {
 		or throw Error::Simple("bare repository");
 
 	-d $self->wc_path().'/'.$subdir
-		or throw Error::Simple("subdir not found: $!");
+		or throw Error::Simple("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.
 
-- 
1.7.0.4

```

## Jeff King, 2010-06-14 07:10

Subject: Re: [PATCH] Git.pm: better error message
Message-ID: <20100614071046.GA5639@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20100614071046.GA5639%40sigill.intra.peff.net
In-Reply-To: <1276477222-21465-1-git-send-email-book@cpan.org>

```
On Mon, Jun 14, 2010 at 03:00:22AM +0200, Philippe Bruhat (BooK) wrote:

> -d doesn't set $! if the directory doesn't exist

Really?

  $ perl -e '-d "bogus" or die "fail: $!"'
  fail: No such file or directory at -e line 1.

On the other hand:

  $ touch file
  perl -e '-d "file" or die "fail: $!"'
  fail:  at -e line 1.

So perhaps it is best not to rely on $!. Also, this is with perl 5.10.
Is it different with other versions?

-Peff

```

## Philippe Bruhat (BooK), 2010-06-14 09:19

Subject: Re: [PATCH] Git.pm: better error message
Message-ID: <20100614091916.GB31387@swoosh>
URL: https://gitlist.dev/e/20100614091916.GB31387%40swoosh
In-Reply-To: <20100614071046.GA5639@sigill.intra.peff.net>

```
On Mon, Jun 14, 2010 at 03:10:46AM -0400, Jeff King wrote:
> On Mon, Jun 14, 2010 at 03:00:22AM +0200, Philippe Bruhat (BooK) wrote:
> 
> > -d doesn't set $! if the directory doesn't exist
> 
> Really?
> 
>   $ perl -e '-d "bogus" or die "fail: $!"'
>   fail: No such file or directory at -e line 1.
> 
> On the other hand:
> 
>   $ touch file
>   perl -e '-d "file" or die "fail: $!"'
>   fail:  at -e line 1.
> 
> So perhaps it is best not to rely on $!. Also, this is with perl 5.10.
> Is it different with other versions?

Come to think of it, it probably makes sense: -d probably depends on stat
to get the information about the file, and that fails harder when the
file in question doesn't exists.

I guess the best would be to put the directory name in the error message
(always interesting information), and keep $! in case it was set by an
harder error.

-- 
 Philippe Bruhat (BooK)

 There is no solution to a problem of sheer greed.
                                    (Moral from Groo The Wanderer #94 (Epic))

```

## Jeff King, 2010-06-14 09:20

Subject: Re: [PATCH] Git.pm: better error message
Message-ID: <20100614092024.GA7854@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20100614092024.GA7854%40sigill.intra.peff.net
In-Reply-To: <20100614091916.GB31387@swoosh>

```
On Mon, Jun 14, 2010 at 11:19:16AM +0200, Philippe Bruhat (BooK) wrote:

> Come to think of it, it probably makes sense: -d probably depends on stat
> to get the information about the file, and that fails harder when the
> file in question doesn't exists.

Yeah, that makes sense to me.

> I guess the best would be to put the directory name in the error message
> (always interesting information), and keep $! in case it was set by an
> harder error.

Agreed.

-Peff

```

## Philippe Bruhat (BooK), 2010-06-17 23:47

Subject: [PATCH] Git.pm: better error message
Message-ID: <1276818451-17003-1-git-send-email-book@cpan.org>
URL: https://gitlist.dev/e/1276818451-17003-1-git-send-email-book%40cpan.org
In-Reply-To: <1276477222-21465-1-git-send-email-book@cpan.org>

```
Provide the bad directory name alongside with $!

Note: $! is set if there is "No such file or directory",
but isn't set if the file exists but is not a directory.

Signed-off-by: Philippe Bruhat (BooK) <book@cpan.org>
---
 perl/Git.pm |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/perl/Git.pm b/perl/Git.pm
index 1926dc9..6cb0dd1 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -172,7 +172,7 @@ sub repository {
 	}
 
 	if (defined $opts{Directory}) {
-		-d $opts{Directory} or throw Error::Simple("Directory not found: $!");
+		-d $opts{Directory} or throw Error::Simple("Directory not found: $opts{Directory} $!");
 
 		my $search = Git->repository(WorkingCopy => $opts{Directory});
 		my $dir;
@@ -545,7 +545,7 @@ sub wc_chdir {
 		or throw Error::Simple("bare repository");
 
 	-d $self->wc_path().'/'.$subdir
-		or throw Error::Simple("subdir not found: $!");
+		or throw Error::Simple("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.
 
-- 
1.7.0.4

```
