| 1 | # ABSTRACT: Exception class for Git::Native | ||
| 2 | |||
| 3 | package Git::Native::Error; | ||
| 4 | our $VERSION = '0.006'; | ||
| 5 | 1 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Exporter qw( import ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use Git::Libgit2::Error (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Git::Libgit2 qw( | ||
| 9 | GIT_ENOTFOUND GIT_EEXISTS GIT_EAUTH GIT_ECERTIFICATE | ||
| 10 | GIT_ECONFLICT GIT_ENONFASTFORWARD GIT_EUNBORNBRANCH GIT_EINVALIDSPEC | ||
| 11 | GIT_EMODIFIED GIT_ELOCKED GIT_EBAREREPO GIT_EAMBIGUOUS GIT_EOWNER | ||
| 12 | ); | ||
| 13 | extends 'Throwable::Error'; | ||
| 14 | |||
| 15 | our @EXPORT_OK = qw( check_rc ); | ||
| 16 | |||
| 17 | has code => ( is => 'ro', required => 1 ); | ||
| 18 | has klass => ( is => 'ro', default => 0 ); | ||
| 19 | |||
| 20 | # Predicates over the libgit2 error `code`, for callers that branch on the | ||
| 21 | # kind of failure (`if ($err->is_not_found) {...}`). Curated to the codes | ||
| 22 | # that have real consumer use cases; the long tail is reachable via ->code | ||
| 23 | # compared against the Git::Libgit2 GIT_E* constants. | ||
| 24 | sub is_not_found { $_[0]->code == GIT_ENOTFOUND ? 1 : 0 } | ||
| 25 | sub is_exists { $_[0]->code == GIT_EEXISTS ? 1 : 0 } | ||
| 26 | sub is_auth { $_[0]->code == GIT_EAUTH ? 1 : 0 } | ||
| 27 | sub is_certificate { $_[0]->code == GIT_ECERTIFICATE ? 1 : 0 } | ||
| 28 | sub is_conflict { $_[0]->code == GIT_ECONFLICT ? 1 : 0 } | ||
| 29 | sub is_not_fast_forward { $_[0]->code == GIT_ENONFASTFORWARD ? 1 : 0 } | ||
| 30 | sub is_unborn_branch { $_[0]->code == GIT_EUNBORNBRANCH ? 1 : 0 } | ||
| 31 | sub is_invalid_spec { $_[0]->code == GIT_EINVALIDSPEC ? 1 : 0 } | ||
| 32 | sub is_not_matched { $_[0]->code == GIT_EMODIFIED ? 1 : 0 } | ||
| 33 | sub is_locked { $_[0]->code == GIT_ELOCKED ? 1 : 0 } | ||
| 34 | sub is_bare_repo { $_[0]->code == GIT_EBAREREPO ? 1 : 0 } | ||
| 35 | sub is_ambiguous { $_[0]->code == GIT_EAMBIGUOUS ? 1 : 0 } | ||
| 36 | sub is_owner_mismatch { $_[0]->code == GIT_EOWNER ? 1 : 0 } | ||
| 37 | |||
| 38 | around BUILDARGS => sub { | ||
| 39 | my ( $orig, $class, @args ) = @_; | ||
| 40 | my %args = @args == 1 && ref $args[0] ? %{ $args[0] } : @args; | ||
| 41 | $args{message} //= '<unknown libgit2 error>'; | ||
| 42 | return $class->$orig(\%args); | ||
| 43 | }; | ||
| 44 | |||
| 45 | # check_rc($rc): pass non-negative rc straight through; on a negative rc, | ||
| 46 | # pull libgit2's thread-local error (a low-level Git::Libgit2::Error) and | ||
| 47 | # re-throw it as a Throwable Git::Native::Error so no raw libgit2 error | ||
| 48 | # object leaks above this layer. Every FFI int-return goes through here, so | ||
| 49 | # callers in Git::Native import check_rc from THIS module, not Git::Libgit2. | ||
| 50 | sub check_rc { | ||
| 51 | my ($rc) = @_; | ||
| 52 | return $rc if !defined $rc || $rc >= 0; | ||
| 53 | my $low = Git::Libgit2::Error->last($rc); | ||
| 54 | __PACKAGE__->throw( | ||
| 55 | code => $low->code, | ||
| 56 | klass => $low->klass, | ||
| 57 | message => $low->message, | ||
| 58 | ); | ||
| 59 | } | ||
| 60 | |||
| 61 | 1; | ||
| 62 | |||
| 63 | __END__ | ||