blib/lib/Zuzu/Error.pm

filter: Statementstmt 92.3% 36 / 39 Branchbran 57.1% 8 / 14 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 90.9% 10 / 11 Podpod 100% 5 / 5 totaltotal 78.0% 64 / 82 CC 11 SCAR 11.8
CC
11
Cov
79%
as_string
5.7
_normalize_args
4.1
as_struct
3.3
CRAP 3.2
« blib/lib/Zuzu/Parser.pm ↑ Summary blib/lib/Zuzu/Module/Base64.pm »
1 package Zuzu::Error;
2  
3 78 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 78 our $VERSION = '0.007002';
6  
7 78 ▶use overload '""' => sub { $_[0]->as_string }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
8  
9 78 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 78 has 'message' => ( is => 'rw' );
12 78 has 'file' => ( is => 'rw' );
13 78 has 'line' => ( is => 'rw' );
14 78 has 'code' => ( is => 'rw' );
15  
16 270 ▶sub _normalize_args {
Subroutine _normalize_args: called CC=3 SCAR=14.2
17 270 my ( $class, @args ) = @_;
18  
19 270 ▶ if ( @args == 1 and ref($args[0]) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and ref $args[0] eq "HASH") 0 270 270
Condition33%1/3
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
27000
MC/DC0%0/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1 ref $args[0] eq "HASH"
Truth table33%1/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
ABresult
0X0
100
111
20 0 return %{ $args[0] };
21 }
22  
23 270 return @args;
24 }
25  
26 270 ▶sub _build_with_default_code {
Subroutine _build_with_default_code: called CC=3 SCAR=11.0
27 270 my ( $class, $default_code, @args ) = @_;
28  
29 270 my %ctor = $class->_normalize_args(@args);
30 270 ▶ $ctor{code} = $default_code if !defined $ctor{code} or $ctor{code} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $ctor{"code"} or $ctor{"code"} eq "" 91 179 270
Condition66%2/3
not defined $ctor{"code"} or $ctor{"code"} eq ""
l!l&&r!l&&!r
910179
MC/DC50%1/2
not defined $ctor{"code"} or $ctor{"code"} eq ""
not defined $ctor{"code"} $ctor{"code"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $ctor{"code"}B= $ctor{"code"} eq ""
ABresult
1X1
011
000
31  
32 270 return %ctor;
33 }
34  
35 189 ▶sub new_compile {
Subroutine new_compile: called CC=1 SCAR=0.0
Pod documented
36 189 my ( $class, @args ) = @_;
37  
38 189 require Zuzu::Error::Compile;
39  
40 189 return Zuzu::Error::Compile->new(
41 $class->_build_with_default_code( 'E_COMPILE_GENERIC', @args )
42 );
43 }
44  
45 81 ▶sub new_runtime {
Subroutine new_runtime: called CC=1 SCAR=0.0
Pod documented
46 81 my ( $class, @args ) = @_;
47  
48 81 require Zuzu::Error::Runtime;
49  
50 81 return Zuzu::Error::Runtime->new(
51 $class->_build_with_default_code( 'E_RUNTIME_GENERIC', @args )
52 );
53 }
54  
55 0 ▶sub kind { 'Error' }
Subroutine kind: not called CC=1 SCAR=6.9
Pod documented
56  
57 2 ▶sub as_struct {
Subroutine as_struct: called CC=3 SCAR=12.0
Pod documented
58 2 my ( $self ) = @_;
59  
60 return {
61 2 ▶ kind => $self->kind,
Branch: 2 branches
BranchTrueFalseTotal
defined $self->file ? : 2 0 2
defined $self->line ? : 2 0 2
62 code => $self->code,
63 message => $self->message,
64 file => ( defined $self->file ? $self->file : '<unknown>' ),
65 line => ( defined $self->line ? 0 + $self->line : 0 ),
66 };
67 }
68  
69 164 ▶sub as_string {
Subroutine as_string: called CC=5 SCAR=17.5
Pod documented
70 164 my ($self) = @_;
71  
72 164 ▶ my $loc = defined $self->file ? ( $self->file // '<unknown>' ) : '<unknown>';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->file ? : 164 0 164
Condition50%1/2
$self->file // "<unknown>"
l!l
1640
MC/DC0%0/1
$self->file // "<unknown>"
$self->file
Truth table50%1/2
A
A= $self->file
Aresult
11
00
73 164 ▶ my $ln = defined $self->line ? $self->line : '?';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->line ? : 164 0 164
74 164 ▶ my $code = defined $self->code ? $self->code : 'E_UNKNOWN';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->code ? : 164 0 164
75  
76 164 return sprintf( "%s[%s]: %s at %s:%s", $self->kind, $code, $self->message, $loc, $ln );
77 }
78  
79 =pod
80  
81 =head1 NAME
82  
83 Zuzu::Error - base structured error object for Zuzu
84  
85 =head1 DESCRIPTION
86  
87 Provides shared message/location attributes and string formatting for all
88 Zuzu error classes.
89  
90 Most callers should construct a subclass via C<new_compile> or
91 C<new_runtime>, which return C<Zuzu::Error::Compile> and
92 C<Zuzu::Error::Runtime> objects respectively.
93  
94 =head1 INHERITANCE
95  
96 Inherits from C<Moo::Object>.
97  
98 =head1 ROLES
99  
100 None.
101  
102 =head1 ATTRIBUTES
103  
104 =head2 message
105  
106 Type: B<Str>.
107  
108 Human-readable error message.
109  
110 =head2 file
111  
112 Type: B<Maybe[Str]>.
113  
114 Source filename used for diagnostics.
115  
116 =head2 line
117  
118 Type: B<Int>.
119  
120 1-based source line number used for diagnostics.
121  
122 =head2 code
123  
124 Type: B<Str>.
125  
126 Machine-readable stable error code.
127  
128 =head1 METHODS
129  
130 =head2 new_compile
131  
132 Creates and returns a C<Zuzu::Error::Compile> object.
133  
134 =head2 new_runtime
135  
136 Creates and returns a C<Zuzu::Error::Runtime> object.
137  
138 =head2 kind
139  
140 Returns the error category label.
141  
142 The base implementation returns C<Error>. Subclasses override this.
143  
144 =head2 as_struct
145  
146 Returns machine-readable metadata as a hash reference.
147  
148 =head2 as_string
149  
150 Formats the error as a single diagnostic string.
151  
152 =head1 SEE ALSO
153  
154 L<Zuzu::Error::Compile>, L<Zuzu::Error::Runtime>.
155  
156 =head1 COPYRIGHT AND LICENCE
157  
158 B<< Zuzu::Error >> is copyright Toby Inkster.
159  
160 It is free software; you may redistribute it and/or modify it under
161 the terms of either the Artistic License 1.0 or the GNU General Public
162 License version 2.
163  
164 =cut
165  
166 78 1;
« blib/lib/Zuzu/Parser.pm ↑ Summary blib/lib/Zuzu/Module/Base64.pm »