Git.pm: Better error handling
So far, errors just killed the whole program and in case of an error inside of libgit it would be totally uncatchable. This patch makes Git.pm throw standard Perl exceptions instead. In the future we might subclass Error to Git::Error or something but for now Error::Simple is more than enough. Signed-off-by: Petr Baudis <pasky@suse.cz> Signed-off-by: Junio C Hamano <junkio@cox.net>
Petr Baudis committed
Jun 24, 2006 at 04:34 UTC
97b16c067492506287a6f474e79ef6cbe0a30e49
2 files changed
+60
-16
perl/Git.pm
+21
-16
@@ -88,7 +88,8 @@ increate nonwithstanding).
88
=cut
89
90
91
-use Carp qw(carp croak);
91
+use Carp qw(carp); # croak is bad - throw instead
92
+use Error qw(:try);
93
94
require XSLoader;
95
XSLoader::load('Git', $VERSION);
@@ -143,14 +144,14 @@ sub repository {
144
if (defined $args[0]) {
145
if ($#args % 2 != 1) {
146
# Not a hash.
146
- $#args == 0 or croak "bad usage";
147
- %opts = (Directory => $args[0]);
147
+ $#args == 0 or throw Error::Simple("bad usage");
148
+ %opts = ( Directory => $args[0] );
149
} else {
150
%opts = @args;
151
}
152
153
if ($opts{Directory}) {
153
- -d $opts{Directory} or croak "Directory not found: $!";
154
+ -d $opts{Directory} or throw Error::Simple("Directory not found: $!");
155
if (-d $opts{Directory}."/.git") {
156
# TODO: Might make this more clever
157
$opts{WorkingCopy} = $opts{Directory};
@@ -242,11 +243,11 @@ read.
243
sub command_pipe {
244
my ($self, $cmd, @args) = _maybe_self(@_);
245
245
- $cmd =~ /^[a-z0-9A-Z_-]+$/ or croak "bad command: $cmd";
246
+ $cmd =~ /^[a-z0-9A-Z_-]+$/ or throw Error::Simple("bad command: $cmd");
247
248
my $pid = open(my $fh, "-|");
249
if (not defined $pid) {
249
- croak "open failed: $!";
250
+ throw Error::Simple("open failed: $!");
251
} elsif ($pid == 0) {
252
_cmd_exec($self, $cmd, @args);
253
}
@@ -271,16 +272,17 @@ The function returns only after the command has finished running.
272
sub command_noisy {
273
my ($self, $cmd, @args) = _maybe_self(@_);
274
274
- $cmd =~ /^[a-z0-9A-Z_-]+$/ or croak "bad command: $cmd";
275
+ $cmd =~ /^[a-z0-9A-Z_-]+$/ or throw Error::Simple("bad command: $cmd");
276
277
my $pid = fork;
278
if (not defined $pid) {
278
- croak "fork failed: $!";
279
+ throw Error::Simple("fork failed: $!");
280
} elsif ($pid == 0) {
281
_cmd_exec($self, $cmd, @args);
282
}
283
if (waitpid($pid, 0) > 0 and $? != 0) {
283
- croak "exit status: $?";
284
+ # This is the best candidate for a custom exception class.
285
+ throw Error::Simple("exit status: $?");
286
}
287
}
288
@@ -340,10 +342,10 @@ are involved.
342
343
=back
344
343
-=head1 TODO
345
+=head1 ERROR HANDLING
346
345
-This is still fairly crude.
346
-We need some good way to report errors back except just dying.
347
+All functions are supposed to throw Perl exceptions in case of errors.
348
+See L<Error>.
349
350
=head1 COPYRIGHT
351
@@ -372,8 +374,8 @@ sub _cmd_exec {
374
$self->{opts}->{Repository} and $ENV{'GIT_DIR'} = $self->{opts}->{Repository};
375
$self->{opts}->{WorkingCopy} and chdir($self->{opts}->{WorkingCopy});
376
}
375
- xs__execv_git_cmd(@args);
376
- croak "exec failed: $!";
377
+ _execv_git_cmd(@args);
378
+ die "exec failed: $!";
379
}
380
381
# Execute the given Git command ($_[0]) with arguments ($_[1..])
@@ -388,7 +390,8 @@ sub _cmd_close {
390
# It's just close, no point in fatalities
391
carp "error closing pipe: $!";
392
} elsif ($? >> 8) {
391
- croak "exit status: ".($? >> 8);
393
+ # This is the best candidate for a custom exception class.
394
+ throw Error::Simple("exit status: ".($? >> 8));
395
}
396
# else we might e.g. closed a live stream; the command
397
# dying of SIGPIPE would drive us here.
@@ -415,6 +418,8 @@ sub _call_gate {
418
#xs_call_gate($self->{opts}->{Repository});
419
}
420
421
+ # Having to call throw from the C code is a sure path to insanity.
422
+ local $SIG{__DIE__} = sub { throw Error::Simple("@_"); };
423
&$xsfunc(@args);
424
}
425
@@ -422,7 +427,7 @@ sub AUTOLOAD {
427
my $xsname;
428
our $AUTOLOAD;
429
($xsname = $AUTOLOAD) =~ s/.*:://;
425
- croak "&Git::$xsname not defined" if $xsname =~ /^xs_/;
430
+ throw Error::Simple("&Git::$xsname not defined") if $xsname =~ /^xs_/;
431
$xsname = 'xs_'.$xsname;
432
_call_gate(\&$xsname, @_);
433
}
perl/Git.xs
+39
@@ -8,6 +8,8 @@
8
#include "../cache.h"
9
#include "../exec_cmd.h"
10
11
+#define die perlyshadow_die__
12
+
13
/* XS and Perl interface */
14
#include "EXTERN.h"
15
#include "perl.h"
@@ -15,11 +17,48 @@
17
18
#include "ppport.h"
19
20
+#undef die
21
+
22
+
23
+static char *
24
+report_xs(const char *prefix, const char *err, va_list params)
25
+{
26
+ static char buf[4096];
27
+ strcpy(buf, prefix);
28
+ vsnprintf(buf + strlen(prefix), 4096 - strlen(prefix), err, params);
29
+ return buf;
30
+}
31
+
32
+void
33
+die_xs(const char *err, va_list params)
34
+{
35
+ char *str;
36
+ str = report_xs("fatal: ", err, params);
37
+ croak(str);
38
+}
39
+
40
+int
41
+error_xs(const char *err, va_list params)
42
+{
43
+ char *str;
44
+ str = report_xs("error: ", err, params);
45
+ warn(str);
46
+ return -1;
47
+}
48
+
49
50
MODULE = Git PACKAGE = Git
51
52
PROTOTYPES: DISABLE
53
54
+
55
+BOOT:
56
+{
57
+ set_error_routine(error_xs);
58
+ set_die_routine(die_xs);
59
+}
60
+
61
+
62
# /* TODO: xs_call_gate(). See Git.pm. */
63
64