From: Jacob Bachmeyer Date: Sun, 4 Jun 2023 00:49:00 +0000 (-0500) Subject: Make sub throw a class method on all exceptions X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=fe809d28f07fa712059cdf421b8b8827a7f9f94f;p=gatekeeper.git Make sub throw a class method on all exceptions The throw sub can still be called as a function, since its syntax is easily compatible both ways. When called as a function, the exception type may be abbreviated. --- diff --git a/gatekeeper.pl b/gatekeeper.pl index d918953..16a1863 100755 --- a/gatekeeper.pl +++ b/gatekeeper.pl @@ -901,8 +901,37 @@ BEGIN { return $self->{type} eq $query } + + +=item throw $type => ($key => $value)... + +=item throw Local::Exception::$type ($key => $value)... + +Throw a TYPE exception, with further options specified as key/value pairs. + +All types include a brief summary given with the C option or +implied by the type. This summary is sent to the log. + +This can be called as a class method on any exception type or imported and +called as a function. The C prefix may be omitted when +called as a function. + +=cut + + sub throw { + my $type; ($type = shift) =~ s/^Local::Exception:://; + my $ob = bless {type => $type, @_}, 'Local::Exception::'.$type; + + # ftp_syslog is a function defined at top-level in main + ::ftp_syslog err => $ob->summary if $ob->summary; + + die $ob; + } + } +BEGIN { *throw = \&Local::Exception::throw } + { package Local::Exception::package_configuration; {our @ISA = qw(Local::Exception)} @@ -1031,24 +1060,6 @@ package. END } -=item throw $type => ($key => $value)... - -Throw a TYPE exception, with further options specified as key/value pairs. - -All types include a brief summary given with the C option or -implied by the type. This summary is sent to the log. - -=cut - -sub throw { - my $type = shift; - my $ob = bless {type => $type, @_}, 'Local::Exception::'.$type; - - ftp_syslog err => $ob->summary if $ob->summary; - - die $ob; -} - =back