1 package File
::KDBX
::Error
;
2 # ABSTRACT: Represents something bad that happened
7 use Exporter
qw(import);
8 use Scalar
::Util
qw(blessed);
9 use namespace
::clean
-except
=> 'import';
11 our $VERSION = '999.999'; # VERSION
13 our @EXPORT = qw(alert error throw);
15 my $WARNINGS_CATEGORY;
17 $WARNINGS_CATEGORY = 'File::KDBX';
18 if (warnings-
>can('register_categories')) {
19 warnings
::register_categories
($WARNINGS_CATEGORY);
22 eval qq{package $WARNINGS_CATEGORY; use warnings::register; 1}; ## no critic ProhibitStringyEval
26 use overload
'""' => 'to_string', cmp => '_cmp';
30 $error = File
::KDBX
::Error-
>new($message, %details);
32 Construct a new error
.
38 my %args = @_ % 2 == 0 ? @_ : (_error
=> shift, @_);
40 my $error = delete $args{_error
};
42 $e =~ s/ at \H+ line \d+.*//g;
46 error
=> $e // 'Something happened',
51 local $Carp::CarpInternal
{''.__PACKAGE__
} = 1;
52 my $mess = $error =~ /at \H+ line \d+/ ? $error : Carp
::longmess
($error);
53 [map { /^\h*(.*?)\.?$/ ? $1 : $_ } split(/\n/, $mess)];
62 $error = error
($error);
63 $error = error
($message, %details);
64 $error = File
::KDBX
::Error-
>error($error);
65 $error = File
::KDBX
::Error-
>error($message, %details);
67 Wrap a thing to make it an error object
. If the thing
is already an error
, it gets returned
. Otherwise what
is
68 passed will be forwarded to L
</new
> to create a new error object
.
70 This can be convenient
for error handling
when you
're not sure what the exception is but you want to treat it
71 as a B<File::KDBX::Error>. Example:
74 if (my $error = error(@_)) {
75 if ($error->type eq 'key
.missing
') {
76 handle_missing_key($error);
79 handle_other_error($error);
86 my $class = @_ && $_[0] eq __PACKAGE__ ? shift : undef;
87 my $self = (blessed($_[0]) && $_[0]->isa('File
::KDBX
::Error
'))
91 : __PACKAGE__->new(@_);
97 \%details = $error->details;
99 Get the error details.
106 my $details = $self->{details} //= {};
107 @$details{keys %args} = values %args;
113 Get the value of C<errno> when the exception was created.
117 Get the value of C<$@> (i.e. latest exception) at the time the exception was created.
121 Get a stack trace indicating where in the code the exception was created.
127 Get the exception type, if any.
131 sub errno { $_[0]->{errno} }
132 sub previous { $_[0]->{previous} }
133 sub trace { $_[0]->{trace} // [] }
134 sub type { $_[0]->details->{type} // '' }
138 $message = $error->to_string;
143 This does not contain a stack trace, but you can set the C<DEBUG> environment
144 variable to truthy to stringify the whole error object.
148 sub _cmp { "$_[0]" cmp "$_[1]" }
152 my $msg = "$self->{trace}[0]";
153 $msg .= '.' if $msg !~ /[\.\!\?]$/;
155 require Data::Dumper;
156 local $Data::Dumper::Indent = 1;
157 local $Data::Dumper::Quotekeys = 0;
158 local $Data::Dumper::Sortkeys = 1;
159 local $Data::Dumper::Terse = 1;
160 local $Data::Dumper::Trailingcomma = 1;
161 local $Data::Dumper::Useqq = 1;
162 $msg .= "\n" . Data::Dumper::Dumper $self;
164 $msg .= "\n" if $msg !~ /\n$/;
170 File::KDBX::Error::throw($message, %details);
178 my $self = error(@_);
184 File::KDBX::Error::warn($message, %details);
192 return if !($File::KDBX::WARNINGS // 1);
194 my $self = error(@_);
196 # Use die and warn directly instead of warnings::warnif because the latter only provides the stringified
197 # error to the warning signal handler (perl 5.34). Maybe that's a warnings
.pm bug
?
199 if (my $fatal = warnings-
>can('fatal_enabled_at_level')) {
200 my $blame = _find_blame_frame
();
201 die $self if $fatal->($WARNINGS_CATEGORY, $blame);
204 if (my $enabled = warnings-
>can('enabled_at_level')) {
205 my $blame = _find_blame_frame
();
206 warn $self if $enabled->($WARNINGS_CATEGORY, $blame);
208 elsif ($enabled = warnings-
>can('enabled')) {
209 warn $self if $enabled->($WARNINGS_CATEGORY);
221 Importable alias
for L
</warn>.
225 sub alert
{ goto &warn }
227 sub _find_blame_frame
{
230 my ($package) = caller($frame);
232 return $frame - 1 if $package !~ /^\Q$WARNINGS_CATEGORY\E/;