File Coverage

lib/Exception/System.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition 2 2 100.0
subroutine 6 6 100.0
pod n/a
total 29 29 100.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -c
2              
3             package Exception::System;
4 1     1   2914 use 5.006;
  1         4  
  1         208  
5             our $VERSION = 0.11;
6              
7             =head1 NAME
8              
9             Exception::System - The exception class for system or library calls
10              
11             =head1 SYNOPSIS
12              
13             # The simplest usage
14             use Exception::Base 'Exception::System';
15             open my $file, "/notfound"
16             or Exception::System->throw(message=>"Can not open file");
17              
18             # The Exception::System class can be a base class for others
19             #
20             # Loaded automatically if used as Exception::Base's argument
21             use Exception::Base,
22             'Exception::System',
23             'Exception::File' => {
24             isa => 'Exception::System',
25             has => 'filename',
26             string_attributes => [ 'message', 'errstr', 'filename' ],
27             };
28              
29             eval {
30             my $filename = "/notfound";
31             open my $fh, $filename
32             or Exception::File->throw(
33             message=>"Can not open file",
34             filename=>$filename,
35             );
36             };
37             if ($@) {
38             my $e = Exception::Base->catch;
39             if ($e->isa('Exception::File')) { warn "File error:".$e->errstr; }
40             if ($e->matches({errname=>'ENOENT'})) { warn "Caught not found error"; }
41             }
42              
43             =head1 DESCRIPTION
44              
45             This class extends standard L with handling system or library
46             errors. The additional attributes of the exception object are filled on throw
47             and contain the error message and error codes.
48              
49             =for readme stop
50              
51             =cut
52              
53              
54 1     1   7 use strict;
  1         2  
  1         47  
55 1     1   32 use warnings;
  1         3  
  1         84  
56              
57              
58             # Extend Exception::Base class
59             use Exception::Base 0.20
60 1         18 'Exception::System' => {
61             has => { ro => [ 'errstr', 'errstros', 'errno', 'errname' ] },
62             message => 'Unknown system exception',
63             verbosity => 3,
64             string_attributes => [ 'message', 'errstr' ],
65             numeric_attribute => 'errno',
66 1     1   6 };
  1         26  
67              
68              
69             # Use ERRNO hash
70 1     1   728 use Errno ();
  1         2  
  1         233  
71              
72              
73             # Map for errno -> errname (choose the shortest errname string for the same errno number)
74             my %Errname = map { Errno->$_ => $_ }
75             sort { length $b <=> length $a }
76             sort
77             keys (%!);
78              
79              
80             # Collect system data
81             sub _collect_system_data {
82 9     9   11264 my $self = shift;
83              
84 9         111 $self->{errstr} = "$!"; # string context
85 9         25 $self->{errstros} = $^E;
86 9         22 $self->{errno} = 0+$!; # numeric context
87 9   100     59 $self->{errname} = $Errname{ $self->{errno} } || '';
88              
89 9         39 return $self->SUPER::_collect_system_data(@_);
90             };
91              
92              
93             1;
94              
95              
96             __END__