Make sub throw a class method on all exceptions
authorJacob Bachmeyer <jcb@gnu.org>
Sun, 4 Jun 2023 00:49:00 +0000 (19:49 -0500)
committerJacob Bachmeyer <jcb@gnu.org>
Sun, 4 Jun 2023 00:49:00 +0000 (19:49 -0500)
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.

gatekeeper.pl

index d91895367b520cea5cdfebb5c926741058bb7787..16a18633ac9d16fcc3c2272920f4c44613807699 100755 (executable)
@@ -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<summary> 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<Local::Exception::> 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<summary> 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;
-}
-
 \f
 
 =back