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 $class = @_ && $_[0] eq __PACKAGE__ ? shift : undef;
82 my $self = (blessed($_[0]) && $_[0]->isa('File
::KDBX
::Error
'))
86 : __PACKAGE__->new(@_);
92 \%details = $error->details;
94 Get the error details.
101 my $details = $self->{details} //= {};
102 @$details{keys %args} = values %args;
106 sub errno { $_[0]->{errno} }
108 sub previous { $_[0]->{previous} }
110 sub trace { $_[0]->{trace} // [] }
112 sub type { $_[0]->details->{type} // '' }
116 $message = $error->to_string;
121 This does not contain a stack trace, but you can set the C<DEBUG> environment
122 variable to truthy to stringify the whole error object.
126 sub _cmp { "$_[0]" cmp "$_[1]" }
135 my $msg = "$self->{trace}[0]";
136 $msg .= '.' if $msg !~ /[\.\!\?]$/; # Why does this cause infinite recursion on some perls?
137 # $msg .= '.' if $msg !~ /(?:\.|!|\?)$/;
139 require Data::Dumper;
140 local $Data::Dumper::Indent = 1;
141 local $Data::Dumper::Quotekeys = 0;
142 local $Data::Dumper::Sortkeys = 1;
143 local $Data::Dumper::Terse = 1;
144 local $Data::Dumper::Trailingcomma = 1;
145 local $Data::Dumper::Useqq = 1;
146 $msg .= "\n" . Data::Dumper::Dumper $self;
148 $msg .= "\n" if $msg !~ /\n$/;
154 File::KDBX::Error::throw($message, %details);
162 my $self = error(@_);
168 File::KDBX::Error::warn($message, %details);
176 return if !($File::KDBX::WARNINGS // 1);
178 my $self = error(@_);
180 # Use die and warn directly instead of warnings::warnif because the latter only provides the stringified
181 # error to the warning signal handler (perl 5.34). Maybe that's a warnings
.pm bug
?
183 if (my $fatal = warnings-
>can('fatal_enabled_at_level')) {
184 my $blame = _find_blame_frame
();
185 die $self if $fatal->($WARNINGS_CATEGORY, $blame);
188 if (my $enabled = warnings-
>can('enabled_at_level')) {
189 my $blame = _find_blame_frame
();
190 warn $self if $enabled->($WARNINGS_CATEGORY, $blame);
192 elsif ($enabled = warnings-
>can('enabled')) {
193 warn $self if $enabled->($WARNINGS_CATEGORY);
205 Importable alias
for L
</warn>.
209 sub alert
{ goto &warn }
211 sub _find_blame_frame
{
214 my ($package) = caller($frame);
216 return $frame - 1 if $package !~ /^\Q$WARNINGS_CATEGORY\E/;