| 1 | package OpenInteract2::Error; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # $Id: Error.pm,v 1.1 2005/02/25 00:07:19 lachoy Exp $ | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use base qw( Class::Accessor::Fast ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use DateTime::Format::Strptime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use File::Basename qw( dirname ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use File::Path qw( mkpath ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use OpenInteract2::Context qw( CTX ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use OpenInteract2::Exception qw( oi_error ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Template; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | $OpenInteract2::Error::VERSION = sprintf("%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | my $DATE_PATTERN = '%Y-%m-%d %H:%M:%S %3N'; | |||||||||||||||||||||||||||
| 17 | 2 | my ( $PARSER ); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | my %FIELDS = map { $_ => 1 } qw( | |||||||||||||||||||||||||||
| 20 | id file_storage message time category class line | ||||||||||||||||||||||||||||
| 21 | host user_id username session browser referer url | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | 2 | __PACKAGE__->mk_accessors( keys %FIELDS ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 0 | my ( $class, %params ) = @_; | |||||||||||||||||||||||||||
| 27 | 0 | $class->_initialize_parser; | |||||||||||||||||||||||||||
| 28 | 0 | ▶ | if ( $params{file_storage} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | return $class->_read_from_file( $params{file_storage} ); | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 0 | my $self = bless( {}, $class ); | |||||||||||||||||||||||||||
| 32 | 0 | for ( keys %FIELDS ) { | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | $self->$_( $params{ $_ } ) if ( $params{ $_ } ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | 0 | return $self; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub _initialize_parser { | ||||||||||||||||||||||||||
|
Subroutine
_initialize_parser: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | return if ( $PARSER ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | my %params = ( pattern => $DATE_PATTERN ); | |||||||||||||||||||||||||||
| 41 | 0 | ▶ | if ( CTX ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | $params{time_zone} = CTX->timezone_object; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 0 | $PARSER = DateTime::Format::Strptime->new( %params ); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub _read_from_file { | ||||||||||||||||||||||||||
|
Subroutine
_read_from_file: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 48 | 0 | my ( $class, $file ) = @_; | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | unless ( -f $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | oi_error "Cannot read serialized error from file '$file' -- ", | |||||||||||||||||||||||||||
| 51 | "file does not exist."; | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | open( IN, '<', $file ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | || oi_error "Cannot read serialized error from '$file' -- $!"; | ||||||||||||||||||||||||||||
| 55 | 0 | my $self = $class->new(); | |||||||||||||||||||||||||||
| 56 | 0 | ▶ | while ( <IN> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | chomp; | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | if ( /^(\w+)\s+:=\s+(.*)$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | my $prop = lc $1; | |||||||||||||||||||||||||||
| 60 | 0 | my $val = $2; | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | next unless ( $prop eq 'user' || $FIELDS{ $prop } ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$prop eq "user" or $FIELDS{$prop}
MC/DC0%0/2
$prop eq "user" or $FIELDS{$prop}
$prop eq "user"
$FIELDS{$prop}
Truth table0%0/3
A or B
A= $prop eq "user"B= $FIELDS{$prop}
| |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | if ( $prop eq 'time' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 63 | 0 | $self->time( $PARSER->parse_datetime( $val ) ); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | elsif ( $prop eq 'user' ) { | ||||||||||||||||||||||||||||
| 66 | 0 | my ( $name, $id ) = split /\s+\|\s+/, $val, 2; | |||||||||||||||||||||||||||
| 67 | 0 | $self->username( $name ); | |||||||||||||||||||||||||||
| 68 | 0 | $self->user_id( $id ); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | else { | ||||||||||||||||||||||||||||
| 71 | 0 | $self->$prop( $val ); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 0 | close( IN ); | |||||||||||||||||||||||||||
| 76 | 0 | $self->file_storage( $file ); | |||||||||||||||||||||||||||
| 77 | 0 | return $self; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub save { | ||||||||||||||||||||||||||
|
Subroutine
save: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 0 | my ( $self, $file ) = @_; | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | unless ( $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | oi_error "Parameter 'file' must be defined to store an error."; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if ( -f $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | oi_error "Cannot overwrite existing file '$file' with error contents"; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 0 | eval { | |||||||||||||||||||||||||||
| 89 | 0 | mkpath( dirname( $file ) ) | |||||||||||||||||||||||||||
| 90 | }; | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | oi_error "Cannot create directories for '$file': $@"; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 0 | my $error_template = $self->_error_template(); | |||||||||||||||||||||||||||
| 95 | 0 | my $template = Template->new(); | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | $template->process( \$error_template, { e => $self }, $file ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | || oi_error "Cannot process error template to '$file': ", $template->error(); | ||||||||||||||||||||||||||||
| 98 | 0 | $self->file_storage( $file ); | |||||||||||||||||||||||||||
| 99 | 0 | return $file; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub _error_template { | ||||||||||||||||||||||||||
|
Subroutine
_error_template: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 103 | 0 | return <<ERROR; | |||||||||||||||||||||||||||
| 104 | time := [% e.time.strftime( '$DATE_PATTERN' ) %] | ||||||||||||||||||||||||||||
| 105 | message := [% e.message %] | ||||||||||||||||||||||||||||
| 106 | url := [% e.url %] | ||||||||||||||||||||||||||||
| 107 | category := [% e.category %] | ||||||||||||||||||||||||||||
| 108 | class := [% e.class %] | ||||||||||||||||||||||||||||
| 109 | line := [% e.line %] | ||||||||||||||||||||||||||||
| 110 | user := [% e.username %] | [% e.user_id %] | ||||||||||||||||||||||||||||
| 111 | session := [% e.session %] | ||||||||||||||||||||||||||||
| 112 | host := [% e.host %] | ||||||||||||||||||||||||||||
| 113 | browser := [% e.browser %] | ||||||||||||||||||||||||||||
| 114 | referer := [% e.referer %] | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | ERROR | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 2 | 1; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | __END__ | ||||||||||||||||||||||||||||