| 1 | package Text::BibTeX::Error; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | 18 | ▶ | use 5.026; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 18 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||
| 7 | 18 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 18 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||
| 10 | 18 | our $VERSION = '1.00'; | |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | 18 | our @EXPORT_OK = qw(BTERR_NOTIFY BTERR_CONTENT BTERR_LEXWARN BTERR_USAGEWARN | |||||||||||||||||||||||||||||||||
| 13 | BTERR_LEXERR BTERR_SYNTAX BTERR_USAGEERR BTERR_INTERNAL); | ||||||||||||||||||||||||||||||||||
| 14 | 18 | our %EXPORT_TAGS = (errclasses => \@EXPORT_OK); | |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | 18 | ▶ | use constant { | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 17 | 18 | BTERR_NOTIFY => 0, | |||||||||||||||||||||||||||||||||
| 18 | BTERR_CONTENT => 1, | ||||||||||||||||||||||||||||||||||
| 19 | BTERR_LEXWARN => 2, | ||||||||||||||||||||||||||||||||||
| 20 | BTERR_USAGEWARN => 3, | ||||||||||||||||||||||||||||||||||
| 21 | BTERR_LEXERR => 4, | ||||||||||||||||||||||||||||||||||
| 22 | BTERR_SYNTAX => 5, | ||||||||||||||||||||||||||||||||||
| 23 | BTERR_USAGEERR => 6, | ||||||||||||||||||||||||||||||||||
| 24 | BTERR_INTERNAL => 7, | ||||||||||||||||||||||||||||||||||
| 25 | 18 | }; | |||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||
| 27 | 18 | my @class_names = (undef, 'warning', 'warning', 'warning', 'error', | |||||||||||||||||||||||||||||||||
| 28 | 'syntax error', 'fatal error', 'internal error'); | ||||||||||||||||||||||||||||||||||
| 29 | 18 | my @severities = qw(notify warning warning warning error error fatal fatal); | |||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | use overload | ||||||||||||||||||||||||||||||||||
| 32 | 7 | ▶ | '""' => sub { $_[0]->as_string }, | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 33 | 1 | ▶ | 'bool' => sub { 1 }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 34 | 18 | fallback => 1; | |||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | 18 | our $Handler; | |||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 39 | 0 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | $args{errclass} //= BTERR_USAGEERR; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"errclass"} //= 6
MC/DC0%0/1
$args{"errclass"} //= 6
$args{"errclass"}
Truth table0%0/2
A
A= $args{"errclass"}
| |||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | return bless {%args}, ref $class || $class; | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table0%0/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | 2 | ▶ | sub errclass { $_[0]{errclass} } | ||||||||||||||||||||||||||||||||
|
Subroutine
errclass: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 45 | 73 | ▶ | sub class { $class_names[$_[0]{errclass}] } | ||||||||||||||||||||||||||||||||
|
Subroutine
class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 46 | 1 | ▶ | sub severity { $severities[$_[0]{errclass}] } | ||||||||||||||||||||||||||||||||
|
Subroutine
severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 47 | 9 | ▶ | sub message { $_[0]{message} } | ||||||||||||||||||||||||||||||||
|
Subroutine
message: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 48 | 1 | ▶ | sub filename { $_[0]{filename} } | ||||||||||||||||||||||||||||||||
|
Subroutine
filename: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 49 | 2 | ▶ | sub line { $_[0]{line} } | ||||||||||||||||||||||||||||||||
|
Subroutine
line: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | sub item_desc { $_[0]{item_desc} } | ||||||||||||||||||||||||||||||||
|
Subroutine
item_desc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 51 | 1 | ▶ | sub item { $_[0]{item} } | ||||||||||||||||||||||||||||||||
|
Subroutine
item: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 52 | 1 | ▶ | sub key { $_[0]{key} } | ||||||||||||||||||||||||||||||||
|
Subroutine
key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | sub caller_file { $_[0]{caller_file} } | ||||||||||||||||||||||||||||||||
|
Subroutine
caller_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | sub caller_line { $_[0]{caller_line} } | ||||||||||||||||||||||||||||||||
|
Subroutine
caller_line: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | # Is this error serious enough to make an entry fail to parse? | ||||||||||||||||||||||||||||||||||
| 57 | 2 | ▶ | sub is_serious { | ||||||||||||||||||||||||||||||||
|
Subroutine
is_serious: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 58 | 2 | my $c = $_[0]{errclass}; | |||||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | return $c != BTERR_NOTIFY && $c != BTERR_CONTENT && $c != BTERR_LEXWARN; | ||||||||||||||||||||||||||||||||
|
Condition66%4/6
$c != 0 && $c != 1 && $c != 2
$c != 0 and $c != 1
MC/DC33%1/3
$c != 0 && $c != 1 && $c != 2
$c != 0
$c != 1
$c != 2
Truth table50%2/4
A and B && C
A= $c != 0B= $c != 1C= $c != 2
| |||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | 73 | ▶ | sub is_fatal { $_[0]{errclass} >= BTERR_USAGEERR } | ||||||||||||||||||||||||||||||||
|
Subroutine
is_fatal: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||
| 64 | # The same format the C library has always used: | ||||||||||||||||||||||||||||||||||
| 65 | # filename, line N, name M, warning: message | ||||||||||||||||||||||||||||||||||
| 66 | 36 | ▶ | sub as_string { | ||||||||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=9 SCAR=22.3
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 67 | 36 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 68 | 36 | my @where; | |||||||||||||||||||||||||||||||||
| 69 | 36 | ▶ | push @where, $self->{filename} if defined $self->{filename}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 70 | 36 | ▶ | push @where, "line $self->{line}" if $self->{line}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 71 | push @where, "$self->{item_desc} $self->{item}" | ||||||||||||||||||||||||||||||||||
| 72 | 36 | ▶ | if defined $self->{item_desc} && $self->{item}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"item_desc"} and $self->{"item"}
MC/DC50%1/2
defined $self->{"item_desc"} and $self->{"item"}
defined $self->{"item_desc"}
$self->{"item"}
Truth table66%2/3
A and B
A= defined $self->{"item_desc"}B= $self->{"item"}
| |||||||||||||||||||||||||||||||||||
| 73 | 36 | ▶ | push @where, $self->class if defined $self->class; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 74 | 36 | ▶ | my $s = @where ? join(', ', @where) . ": $self->{message}" : $self->{message}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 75 | $s .= " at $self->{caller_file} line $self->{caller_line}.\n" | ||||||||||||||||||||||||||||||||||
| 76 | 36 | ▶ | if $self->is_fatal && defined $self->{caller_file}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->is_fatal and defined $self->{"caller_file"}
MC/DC50%1/2
$self->is_fatal and defined $self->{"caller_file"}
$self->is_fatal
defined $self->{"caller_file"}
Truth table66%2/3
A and B
A= $self->is_fatalB= defined $self->{"caller_file"}
| |||||||||||||||||||||||||||||||||||
| 77 | 36 | return $s; | |||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub throw { die $_[0] } | ||||||||||||||||||||||||||||||||
|
Subroutine
throw: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||
| 83 | # The innermost stack frame outside Text::BibTeX | ||||||||||||||||||||||||||||||||||
| 84 | 85 | ▶ | sub _caller_frame { | ||||||||||||||||||||||||||||||||
|
Subroutine
_caller_frame: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 85 | 85 | my $i = 1; | |||||||||||||||||||||||||||||||||
| 86 | 85 | my @frame; | |||||||||||||||||||||||||||||||||
| 87 | 85 | ▶ | while (@frame = caller($i++)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 88 | 243 | ▶ | last if $frame[0] !~ /^Text::BibTeX(?:::|\z)/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||
| 90 | 85 | return @frame; | |||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||
| 93 | # How should errors be reported to the code that called into Text::BibTeX? | ||||||||||||||||||||||||||||||||||
| 94 | # Warnings are on by default (as they always were), but follow the | ||||||||||||||||||||||||||||||||||
| 95 | # caller's lexical warnings: "no warnings 'Text::BibTeX'" (or plain "no | ||||||||||||||||||||||||||||||||||
| 96 | # warnings") turns them off, and "use warnings FATAL => 'Text::BibTeX'" | ||||||||||||||||||||||||||||||||||
| 97 | # makes them fatal. | ||||||||||||||||||||||||||||||||||
| 98 | 25 | ▶ | sub _policy { | ||||||||||||||||||||||||||||||||
|
Subroutine
_policy: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||
| 99 | 25 | my @frame = _caller_frame(); | |||||||||||||||||||||||||||||||||
| 100 | 25 | ▶ | return 'warn' unless @frame; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||
| 102 | 25 | my $bits = $frame[9]; | |||||||||||||||||||||||||||||||||
| 103 | 25 | ▶ | return 'warn' unless defined $bits; # no lexical warnings | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||
| 105 | 24 | my $off = $warnings::Offsets{'Text::BibTeX'}; | |||||||||||||||||||||||||||||||||
| 106 | 24 | my $all = $warnings::Offsets{'all'}; | |||||||||||||||||||||||||||||||||
| 107 | 24 | ▶ | $off = $all if !defined $off || length($bits) <= ($off >> 3); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $off or length $bits <= $off >> 3
MC/DC50%1/2
not defined $off or length $bits <= $off >> 3
not defined $off
length $bits <= $off >> 3
Truth table66%2/3
not(A) or B
A= not defined $offB= length $bits <= $off >> 3
| |||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||
| 109 | 24 | my $on = vec($bits, $off, 1); | |||||||||||||||||||||||||||||||||
| 110 | 24 | my $fatal = vec($bits, $off + 1, 1); | |||||||||||||||||||||||||||||||||
| 111 | 24 | ▶ | return 'off' unless $on; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 112 | 22 | ▶ | return $fatal ? 'fatal' : 'warn'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||
| 115 | 23 | ▶ | sub _stderr_is_utf8 { | ||||||||||||||||||||||||||||||||
|
Subroutine
_stderr_is_utf8: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 116 | 23 | return scalar grep { /^(?:utf8|encoding)/ } PerlIO::get_layers(*STDERR, output => 1); | |||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||
| 119 | # Called from the XS code with the errors collected during one call into | ||||||||||||||||||||||||||||||||||
| 120 | # the C library, and the entry (if any) they belong to. | ||||||||||||||||||||||||||||||||||
| 121 | 60 | ▶ | sub _dispatch { | ||||||||||||||||||||||||||||||||
|
Subroutine
_dispatch: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||||||||
| 122 | 60 | my ($errors, $entry) = @_; | |||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||
| 124 | 60 | ▶ | my $is_entry = ref $entry && Scalar::Util::reftype($entry) eq 'HASH'; | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $entry && builtin::reftype($entry) eq 'HASH'
MC/DC50%1/2
ref $entry && builtin::reftype($entry) eq 'HASH'
ref $entry
builtin::reftype($entry) eq 'HASH'
Truth table66%2/3
A && B
A= ref $entryB= builtin::reftype($entry) eq 'HASH'
| |||||||||||||||||||||||||||||||||||
| 125 | 60 | my ($file, $line) = (_caller_frame())[1, 2]; | |||||||||||||||||||||||||||||||||
| 126 | 60 | for my $err (@$errors) { | |||||||||||||||||||||||||||||||||
| 127 | 82 | @{$err}{qw(caller_file caller_line)} = ($file, $line); | |||||||||||||||||||||||||||||||||
| 128 | 82 | ▶ | $err->{key} = $entry->{key} if $is_entry && defined $entry->{key}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$is_entry and defined $entry->{"key"}
MC/DC100%2/2
$is_entry and defined $entry->{"key"}
$is_entry
defined $entry->{"key"}
Truth table100%3/3
A and B
A= $is_entryB= defined $entry->{"key"}
| |||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||
| 130 | 60 | ▶ | push @{$entry->{errors}}, @$errors if $is_entry; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||
| 132 | 60 | my $policy; | |||||||||||||||||||||||||||||||||
| 133 | 60 | for my $err (@$errors) { | |||||||||||||||||||||||||||||||||
| 134 | 81 | ▶ | if ($Handler) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 135 | 46 | $Handler->($err); | |||||||||||||||||||||||||||||||||
| 136 | 46 | next; | |||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||
| 138 | 35 | ▶ | die $err if $err->is_fatal; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 139 | 34 | ▶ | $policy //= _policy(); | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$policy //= _policy()
MC/DC0%0/2
$policy //= _policy()
$policy
_policy()
Truth table66%2/3
A //= B
A= $policyB= _policy()
| |||||||||||||||||||||||||||||||||||
| 140 | 34 | ▶ | next if $policy eq 'off'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 141 | 30 | ▶ | die $err if $policy eq 'fatal'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||
| 143 | 29 | my $s = $err->as_string . "\n"; | |||||||||||||||||||||||||||||||||
| 144 | 29 | ▶ | utf8::encode($s) if utf8::is_utf8($s) && !_stderr_is_utf8(); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
utf8::is_utf8($s) and not _stderr_is_utf8()
MC/DC50%1/2
utf8::is_utf8($s) and not _stderr_is_utf8()
utf8::is_utf8($s)
not _stderr_is_utf8()
Truth table66%2/3
A and not(B)
A= utf8::is_utf8($s)B= not _stderr_is_utf8()
| |||||||||||||||||||||||||||||||||||
| 145 | 29 | warn $s; | |||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||
| 147 | 58 | return; | |||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||
| 150 | 18 | 1; | |||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||
| 152 | __END__ | ||||||||||||||||||||||||||||||||||