| 1 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | package Exception::Reporter::Dumpable::File; | ||||||||||||||||||||||||||||
| 4 | # ABSTRACT: a dumpable object for a file on disk | ||||||||||||||||||||||||||||
| 5 | 1 | $Exception::Reporter::Dumpable::File::VERSION = '0.014'; | |||||||||||||||||||||||||||
| 6 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 7 | #pod | ||||||||||||||||||||||||||||
| 8 | #pod $reporter->report_exception( | ||||||||||||||||||||||||||||
| 9 | #pod [ | ||||||||||||||||||||||||||||
| 10 | #pod ..., | ||||||||||||||||||||||||||||
| 11 | #pod [ import_file => Exception::Reporter::Dumpable::File->new( | ||||||||||||||||||||||||||||
| 12 | #pod $path_to_file, | ||||||||||||||||||||||||||||
| 13 | #pod { mimetype => 'text/csv', charset => 'us-ascii' }, | ||||||||||||||||||||||||||||
| 14 | #pod ) ], | ||||||||||||||||||||||||||||
| 15 | #pod ] | ||||||||||||||||||||||||||||
| 16 | #pod ); | ||||||||||||||||||||||||||||
| 17 | #pod | ||||||||||||||||||||||||||||
| 18 | #pod This class exists to provide a simple way to tell Exception::Reporter to | ||||||||||||||||||||||||||||
| 19 | #pod include a file from disk. To make this useful, you should also include | ||||||||||||||||||||||||||||
| 20 | #pod L<Exception::Reporter::Summarizer::File> in your summarizers. | ||||||||||||||||||||||||||||
| 21 | #pod | ||||||||||||||||||||||||||||
| 22 | #pod Right now, file content is read as soon as the file is constructed. This may | ||||||||||||||||||||||||||||
| 23 | #pod change in the future. | ||||||||||||||||||||||||||||
| 24 | #pod | ||||||||||||||||||||||||||||
| 25 | #pod =cut | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | sub _err_msg { | ||||||||||||||||||||||||||||
| 28 | 1 | ▶ | my ($class, $path, $msg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_err_msg: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 1 | return "(file at <$path> was requested for dumping, but $msg)"; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | #pod =method new | ||||||||||||||||||||||||||||
| 33 | #pod | ||||||||||||||||||||||||||||
| 34 | #pod my $file_dumpable = Exception::Reporter::Dumpable::File->new( | ||||||||||||||||||||||||||||
| 35 | #pod $path, | ||||||||||||||||||||||||||||
| 36 | #pod \%arg, | ||||||||||||||||||||||||||||
| 37 | #pod ); | ||||||||||||||||||||||||||||
| 38 | #pod | ||||||||||||||||||||||||||||
| 39 | #pod Useful arguments are: | ||||||||||||||||||||||||||||
| 40 | #pod | ||||||||||||||||||||||||||||
| 41 | #pod mimetype - defaults to a guess by extension or application/octet-stream | ||||||||||||||||||||||||||||
| 42 | #pod charset - defaults to utf-8 for text, undef otherwise | ||||||||||||||||||||||||||||
| 43 | #pod max_size - the maximum size to include; if the file is larger, a placeholder | ||||||||||||||||||||||||||||
| 44 | #pod will be included instead | ||||||||||||||||||||||||||||
| 45 | #pod | ||||||||||||||||||||||||||||
| 46 | #pod If the file object can't be constructed, B<the method does not die>. This to | ||||||||||||||||||||||||||||
| 47 | #pod avoid requiring exception handling in your exception handling. Instead, C<new> | ||||||||||||||||||||||||||||
| 48 | #pod I<will return a string> which will then be summarized as any other string. | ||||||||||||||||||||||||||||
| 49 | #pod | ||||||||||||||||||||||||||||
| 50 | #pod Maybe this will change in the future, and the file summarizer will know how to | ||||||||||||||||||||||||||||
| 51 | #pod expect File::Error objects, or something like that. | ||||||||||||||||||||||||||||
| 52 | #pod | ||||||||||||||||||||||||||||
| 53 | #pod =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub new { | ||||||||||||||||||||||||||||
| 56 | 2 | ▶ | my ($class, $path, $arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=13 SCAR=30.3
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 2 | ▶ | $arg ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$arg ||= {}
MC/DC100%1/1
$arg ||= {}
$arg
Truth table100%2/2
A
A= $arg
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 2 | ▶ | return $class->_err_msg($path, 'does not exist') unless -e $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | ▶ | my $realpath = -l $path ? readlink $path : $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 1 | ▶ | return $class->_err_msg($path, 'is not a normal file') unless -f $realpath; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | return $class->_err_msg($path, "can't be read") unless -r $realpath; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | ▶ | if ($arg->{max_size}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | my $size = -s $realpath; | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ($size > $arg->{max_size}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | return $class->_err_msg( | |||||||||||||||||||||||||||
| 71 | $path, | ||||||||||||||||||||||||||||
| 72 | "its size $size " . "exceeds maximum allowed size $arg->{max_size}" | ||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 1 | my $guts = { path => $path }; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | $guts->{mimetype} = $arg->{mimetype} | ||||||||||||||||||||||||||||
| 80 | 1 | ▶ | || $class->_mimetype_from_filename($path) | ||||||||||||||||||||||||||
|
Condition40%2/5
$arg->{'mimetype'} || $class->_mimetype_from_filename($path) || 'application/octet-stream'
$arg->{"mimetype"} or $class->_mimetype_from_filename($path)
MC/DC0%0/2
$arg->{'mimetype'} || $class->_mimetype_from_filename($path) || 'application/octet-stream'
$arg->{"mimetype"}
$class->_mimetype_from_filename($path)
Truth table33%1/3
A or B
A= $arg->{"mimetype"}B= $class->_mimetype_from_filename($path)
| |||||||||||||||||||||||||||||
| 81 | || 'application/octet-stream'; | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | $guts->{charset} = $arg->{charset} | ||||||||||||||||||||||||||||
| 84 | 1 | ▶ | || $guts->{mimetype} =~ m{\Atext/} ? 'utf-8' : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$arg->{"charset"} or $guts->{"mimetype"} =~ m[\Atext/]
MC/DC0%0/2
$arg->{"charset"} or $guts->{"mimetype"} =~ m[\Atext/]
$arg->{"charset"}
$guts->{"mimetype"} =~ m[\Atext/]
Truth table33%1/3
A or B
A= $arg->{"charset"}B= $guts->{"mimetype"} =~ m[\Atext/]
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | ▶ | open my $fh, '<', $path | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | or return $class->_err_msg("there was an error reading it: $!"); | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | my $contents = do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | $guts->{contents_ref} = \$contents; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 1 | bless $guts => $class; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | ▶ | sub path { $_[0]->{path} } | ||||||||||||||||||||||||||
|
Subroutine
path: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 97 | 2 | ▶ | sub mimetype { $_[0]->{mimetype} } | ||||||||||||||||||||||||||
|
Subroutine
mimetype: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 98 | 4 | ▶ | sub charset { $_[0]->{charset} } | ||||||||||||||||||||||||||
|
Subroutine
charset: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 99 | 2 | ▶ | sub contents_ref { $_[0]->{contents_ref} } | ||||||||||||||||||||||||||
|
Subroutine
contents_ref: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # replace with MIME::Type or something -- rjbs, 2012-07-03 | ||||||||||||||||||||||||||||
| 102 | 1 | my %LOOKUP = ( | |||||||||||||||||||||||||||
| 103 | txt => 'text/plain', | ||||||||||||||||||||||||||||
| 104 | html => 'text/html', | ||||||||||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | sub _mimetype_from_filename { | ||||||||||||||||||||||||||||
| 108 | 0 | ▶ | my ($class, $filename) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_mimetype_from_filename: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | my ($extension) = $filename =~ m{\.(.+?)\z}; | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | return unless $extension; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | return $LOOKUP{ $extension }; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 1 | 1; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | __END__ | ||||||||||||||||||||||||||||