| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package SVN::Friendly::Exceptions; | ||
| 5 | |||
| 6 | #------------------------------------------------------------------ | ||
| 7 | use Exporter; | ||
| 8 | our @ISA = qw(Exporter); | ||
| 9 | our @EXPORT_OK=qw(makeErrorHandler); | ||
| 10 | |||
| 11 | #------------------------------------------------------------------ | ||
| 12 | |||
| 13 | use Exception::Lite qw(declareExceptionClass); | ||
| 14 | |||
| 15 | #------------------------------------------------------------------ | ||
| 16 | |||
| 17 | sub ERR_BAD_ARG { 'SVN::Friendly::Exception::BadArg' }; | ||
| 18 | declareExceptionClass(ERR_BAD_ARG | ||
| 19 | , ['Illegal argument <%s>: %s', qw(arg reason)]); | ||
| 20 | |||
| 21 | sub ERR_UNDEF_ARG { 'SVN::Friendly::Exception::UndefArg' }; | ||
| 22 | declareExceptionClass(ERR_UNDEF_ARG | ||
| 23 | , ['Illegal argument: missing/undefined value for the %s parameter' | ||
| 24 | . ' are not allowd', qw(param)]); | ||
| 25 | |||
| 26 | sub ERR_NO_LOG_MSG { 'SVN::Friendly::Exception::NoLogMsg' }; | ||
| 27 | declareExceptionClass(ERR_NO_LOG_MSG | ||
| 28 | , ['The current operation tried to change the repository but ' | ||
| 29 | . 'No log message was provided via the method call']); | ||
| 30 | |||
| 31 | sub ERR_SWIG {'SVN::Friendly::Exception::SWIG' }; | ||
| 32 | declareExceptionClass(ERR_SWIG); | ||
| 33 | # std parameters: | ||
| 34 | # errno = SWIG error id | ||
| 35 | |||
| 36 | ##-------------------------------------------------------------------- | ||
| 37 | |||
| 38 | #{ | ||
| 39 | # package SVN::Friendly::ErrString; | ||
| 40 | |||
| 41 | # my $IDX_ID = 0; | ||
| 42 | # my $IDX_MSG = 1; | ||
| 43 | |||
| 44 | # sub new() { | ||
| 45 | # my $sClass = shift @_; | ||
| 46 | # return bless([ @_ ], $sClass); | ||
| 47 | # } | ||
| 48 | |||
| 49 | # sub getErrId() { return shift->[$IDX_ID]; } | ||
| 50 | # sub getErrMsg() { return shift->[$IDX_MSG]; } | ||
| 51 | #} | ||
| 52 | |||
| 53 | #================================================================== | ||
| 54 | # FUNCTIONS | ||
| 55 | #================================================================== | ||
| 56 | |||
| 57 | sub makeErrorHandler { | ||
| 58 | my ($crCustom) = @_; | ||
| 59 | |||
| 60 | my $crGlobal = $SVN::Error::handler; | ||
| 61 | |||
| 62 | return sub { | ||
| 63 | #before Perl 5.0, $[ could be set globally | ||
| 64 | #since the original file did not enforce a version limit and | ||
| 65 | #we want backwards compatibility with Alien::SVN and friends | ||
| 66 | #but we don't want to use the evil $[, we localize it to 0. | ||
| 67 | #(as suggested by MidLifeXis (PerlMonk) and confirmed OK in | ||
| 68 | #Perl 4 by tye | ||
| 69 | #print STDERR "in croak_on_error: verifying\n"; | ||
| 70 | |||
| 71 | #Note: don't want to use shift or copy because then we would | ||
| 72 | #need to free it to avoid a memory leak. | ||
| 73 | local $[ = 0; | ||
| 74 | return @_ unless SVN::Error::is_error($_[0]); | ||
| 75 | |||
| 76 | my $oSwigErr = shift; | ||
| 77 | my $bDone = 0; | ||
| 78 | if ($crCustom) { | ||
| 79 | my $bDiscard = 0; | ||
| 80 | |||
| 81 | eval { | ||
| 82 | #discard error or pass through | ||
| 83 | $bDiscard = &$crCustom($oSwigErr); | ||
| 84 | return 1; | ||
| 85 | } or do { | ||
| 86 | #in case error is thrown | ||
| 87 | my $oErr=$@; | ||
| 88 | $oSwigErr->clear(); | ||
| 89 | die $oErr; | ||
| 90 | }; | ||
| 91 | |||
| 92 | if ($bDiscard) { | ||
| 93 | $oSwigErr->clear(); | ||
| 94 | return; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | &$crGlobal($oSwigErr) if $crGlobal; | ||
| 99 | }; | ||
| 100 | } | ||
| 101 | |||
| 102 | #code based on SVN::Core::croak_on_error | ||
| 103 | #with three modifications: | ||
| 104 | # 1) $[ localized so 0 can be used as base | ||
| 105 | # 2) A ref to a pure perl error object rather than a string is thrown | ||
| 106 | # 3) Stack tracing of Exception::Lite is used rather than carp | ||
| 107 | |||
| 108 | sub croak_on_error { | ||
| 109 | #before Perl 5.0, $[ could be set globally | ||
| 110 | #since the original file did not enforce a version limit and | ||
| 111 | #we want backwards compatibility with Alien::SVN and friends | ||
| 112 | #but we don't want to use the evil $[, we localize it to 0. | ||
| 113 | #(as suggested by MidLifeXis (PerlMonk) and confirmed OK in | ||
| 114 | #Perl 4 by tye | ||
| 115 | #print STDERR "in croak_on_error: verifying\n"; | ||
| 116 | |||
| 117 | |||
| 118 | #Note: don't want to use shift or copy because then we would | ||
| 119 | #need to free it to avoid a memory leak. | ||
| 120 | local $[ = 0; | ||
| 121 | return @_ unless SVN::Error::is_error($_[0]); | ||
| 122 | |||
| 123 | #print STDERR "in croak_on_error: processing\n"; | ||
| 124 | |||
| 125 | my $oSwigErr = shift; | ||
| 126 | my $oPerlErr = ERR_SWIG->new($oSwigErr->expanded_message() | ||
| 127 | , errno => $oSwigErr->apr_err()); | ||
| 128 | #my $oPerlErr = SVN::Friendly::ErrString->new | ||
| 129 | # ($oSwigErr->apr_err(), $oSwigErr->expanded_message()); | ||
| 130 | |||
| 131 | |||
| 132 | #gotta free the memory - to prevent a leak | ||
| 133 | $oSwigErr->clear(); | ||
| 134 | |||
| 135 | #croak($oPerlErr); | ||
| 136 | die $oPerlErr; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub confess_on_error { | ||
| 140 | #before Perl 5.0, $[ could be set globally | ||
| 141 | #since the original file did not enforce a version limit and | ||
| 142 | #we want backwards compatibility with Alien::SVN and friends | ||
| 143 | #but we don't want to use the evil $[, we localize it to 0. | ||
| 144 | #(as suggested by MidLifeXis (PerlMonk) and confirmed OK in | ||
| 145 | #Perl 4 by tye | ||
| 146 | #print STDERR "in croak_on_error: verifying\n"; | ||
| 147 | |||
| 148 | |||
| 149 | #Note: don't want to use shift or copy because then we would | ||
| 150 | #need to free it to avoid a memory leak. | ||
| 151 | local $[ = 0; | ||
| 152 | return @_ unless SVN::Error::is_error($_[0]); | ||
| 153 | |||
| 154 | #print STDERR "in croak_on_error: processing\n"; | ||
| 155 | |||
| 156 | my $oSwigErr = shift; | ||
| 157 | my $oPerlErr = ERR_SWIG->new($oSwigErr->expanded_message() | ||
| 158 | , errno => $oSwigErr->apr_err()); | ||
| 159 | #my $oPerlErr = SVN::Friendly::ErrString->new | ||
| 160 | # ($oSwigErr->apr_err(), $oSwigErr->expanded_message()); | ||
| 161 | |||
| 162 | |||
| 163 | #gotta free the memory - to prevent a leak | ||
| 164 | $oSwigErr->clear(); | ||
| 165 | |||
| 166 | #confess($oPerlErr); | ||
| 167 | print STDERR "$oPerlErr"; | ||
| 168 | } | ||
| 169 | |||
| 170 | #================================================================== | ||
| 171 | # MODULE INITIALIZATION | ||
| 172 | #================================================================== | ||
| 173 | |||
| 174 | 1; |