| 1 | package File::KDBX::Error; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Represents something bad that happened | ||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 65 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 65 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 65 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 65 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 65 | ▶ | use Scalar::Util qw(blessed looks_like_number); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 65 | ▶ | use namespace::clean -except => 'import'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 65 | our $VERSION = '0.904'; # VERSION | |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 65 | our @EXPORT = qw(alert error throw); | |||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 65 | my $WARNINGS_CATEGORY; | |||||||||||||||||||||||||||||||
| 17 | BEGIN { | ||||||||||||||||||||||||||||||||
| 18 | 65 | ▶ | $WARNINGS_CATEGORY = 'File::KDBX'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=8 SCAR=24.5
| |||||||||||||||||||||||||||||||||
| 19 | 65 | ▶ | if (warnings->can('register_categories')) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 20 | 65 | warnings::register_categories($WARNINGS_CATEGORY); | |||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||
| 22 | else { | ||||||||||||||||||||||||||||||||
| 23 | 0 | eval qq{package $WARNINGS_CATEGORY; use warnings::register; 1}; ## no critic ProhibitStringyEval | |||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 65 | my $debug = $ENV{DEBUG}; | |||||||||||||||||||||||||||||||
| 27 | 65 | ▶ | $debug = looks_like_number($debug) ? (0 + $debug) : ($debug ? 1 : 0); | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 28 | *_DEBUG = $debug == 1 ? sub() { 1 } : | ||||||||||||||||||||||||||||||||
| 29 | $debug == 2 ? sub() { 2 } : | ||||||||||||||||||||||||||||||||
| 30 | $debug == 3 ? sub() { 3 } : | ||||||||||||||||||||||||||||||||
| 31 | 65 | ▶ | $debug == 4 ? sub() { 4 } : sub() { 0 }; | ||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 65 | ▶ | use overload '""' => 'to_string', cmp => '_cmp'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | sub new { | ||||||||||||||||||||||||||||||||
| 38 | 375 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 39 | 375 | ▶ | my %args = @_ % 2 == 0 ? @_ : (_error => shift, @_); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 375 | my $error = delete $args{_error}; | |||||||||||||||||||||||||||||||
| 42 | 375 | my $e = $error; | |||||||||||||||||||||||||||||||
| 43 | 375 | $e =~ s/ at \H+ line \d+.*//g; | |||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | my $self = bless { | ||||||||||||||||||||||||||||||||
| 46 | details => \%args, | ||||||||||||||||||||||||||||||||
| 47 | error => $e // 'Something happened', | ||||||||||||||||||||||||||||||||
| 48 | errno => $!, | ||||||||||||||||||||||||||||||||
| 49 | previous => $@, | ||||||||||||||||||||||||||||||||
| 50 | 375 | ▶ | trace => do { | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$e // "Something happened"
MC/DC0%0/1
$e // "Something happened"
$e
Truth table50%1/2
A
A= $e
| |||||||||||||||||||||||||||||||||
| 51 | 375 | require Carp; | |||||||||||||||||||||||||||||||
| 52 | 375 | local $Carp::CarpInternal{''.__PACKAGE__} = 1; | |||||||||||||||||||||||||||||||
| 53 | 375 | ▶ | my $mess = $error =~ /at \H+ line \d+/ ? $error : Carp::longmess($error); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 54 | 375 | ▶ | [map { /^\h*(.*?)\.?$/ ? $1 : $_ } split(/\n/, $mess)]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | }, | ||||||||||||||||||||||||||||||||
| 56 | }, $class; | ||||||||||||||||||||||||||||||||
| 57 | 375 | chomp $self->{error}; | |||||||||||||||||||||||||||||||
| 58 | 375 | return $self; | |||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | sub error { | ||||||||||||||||||||||||||||||||
| 63 | 377 | ▶ | my $class = @_ && $_[0] eq __PACKAGE__ ? shift : undef; | ||||||||||||||||||||||||||||||
|
Subroutine
error: called CC=6 SCAR=18.1
Pod
documented
Branch: 1 branch
Condition66%2/3
@_ and $_[0] eq __PACKAGE__
MC/DC50%1/2
@_ and $_[0] eq __PACKAGE__
@_
$_[0] eq __PACKAGE__
Truth table66%2/3
A and B
A= @_B= $_[0] eq __PACKAGE__
| |||||||||||||||||||||||||||||||||
| 64 | 377 | ▶ | my $self = (blessed($_[0]) && $_[0]->isa('File::KDBX::Error')) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
builtin::blessed($_[0]) and $_[0]->isa("File::KDBX::Error")
MC/DC50%1/2
builtin::blessed($_[0]) and $_[0]->isa("File::KDBX::Error")
builtin::blessed($_[0])
$_[0]->isa("File::KDBX::Error")
Truth table66%2/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("File::KDBX::Error")
| |||||||||||||||||||||||||||||||||
| 65 | ? shift | ||||||||||||||||||||||||||||||||
| 66 | : $class | ||||||||||||||||||||||||||||||||
| 67 | ? $class->new(@_) | ||||||||||||||||||||||||||||||||
| 68 | : __PACKAGE__->new(@_); | ||||||||||||||||||||||||||||||||
| 69 | 377 | return $self; | |||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | sub details { | ||||||||||||||||||||||||||||||||
| 74 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
details: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 75 | 2 | my %args = @_; | |||||||||||||||||||||||||||||||
| 76 | 2 | ▶ | my $details = $self->{details} //= {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"details"} //= {}
MC/DC0%0/1
$self->{"details"} //= {}
$self->{"details"}
Truth table50%1/2
A
A= $self->{"details"}
| |||||||||||||||||||||||||||||||||
| 77 | 2 | @$details{keys %args} = values %args; | |||||||||||||||||||||||||||||||
| 78 | 2 | return $details; | |||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 2 | ▶ | sub errno { $_[0]->{errno} } | ||||||||||||||||||||||||||||||
|
Subroutine
errno: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 84 | 2 | ▶ | sub previous { $_[0]->{previous} } | ||||||||||||||||||||||||||||||
|
Subroutine
previous: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 85 | 6 | ▶ | sub trace { $_[0]->{trace} // [] } | ||||||||||||||||||||||||||||||
|
Subroutine
trace: called CC=2 SCAR=7.6
Pod
documented
Condition50%1/2
$_[0]{"trace"} // []
MC/DC0%0/1
$_[0]{"trace"} // []
$_[0]{"trace"}
Truth table50%1/2
A
A= $_[0]{"trace"}
| |||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub type { $_[0]->details->{type} // '' } | ||||||||||||||||||||||||||||||
|
Subroutine
type: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/2
$_[0]->details->{"type"} // ""
MC/DC0%0/1
$_[0]->details->{"type"} // ""
$_[0]->details->{"type"}
Truth table0%0/2
A
A= $_[0]->details->{"type"}
| |||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | 5 | ▶ | sub _cmp { "$_[0]" cmp "$_[1]" } | ||||||||||||||||||||||||||||||
|
Subroutine
_cmp: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | sub to_string { | ||||||||||||||||||||||||||||||||
| 92 | 114 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
to_string: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 93 | 114 | my $msg = "$self->{trace}[0]"; | |||||||||||||||||||||||||||||||
| 94 | 114 | ▶ | $msg .= '.' if $msg !~ /[\.\!\?]$/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 95 | 114 | if (2 <= _DEBUG) { | |||||||||||||||||||||||||||||||
| 96 | require Data::Dumper; | ||||||||||||||||||||||||||||||||
| 97 | local $Data::Dumper::Indent = 1; | ||||||||||||||||||||||||||||||||
| 98 | local $Data::Dumper::Quotekeys = 0; | ||||||||||||||||||||||||||||||||
| 99 | local $Data::Dumper::Sortkeys = 1; | ||||||||||||||||||||||||||||||||
| 100 | local $Data::Dumper::Terse = 1; | ||||||||||||||||||||||||||||||||
| 101 | local $Data::Dumper::Trailingcomma = 1; | ||||||||||||||||||||||||||||||||
| 102 | local $Data::Dumper::Useqq = 1; | ||||||||||||||||||||||||||||||||
| 103 | $msg .= "\n" . Data::Dumper::Dumper $self; | ||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||
| 105 | 114 | ▶ | $msg .= "\n" if $msg !~ /\n$/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 106 | 114 | return $msg; | |||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | sub throw { | ||||||||||||||||||||||||||||||||
| 111 | 360 | ▶ | my $self = error(@_); | ||||||||||||||||||||||||||||||
|
Subroutine
throw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 112 | 360 | die $self; | |||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | sub warn { | ||||||||||||||||||||||||||||||||
| 117 | 16 | ▶ | return if !($File::KDBX::WARNINGS // 1); | ||||||||||||||||||||||||||||||
|
Subroutine
warn: called CC=9 SCAR=24.1
Pod
documented
Branch: 1 branch
Condition100%2/2
$File::KDBX::WARNINGS // 1
MC/DC100%1/1
$File::KDBX::WARNINGS // 1
$File::KDBX::WARNINGS
Truth table100%2/2
A
A= $File::KDBX::WARNINGS
| |||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | 15 | my $self = error(@_); | |||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | # Use die and warn directly instead of warnings::warnif because the latter only provides the stringified | ||||||||||||||||||||||||||||||||
| 122 | # error to the warning signal handler (perl 5.34). Maybe that's a warnings.pm bug? | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | 15 | ▶ | if (my $fatal = warnings->can('fatal_enabled_at_level')) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 125 | 15 | my $blame = _find_blame_frame(); | |||||||||||||||||||||||||||||||
| 126 | 15 | ▶ | die $self if $fatal->($WARNINGS_CATEGORY, $blame); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 14 | ▶ | if (my $enabled = warnings->can('enabled_at_level')) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 130 | 14 | my $blame = _find_blame_frame(); | |||||||||||||||||||||||||||||||
| 131 | 14 | ▶ | warn $self if $enabled->($WARNINGS_CATEGORY, $blame); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | elsif ($enabled = warnings->can('enabled')) { | ||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | warn $self if $enabled->($WARNINGS_CATEGORY); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||
| 136 | else { | ||||||||||||||||||||||||||||||||
| 137 | 0 | warn $self; | |||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||
| 139 | 14 | return $self; | |||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | 15 | ▶ | sub alert { goto &warn } | ||||||||||||||||||||||||||||||
|
Subroutine
alert: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | sub _find_blame_frame { | ||||||||||||||||||||||||||||||||
| 146 | 29 | ▶ | my $frame = 1; | ||||||||||||||||||||||||||||||
|
Subroutine
_find_blame_frame: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||
| 147 | 29 | while (1) { | |||||||||||||||||||||||||||||||
| 148 | 413 | my ($package) = caller($frame); | |||||||||||||||||||||||||||||||
| 149 | 413 | ▶ | last if !$package; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 150 | 413 | ▶ | return $frame - 1 if $package !~ /^\Q$WARNINGS_CATEGORY\E/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 151 | 384 | $frame++; | |||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||
| 153 | 0 | return 0; | |||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | 65 | 1; | |||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||||||||||||