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;
111 sub errno { $_[0]->{errno} }
113 sub previous { $_[0]->{previous} }
115 sub trace { $_[0]->{trace} // [] }
117 sub type { $_[0]->details->{type} // '' }
121 $message = $error->to_string;
126 This does not contain a stack trace, but you can set the C<DEBUG> environment
127 variable to truthy to stringify the whole error object.
131 sub _cmp { "$_[0]" cmp "$_[1]" }
140 my $msg = "$self->{trace}[0]";
141 $msg .= '.' if $msg !~ /[\.\!\?]$/; # Why does this cause infinite recursion on some perls?
142 # $msg .= '.' if $msg !~ /(?:\.|!|\?)$/;
144 require Data::Dumper;
145 local $Data::Dumper::Indent = 1;
146 local $Data::Dumper::Quotekeys = 0;
147 local $Data::Dumper::Sortkeys = 1;
148 local $Data::Dumper::Terse = 1;
149 local $Data::Dumper::Trailingcomma = 1;
150 local $Data::Dumper::Useqq = 1;
151 $msg .= "\n" . Data::Dumper::Dumper $self;
153 $msg .= "\n" if $msg !~ /\n$/;
159 File::KDBX::Error::throw($message, %details);
167 my $self = error(@_);
173 File::KDBX::Error::warn($message, %details);
181 return if !($File::KDBX::WARNINGS // 1);
183 my $self = error(@_);
185 # Use die and warn directly instead of warnings::warnif because the latter only provides the stringified
186 # error to the warning signal handler (perl 5.34). Maybe that's a warnings
.pm bug
?
188 if (my $fatal = warnings-
>can('fatal_enabled_at_level')) {
189 my $blame = _find_blame_frame
();
190 die $self if $fatal->($WARNINGS_CATEGORY, $blame);
193 if (my $enabled = warnings-
>can('enabled_at_level')) {
194 my $blame = _find_blame_frame
();
195 warn $self if $enabled->($WARNINGS_CATEGORY, $blame);
197 elsif ($enabled = warnings-
>can('enabled')) {
198 warn $self if $enabled->($WARNINGS_CATEGORY);
210 Importable alias
for L
</warn>.
214 sub alert
{ goto &warn }
216 sub _find_blame_frame
{
219 my ($package) = caller($frame);
221 return $frame - 1 if $package !~ /^\Q$WARNINGS_CATEGORY\E/;