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 warnings
::register_categories
($WARNINGS_CATEGORY) if warnings-
>can('register_categories');
21 use overload
'""' => 'to_string', cmp => '_cmp';
25 $error = File
::KDBX
::Error-
>new($message, %details);
27 Construct a new error
.
33 my %args = @_ % 2 == 0 ? @_ : (_error
=> shift, @_);
35 my $error = delete $args{_error
};
37 # $e =~ s/ at \H+ line \d+.*//g;
41 error
=> $e // 'Something happened',
46 local $Carp::CarpInternal
{''.__PACKAGE__
} = 1;
47 my $mess = $error =~ /at \H+ line \d+/ ? $error : Carp
::longmess
($error);
48 [map { /^\h*(.*?)\.?$/ ? $1 : $_ } split(/\n/, $mess)];
57 $error = error
($error);
58 $error = error
($message, %details);
59 $error = File
::KDBX
::Error-
>error($error);
60 $error = File
::KDBX
::Error-
>error($message, %details);
62 Wrap a thing to make it an error object
. If the thing
is already an error
, it gets returned
. Otherwise what
is
63 passed will be forwarded to L
</new
> to create a new error object
.
65 This can be convenient
for error handling
when you
're not sure what the exception is but you want to treat it
66 as a B<File::KDBX::Error>. Example:
69 if (my $error = error(@_)) {
70 if ($error->type eq 'key
.missing
') {
71 handle_missing_key($error);
74 handle_other_error($error);
81 my $self = (blessed($_[0]) && $_[0]->isa('File
::KDBX
::Error
'))
83 : (@_ && $_[0] eq __PACKAGE__)
85 : __PACKAGE__->new(@_);
91 \%details = $error->details;
93 Get the error details.
100 my $details = $self->{details} //= {};
101 @$details{keys %args} = values %args;
105 sub errno { $_[0]->{errno} }
107 sub previous { $_[0]->{previous} }
109 sub trace { $_[0]->{trace} // [] }
111 sub type { $_[0]->details->{type} // '' }
115 $message = $error->to_string;
120 This does not contain a stack trace, but you can set the C<DEBUG> environment
121 variable to truthy to stringify the whole error object.
125 sub _cmp { "$_[0]" cmp "$_[1]" }
134 my $msg = "$self->{trace}[0]";
135 $msg .= '.' if $msg !~ /[\.\!\?]$/; # Why does this cause infinite recursion on some perls?
136 # $msg .= '.' if $msg !~ /(?:\.|!|\?)$/;
138 require Data::Dumper;
139 local $Data::Dumper::Indent = 1;
140 local $Data::Dumper::Quotekeys = 0;
141 local $Data::Dumper::Sortkeys = 1;
142 local $Data::Dumper::Terse = 1;
143 local $Data::Dumper::Trailingcomma = 1;
144 local $Data::Dumper::Useqq = 1;
145 $msg .= "\n" . Data::Dumper::Dumper $self;
147 $msg .= "\n" if $msg !~ /\n$/;
153 File::KDBX::Error::throw($message, %details);
161 my $self = error(@_);
167 File::KDBX::Error::warn($message, %details);
175 return if !($File::KDBX::WARNINGS // 1);
177 my $self = error(@_);
179 # Use die and warn directly instead of warnings::warnif because the latter only provides the stringified
180 # error to the warning signal handler (perl 5.34). Maybe that's a warnings
.pm bug
?
182 if (my $fatal = warnings-
>can('fatal_enabled_at_level')) {
183 my $blame = _find_blame_frame
();
184 die $self if $fatal->($WARNINGS_CATEGORY, $blame);
187 if (my $enabled = warnings-
>can('enabled_at_level')) {
188 my $blame = _find_blame_frame
();
189 warn $self if $enabled->($WARNINGS_CATEGORY, $blame);
191 elsif ($enabled = warnings-
>can('enabled')) {
192 warn $self if $enabled->($WARNINGS_CATEGORY);
204 Importable alias
for L
</warn>.
208 sub alert
{ goto &warn }
210 sub _find_blame_frame
{
213 my ($package) = caller($frame);
215 return $frame - 1 if $package !~ /^\Q$WARNINGS_CATEGORY\E/;