]> Dogcows Code - chaz/p5-File-KDBX/blobdiff - lib/File/KDBX/Error.pm
Add function for creating class attributes
[chaz/p5-File-KDBX] / lib / File / KDBX / Error.pm
index f80155796f4ae40e8d17afcda9dd8b0c8bdfce39..d12d0806588e35ae67bddfe1148847fc504055c8 100644 (file)
@@ -15,7 +15,12 @@ our @EXPORT = qw(alert error throw);
 my $WARNINGS_CATEGORY;
 BEGIN {
     $WARNINGS_CATEGORY = 'File::KDBX';
-    warnings::register_categories($WARNINGS_CATEGORY) if warnings->can('register_categories');
+    if (warnings->can('register_categories')) {
+        warnings::register_categories($WARNINGS_CATEGORY);
+    }
+    else {
+        eval qq{package $WARNINGS_CATEGORY; use warnings::register; 1}; ## no critic ProhibitStringyEval
+    }
 }
 
 use overload '""' => 'to_string', cmp => '_cmp';
@@ -34,7 +39,7 @@ sub new {
 
     my $error = delete $args{_error};
     my $e = $error;
-    $e =~ s/ at \H+ line \d+.*//g;
+    $e =~ s/ at \H+ line \d+.*//g;
 
     my $self = bless {
         details     => \%args,
@@ -65,7 +70,7 @@ passed will be forwarded to L</new> to create a new error object.
 This can be convenient for error handling when you're not sure what the exception is but you want to treat it
 as a B<File::KDBX::Error>. Example:
 
-    eval { .... };
+    eval { ... };
     if (my $error = error(@_)) {
         if ($error->type eq 'key.missing') {
             handle_missing_key($error);
@@ -78,10 +83,11 @@ as a B<File::KDBX::Error>. Example:
 =cut
 
 sub error {
+    my $class = @_ && $_[0] eq __PACKAGE__ ? shift : undef;
     my $self = (blessed($_[0]) && $_[0]->isa('File::KDBX::Error'))
         ? shift
-        : (@_ && $_[0] eq __PACKAGE__)
-            ? shift->new(@_)
+        : $class
+            ? $class->new(@_)
             : __PACKAGE__->new(@_);
     return $self;
 }
@@ -102,13 +108,30 @@ sub details {
     return $details;
 }
 
-sub errno { $_[0]->{errno} }
+=attr errno
 
-sub previous { $_[0]->{previous} }
+Get the value of C<errno> when the exception was created.
+
+=attr previous
+
+Get the value of C<$@> (i.e. latest exception) at the time the exception was created.
+
+=attr trace
+
+Get a stack trace indicating where in the code the exception was created.
 
-sub trace { $_[0]->{trace} // [] }
+=cut
+
+=attr type
+
+Get the exception type, if any.
+
+=cut
 
-sub type { $_[0]->details->{type} // '' }
+sub errno    { $_[0]->{errno} }
+sub previous { $_[0]->{previous} }
+sub trace    { $_[0]->{trace} // [] }
+sub type     { $_[0]->details->{type} // '' }
 
 =method to_string
 
@@ -124,16 +147,10 @@ variable to truthy to stringify the whole error object.
 
 sub _cmp { "$_[0]" cmp "$_[1]" }
 
-sub PROPAGATE {
-    'wat';
-}
-
 sub to_string {
     my $self = shift;
-    # return "uh oh\n";
     my $msg = "$self->{trace}[0]";
-    $msg .= '.' if $msg !~ /[\.\!\?]$/; # Why does this cause infinite recursion on some perls?
-    # $msg .= '.' if $msg !~ /(?:\.|!|\?)$/;
+    $msg .= '.' if $msg !~ /[\.\!\?]$/;
     if ($ENV{DEBUG}) {
         require Data::Dumper;
         local $Data::Dumper::Indent = 1;
This page took 0.025397 seconds and 4 git commands to generate.