OpenInteract/Error/Main.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 116 CC 5 SCAR 17.1
CC
5
Cov
0%
require_error_handlers
12.0
catch
6.0
save_error
6.0
CRAP 5.5
1 package OpenInteract::Error::Main;
2  
3 # $Id: Main.pm,v 1.7 2002/01/02 02:43:53 lachoy Exp $
4  
5 use strict;
6 require Exporter;
7 use vars qw( $ERROR_HOLD );
8 use Carp qw( carp );
9 use Data::Dumper qw( Dumper );
10  
11 $ERROR_HOLD = 'error_hold';
12  
13 @OpenInteract::Error::Main::ISA = qw( Exporter );
14 $OpenInteract::Error::Main::VERSION = sprintf("%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/);
15 @OpenInteract::Error::Main::EXPORT_OK = qw( $ERROR_HOLD );
16  
17  
18 sub catch {
19 my ( $class, $err ) = @_;
20 my $R = OpenInteract::Request->instance;
21  
22 # Find the action and its error handlers
23  
24 my $action = lc $err->{action};
25 $R->DEBUG && $R->scrib( 1, "Action where the error was thrown from: <<$action>>" );
26 my $MOD_INFO = $R->error_handlers;
27 my $err_class_list = $MOD_INFO->{ $action } || [];
28 push @{ $err_class_list }, $MOD_INFO->{'_DEFAULT_HANDLER'};
29  
30 # For every handler, see if it can catch the error
31 # If so, it returns a coderef to run; if not, it simply
32 # returns undef or any other false value.
33  
34 foreach my $err_class ( @{ $err_class_list } ) {
35 $R->DEBUG && $R->scrib( 1, "See if class <<$err_class>> will take error" );
36 my $info = $err_class->can_handle_error( $err );
37 next if ( ! $info or ! ref $info or ref $info ne 'CODE' );
38 return $info->( $err );
39 }
40 die "Cannot handle error thrown: ", Dumper( $err );
41 }
42  
43  
44 # Just initialize the module info and the default error handler
45  
46 sub initialize {
47 my ( $class, $p ) = @_;
48 my $R = OpenInteract::Request->instance;
49 my $handler_info = {};
50 my $action_info = $p->{config}{action};
51 foreach my $action ( keys %{ $action_info } ) {
52 $handler_info->{ lc $action } = $action_info->{ lc $action }{error};
53 }
54 $handler_info->{'_DEFAULT_HANDLER'} = $p->{config}{error}{default_error_handler} ||
55 $p->{config}{default_error_handler};
56 $class->require_error_handlers( $handler_info );
57 my $stash_class = $p->{config}{server_info}{stash_class};
58 $stash_class->set_stash( 'error_handlers', $handler_info );
59 $R->DEBUG && $R->scrib( 1, "Initialized DEFAULT_HANDLER for app",
60 "($stash_class) to ($handler_info->{'_DEFAULT_HANDLER'})" );
61 }
62  
63  
64 sub require_error_handlers {
65 my ( $class, $handlers ) = @_;
66 foreach my $h ( values %{ $handlers } ) {
67 my $handler_list = ( ref $h ) ? $h : [ $h ];
68 foreach my $error_handler ( @{ $handler_list } ) {
69 next unless ( $error_handler );
70 eval "require $error_handler";
71 }
72 }
73 }
74  
75  
76 sub save_error {
77 my ( $class, $err ) = @_;
78 eval { $err->save() };
79 if ( $@ ) { carp " Cannot save error $err->{code}: $@"; }
80 else { carp " Error saved ok (ID: $err->{error_id})."; }
81 return $err->id;
82 }
83  
84 1;
85  
86 __END__