CommonJS/Exception.pm untested

filter: Statementstmt 0.0% 0 / 45 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 103 CC 8 SCAR 34.5
CC
8
Cov
0%
new_from_string
72.0
new
2.0
stack
2.0
CRAP 31.5
1 package JavaScript::V8::CommonJS::Exception;
2  
3 use strict;
4 use warnings;
5 use overload '""' => 'to_string';
6  
7 sub new {
8 my $class = shift;
9 my $args = shift || {};
10  
11 bless {
12 stack => $args->{stack} || [],
13 message => $args->{message} || '',
14 source => $args->{source} || '?',
15 line => $args->{line} // '?',
16 column => $args->{column} // '?',
17 }, $class;
18 }
19  
20 sub new_from_string {
21 my ($class, $string) = @_;
22  
23 # single line
24 if ($string =~ /^(.*) at (.*):(\d+)$/) {
25 return $class->new({ message => $1, source => $2, line => $3 });
26 }
27  
28 # stacktrace
29 # die "invalid stacktrace string" unless $string =~ /\nat /;
30 my @lines = split /\n/, $string;
31  
32 # ReferenceError: foo is not defined
33 # at eval (eval at <anonymous> (/home/cafe/workspace/JavaScript-V8-CommonJS/share/require.js:45:80), /home/cafe/workspace/JavaScript-V8-CommonJS/t/modules/notStrict.js:2:5)
34 # at /home/cafe/workspace/JavaScript-V8-CommonJS/share/require.js:45:75
35 # at global.require (/home/cafe/workspace/JavaScript-V8-CommonJS/share/require.js:45:96)
36 # at eval (eval at <anonymous> (/home/cafe/workspace/JavaScript-V8-CommonJS/share/require.js:45:80), /home/cafe/workspace/JavaScript-V8-CommonJS/t/modules/exception.js:3:17)
37 # at /home/cafe/workspace/JavaScript-V8-CommonJS/share/require.js:45:75
38 # at global.require (/home/cafe/workspace/JavaScript-V8-CommonJS/share/require.js:45:96)
39 # at test_script:1:6 at test_script:1
40  
41  
42 my $message = shift @lines;
43  
44 # HACK compile time errors report corret source at message line, ex:
45 # Error: ReferenceError: foo is not defined at /home/cafe/workspace/JavaScript-V8-CommonJS/t/modules/notStrict.js:2 at undefined:1
46 if ($message =~ /^Error: (.*)( at \/.*) at undefined:1$/) {
47 $message = $1;
48 unshift @lines, $2;
49 }
50 my @stack;
51 foreach (@lines) {
52 my ($source, $line, $col);
53  
54 $_ =~ s/^\s+//;
55  
56 # at eval (eval at <anonymous> (/home/cafe/workspace/JavaScript-V8-CommonJS/share/require.js:45:80), /home/cafe/workspace/JavaScript-V8-CommonJS/t/modules/notStrict.js:2:5)
57 if (($source, $line, $col) = $_ =~ /^at .*?, (.+):(\d+):(\d+)\)$/) { }
58  
59 # at /home/cafe/workspace/JavaScript-V8-CommonJS/share/require.js:45:75
60 elsif (($source, $line, $col) = $_ =~ /^at (.+):(\d+):(\d+)$/) { }
61  
62 # at global.require (/home/cafe/workspace/JavaScript-V8-CommonJS/share/require.js:45:96)
63 elsif (($source, $line, $col) = $_ =~ /^at \S+ \((.+):(\d+):(\d+)\)$/) { }
64  
65 # at /home/cafe/workspace/JavaScript-V8-CommonJS/share/require.js:45
66 elsif (($source, $line) = $_ =~ /^at (\S+):(\d+)$/) { }
67  
68 # at test_script:1:6 at test_script:1
69 elsif (($source, $line, $col) = $_ =~ /^at (\S+):(\d+):(\d+) at \S+:\d+$/) { }
70  
71 # unknown line format
72 else {
73 warn "Couldn't parse javascript stacktrace line ($_)";
74 }
75  
76 # push parsed line
77 push @stack, {
78 source => $source,
79 line => $line,
80 column => $col
81 }
82 }
83  
84 $class->new({
85 stack => \@stack,
86 message => $message,
87 %{ $stack[0] }
88 })
89  
90 }
91  
92  
93  
94 sub stack { shift->{stack} }
95 sub message { shift->{message} }
96 sub source { shift->{source} }
97 sub line { shift->{line} }
98 sub column { shift->{column} }
99  
100 sub is_time_limit {
101 my $self = shift;
102 $self->{message} eq 'null' && $self->{source} eq 'eval' && $self->{line} eq '0';
103 }
104  
105 sub to_string {
106 my $self = shift;
107 sprintf "[JavaScript Exception] %s at %s:%s:%s", $self->message, $self->source, $self->line, $self->column;
108 }
109  
110  
111 1;