| 1 | package OpenInteract::Error::System; | ||
| 2 | |||
| 3 | # $Id: System.pm,v 1.9 2002/04/13 16:10:06 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Carp qw( cluck ); | ||
| 7 | use Data::Dumper qw( Dumper ); | ||
| 8 | use OpenInteract::Error::Main; | ||
| 9 | use OpenInteract::Utility; | ||
| 10 | use SPOPS::Utility; | ||
| 11 | |||
| 12 | @OpenInteract::Error::System::ISA = (); | ||
| 13 | $OpenInteract::Error::System::VERSION = sprintf("%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/); | ||
| 14 | |||
| 15 | my $ERROR_HOLD = $OpenInteract::Error::Main::ERROR_HOLD; | ||
| 16 | |||
| 17 | # Increment at which we should break down | ||
| 18 | # codes if not found (change to 100 for more macro) | ||
| 19 | |||
| 20 | my $CODE_SEEK = 10; | ||
| 21 | |||
| 22 | # Match up codes with the actual handler | ||
| 23 | |||
| 24 | $OpenInteract::Error::System::CODES = { | ||
| 25 | '-1' => \&log_and_return, | ||
| 26 | '0' => \&log_and_return, | ||
| 27 | '10' => \&cannot_parse_config, | ||
| 28 | '11' => \&cannot_connect_db, | ||
| 29 | '100' => \&log_and_return, | ||
| 30 | '200' => \&log_and_return, | ||
| 31 | '201' => \&cannot_open_template, | ||
| 32 | '202' => \&cannot_parse_template, | ||
| 33 | '203' => \&cannot_send_mail, | ||
| 34 | '204' => \&cannot_open_template_db, | ||
| 35 | '205',=> \&cannot_find_login_fields, | ||
| 36 | '300' => \&log_and_return, | ||
| 37 | '301' => \&file_not_found, | ||
| 38 | '302' => \&cannot_log_object_creation, | ||
| 39 | '303' => \&task_is_forbidden, | ||
| 40 | '304' => \&task_no_default, | ||
| 41 | '305' => \&task_not_allowed_security, | ||
| 42 | '307' => \&cannot_retrieve_object_of_id, | ||
| 43 | '308' => \&log_and_return, | ||
| 44 | '309' => \&log_and_return, | ||
| 45 | '310' => \&cannot_create_session, | ||
| 46 | '311' => \&log_and_return, | ||
| 47 | '312' => \&log_and_return, | ||
| 48 | '313' => \&log_and_return, | ||
| 49 | '314' => \&file_not_found, | ||
| 50 | '400' => \&log_and_return, | ||
| 51 | '401' => \&bad_username, | ||
| 52 | '402' => \&bad_password, | ||
| 53 | '403' => \&cannot_fetch, | ||
| 54 | '404' => \&cannot_fetch, | ||
| 55 | '405' => \&log_and_return, | ||
| 56 | '406' => \&log_and_return, | ||
| 57 | '407' => \&log_and_return, | ||
| 58 | '500' => \&log_and_return, | ||
| 59 | '600' => \&log_and_return, | ||
| 60 | '700' => \&log_and_return, | ||
| 61 | '800' => \&log_and_return, | ||
| 62 | '900' => \&log_and_return, | ||
| 63 | }; | ||
| 64 | |||
| 65 | # Since we can handle any code, we move the checking phase to the error handler | ||
| 66 | |||
| 67 | sub can_handle_error { | ||
| 68 | my ( $class, $err ) = @_; | ||
| 69 | |||
| 70 | # Just find the general handler for that particular code. Here's an | ||
| 71 | # example of how we find a code: | ||
| 72 | # Original code: 512 | ||
| 73 | # 1) 512 -> not found -> 510 | ||
| 74 | # 2) 510 -> not found -> 500 | ||
| 75 | |||
| 76 | my $start = $CODE_SEEK; | ||
| 77 | my $info = $OpenInteract::Error::System::CODES->{ $err->{code} }; | ||
| 78 | while ( ! $info ) { | ||
| 79 | my $use_code = $err->{code} - ( $err->{code} % $start ); | ||
| 80 | $start *= 10; | ||
| 81 | $info = $OpenInteract::Error::System::CODES->{ $use_code }; | ||
| 82 | } | ||
| 83 | return $info; | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | #000 | ||
| 88 | |||
| 89 | sub cannot_parse_config { | ||
| 90 | my ( $err ) = @_; | ||
| 91 | my $R = OpenInteract::Request->instance; | ||
| 92 | $R->DEBUG && cluck ">> Error: Cannot open/parse config file.", | ||
| 93 | "$err->{tmp_filename} / $err->{system_msg}"; | ||
| 94 | $err->{notes} = "Filename tried to open: $err->{tmp_filename}"; | ||
| 95 | OpenInteract::Error::Main->save_error( $err ); | ||
| 96 | return '<h2 align="center">Website Down</h2>' . | ||
| 97 | '<p>This website is currently down for technical reasons. Please come back shortly ' . | ||
| 98 | 'and we will surely have this configuration problem licked.'; | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | # Since the db is down, we need to bypass the normal saving mechanisms | ||
| 103 | # and save the object by hand. We generate the ID here since it's | ||
| 104 | # normally done by the $err->save call. | ||
| 105 | |||
| 106 | sub cannot_connect_db { | ||
| 107 | my ( $err ) = @_; | ||
| 108 | my $R = OpenInteract::Request->instance; | ||
| 109 | $R->DEBUG && cluck ">> Error: Cannot connect to db. Info:\n", | ||
| 110 | Dumper( $err->{tmp_db_info} ) ; | ||
| 111 | my $C = $R->CONFIG; | ||
| 112 | $err->{error_id} = SPOPS::Utility->generate_random_code( 16 ); | ||
| 113 | |||
| 114 | # First send an email | ||
| 115 | |||
| 116 | my $msg = <<MSG; | ||
| 117 | |||
| 118 | Website Name: $C->{server_info}{website_name} | ||
| 119 | |||
| 120 | Something terrible has happened: I cannot connect to | ||
| 121 | the database or a previous connection has been dropped. | ||
| 122 | If you do not fix this soon, you will have an angry | ||
| 123 | mob of users on your hands. | ||
| 124 | |||
| 125 | Error Code: $err->{error_id} | ||
| 126 | (written out to $R->{dir}{base}/error) | ||
| 127 | |||
| 128 | Your friendly OpenInteract System | ||
| 129 | MSG | ||
| 130 | my $send_to = $R->CONFIG->{mail}{admin_email} || | ||
| 131 | $R->CONFIG->{admin_email}; | ||
| 132 | eval { OpenInteract::Utility->send_email({ to => $send_to, | ||
| 133 | message => $msg, | ||
| 134 | subject => 'Cannot connect to database!' }) }; | ||
| 135 | if ( $@ ) { | ||
| 136 | $err->{notes} = "Cannot send email to admin; saved email msg"; | ||
| 137 | $R->throw({ code => 203 }); | ||
| 138 | } | ||
| 139 | else { | ||
| 140 | $err->{notes} = 'E-mail sent to admin ok.'; | ||
| 141 | } | ||
| 142 | $err->{notes} = "DB connection info:", Dumper( $err->{tmp_db_info} ); | ||
| 143 | $err->fail_save; | ||
| 144 | return '<h2 align="center">Website Down</h2>' . | ||
| 145 | '<p>This website is currently down for technical reasons. Please come back shortly ' . | ||
| 146 | 'and we will surely have this database problem licked.'; | ||
| 147 | } | ||
| 148 | |||
| 149 | |||
| 150 | #100 | ||
| 151 | |||
| 152 | |||
| 153 | #200 | ||
| 154 | |||
| 155 | sub cannot_open_template { | ||
| 156 | my ( $err ) = @_; | ||
| 157 | my $R = OpenInteract::Request->instance; | ||
| 158 | $R->DEBUG && cluck ">> Error: cannot open template.", | ||
| 159 | "$err->{tmp_filename} / $err->{system_msg}"; | ||
| 160 | $err->{user_msg} = 'Could not open template file'; | ||
| 161 | $err->{notes} = "Filename tried to open: $err->{tmp_filename}"; | ||
| 162 | OpenInteract::Error::Main->save_error( $err ); | ||
| 163 | return undef; | ||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | sub cannot_parse_template { | ||
| 168 | my ( $err ) = @_; | ||
| 169 | my $R = OpenInteract::Request->instance; | ||
| 170 | $R->DEBUG && cluck ">> Error: cannot parse template.", | ||
| 171 | "$err->{tmp_filename} / $err->{system_msg}"; | ||
| 172 | $err->{user_msg} = 'Could not process template'; | ||
| 173 | $err->{notes} = "Filename tried to open: $err->{tmp_filename} (if blank, we used text passed in)"; | ||
| 174 | OpenInteract::Error::Main->save_error( $err ); | ||
| 175 | return "[[ error processing directive: template error ]]"; | ||
| 176 | } | ||
| 177 | |||
| 178 | |||
| 179 | sub cannot_send_mail { | ||
| 180 | my ( $err ) = @_; | ||
| 181 | my $mail_info = $OpenInteract::Error::extra; | ||
| 182 | OpenInteract::Error::Main->save_error( $err ); | ||
| 183 | my $R = OpenInteract::Request->instance; | ||
| 184 | my $filename = $R->CONFIG->get_dir( 'mail' ) . "/msg_$err->{error_id}"; | ||
| 185 | eval { open( MAIL, "> $filename" ) || die $! }; | ||
| 186 | if ( $@ ) { | ||
| 187 | $R->scrib( 0, "Good gravy! I cannot send an email, and I cannot open up a file to dump\n", | ||
| 188 | "the email into. (Tried: (($filename)) and got (($@)) in return.)\n", | ||
| 189 | "I'll just put it into STDERR and you can deal with it.\n\n", Dumper( $mail_info ), "\n" ); | ||
| 190 | return undef; | ||
| 191 | } | ||
| 192 | print MAIL "To: $mail_info->{to}\n", | ||
| 193 | "From: $mail_info->{from}\n", | ||
| 194 | "Subject: $mail_info->{subject}\n\n", | ||
| 195 | $mail_info->{message}, "\n\n"; | ||
| 196 | close( MAIL ); | ||
| 197 | return undef; | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | |||
| 202 | sub cannot_open_template_db { | ||
| 203 | my ( $err ) = @_; | ||
| 204 | my $R = OpenInteract::Request->instance; | ||
| 205 | $R->DEBUG && cluck ">> Error: cannot open template.", | ||
| 206 | "$err->{tmp_name} / $err->{system_msg}"; | ||
| 207 | $err->{user_msg} = 'Could not open template from database'; | ||
| 208 | $err->{notes} = "Tag tried to open with: $err->{tmp_name}"; | ||
| 209 | OpenInteract::Error::Main->save_error( $err ); | ||
| 210 | return undef; | ||
| 211 | } | ||
| 212 | |||
| 213 | |||
| 214 | sub cannot_find_login_fields { | ||
| 215 | my ( $err ) = @_; | ||
| 216 | my $R = OpenInteract::Request->instance; | ||
| 217 | $R->DEBUG && cluck ">> Error: cannot find login/password fields!"; | ||
| 218 | $err->{user_msg} = 'No login/password fields specified!'; | ||
| 219 | $err->{system_msg} = "Please create entries in your 'conf/server.perl' file under " . | ||
| 220 | "the 'login->login_field' and 'login->password_field' keys. I cannot " . | ||
| 221 | "process logins until this is done. Remember to restart the server after " . | ||
| 222 | "you have made the change."; | ||
| 223 | OpenInteract::Error::Main->save_error( $err ); | ||
| 224 | return undef; | ||
| 225 | } | ||
| 226 | |||
| 227 | |||
| 228 | #300 | ||
| 229 | |||
| 230 | # Syntax: $R->throw({ code => 314, system_msg => '/location/notfound' }); | ||
| 231 | |||
| 232 | sub file_not_found { | ||
| 233 | my ( $err ) = @_; | ||
| 234 | my $R = OpenInteract::Request->instance; | ||
| 235 | $R->DEBUG && cluck ">> Error: cannot find or open requested location *$err->{system_msg})"; | ||
| 236 | $err->{user_msg} = 'Requested file not found or cannot be opened'; | ||
| 237 | OpenInteract::Error::Main->save_error( $err ); | ||
| 238 | $R->{page}{title} = 'Sorry: Not found'; | ||
| 239 | my $html = $R->template->handler( {}, { err => $err }, | ||
| 240 | { name => 'error_not_found' } ); | ||
| 241 | die "$html\n"; | ||
| 242 | } | ||
| 243 | |||
| 244 | |||
| 245 | sub cannot_log_object_creation { | ||
| 246 | my ( $err ) = @_; | ||
| 247 | OpenInteract::Error::Main->save_error( $err ); | ||
| 248 | return undef; | ||
| 249 | } | ||
| 250 | |||
| 251 | |||
| 252 | sub task_is_forbidden { | ||
| 253 | my ( $err ) = @_; | ||
| 254 | my $R = OpenInteract::Request->instance; | ||
| 255 | $R->DEBUG && cluck ">> Error: Cannot perform task due to security ($err->{system_msg})"; | ||
| 256 | my $html = $R->template->handler( {}, | ||
| 257 | { err => $err, | ||
| 258 | admin_email => $R->CONFIG->{mail}{admin_email} || | ||
| 259 | $R->CONFIG->{admin_email} }, | ||
| 260 | { name => 'error_task_forbidden' } ); | ||
| 261 | die "$html\n"; | ||
| 262 | } | ||
| 263 | |||
| 264 | |||
| 265 | sub task_no_default { | ||
| 266 | my ( $err ) = @_; | ||
| 267 | my $R = OpenInteract::Request->instance; | ||
| 268 | $err->{user_msg} = 'No default method defined'; | ||
| 269 | $R->DEBUG && cluck ">> Error: Cannot do task due to no default: $err->{system_msg}"; | ||
| 270 | |||
| 271 | # First send an email | ||
| 272 | |||
| 273 | eval { OpenInteract::Utility->send_email({ to => $err->{tmp_email}, | ||
| 274 | message => $err->{tmp_msg}, | ||
| 275 | subject => $err->{tmp_subject} }) }; | ||
| 276 | if ( $@ ) { | ||
| 277 | $err->{notes} = "Cannot send email to author; saved email msg"; | ||
| 278 | $R->throw({ code => 203 }); | ||
| 279 | } | ||
| 280 | else { | ||
| 281 | $err->{notes} = 'E-mail sent to author ok.'; | ||
| 282 | } | ||
| 283 | OpenInteract::Error::Main->save_error( $err ); | ||
| 284 | my $html = $R->template->handler( {}, | ||
| 285 | { err => $err, | ||
| 286 | author_email => $err->{tmp_email} }, | ||
| 287 | { name => 'error_task_no_default' } ); | ||
| 288 | die "$html\n"; | ||
| 289 | } | ||
| 290 | |||
| 291 | |||
| 292 | sub task_not_allowed_security { | ||
| 293 | my ( $err ) = @_; | ||
| 294 | my $R = OpenInteract::Request->instance; | ||
| 295 | $R->DEBUG && cluck ">> Error: Cannot accomplish task due to security."; | ||
| 296 | my $html = $R->template->handler( {}, | ||
| 297 | { err => $err, | ||
| 298 | admin_email => $R->CONFIG->{mail}{admin_email} || | ||
| 299 | $R->CONFIG->{admin_email} }, | ||
| 300 | { name => 'error_task_forbidden' } ); | ||
| 301 | die "$html\n"; | ||
| 302 | } | ||
| 303 | |||
| 304 | |||
| 305 | sub cannot_retrieve_object_of_id { | ||
| 306 | my ( $err ) = @_; | ||
| 307 | my $R = OpenInteract::Request->instance; | ||
| 308 | $R->DEBUG && cluck ">> Error: Cannot retrieve object given a particular ID."; | ||
| 309 | |||
| 310 | # First send an email | ||
| 311 | |||
| 312 | my $msg = <<''; | ||
| 313 | For some reason beyond security, a user could not retrieve an object | ||
| 314 | given a particular ID. You might want to look into it. | ||
| 315 | |||
| 316 | eval { OpenInteract::Utility->send_email({ to => $R->CONFIG->{mail}{admin_email} || | ||
| 317 | $R->CONFIG->{admin_email}, | ||
| 318 | message => $msg, | ||
| 319 | subject => 'Failed to retrieve object' }) }; | ||
| 320 | if ( $@ ) { | ||
| 321 | $err->{notes} = "Cannot send email to admin; saved email msg"; | ||
| 322 | $R->throw( { code => 203 } ); | ||
| 323 | } | ||
| 324 | else { | ||
| 325 | $err->{notes} = 'E-mail sent to admin ok.'; | ||
| 326 | } | ||
| 327 | OpenInteract::Error::Main->save_error( $err ); | ||
| 328 | return undef; | ||
| 329 | } | ||
| 330 | |||
| 331 | |||
| 332 | sub cannot_create_session { | ||
| 333 | my ( $err ) = @_; | ||
| 334 | my $R = OpenInteract::Request->instance; | ||
| 335 | $R->scrib( 0, "Cannot create session -- someone is probably using a ", | ||
| 336 | "defunct key or something." ); | ||
| 337 | $R->cookies->create_cookie({ | ||
| 338 | name => $OpenInteract::Session::COOKIE_NAME, | ||
| 339 | path => '/', | ||
| 340 | value => undef, | ||
| 341 | expires => '-3d' }); | ||
| 342 | return log_and_return( $err ); | ||
| 343 | } | ||
| 344 | |||
| 345 | |||
| 346 | #400 | ||
| 347 | |||
| 348 | sub bad_username { | ||
| 349 | my ( $err ) = @_; | ||
| 350 | my $R = OpenInteract::Request->instance; | ||
| 351 | $R->DEBUG && cluck ">> Error: User $err->{tmp_login_name} not found in system"; | ||
| 352 | $R->{ $ERROR_HOLD }{loginbox}{bad_login} = "User $err->{tmp_login_name} not found!"; | ||
| 353 | return undef; | ||
| 354 | } | ||
| 355 | |||
| 356 | sub bad_password { | ||
| 357 | my ( $err ) = @_; | ||
| 358 | my $R = OpenInteract::Request->instance; | ||
| 359 | $R->DEBUG && cluck ">> Error: User $err->{tmp_login_name} login with wrong password"; | ||
| 360 | $R->{ $ERROR_HOLD }{loginbox}{bad_login} = "Bad password for $err->{tmp_login_name}; try again."; | ||
| 361 | $R->{ $ERROR_HOLD }{loginbox}{login_name} = $err->{tmp_login_name}; | ||
| 362 | return undef; | ||
| 363 | } | ||
| 364 | |||
| 365 | |||
| 366 | sub cannot_fetch { | ||
| 367 | my ( $err ) = @_; | ||
| 368 | if ( $err->{code} == 403 ) { | ||
| 369 | cluck ">> Error thrown for not being able to fetch a group of objects. Msg: $err->{system_msg}"; | ||
| 370 | } | ||
| 371 | elsif ( $err->{code} == 404 ) { | ||
| 372 | cluck ">> Error thrown for not being able to fetch an object. Msg: $err->{system_msg}"; | ||
| 373 | } | ||
| 374 | return log_and_return( $err, 'nowarn' ); | ||
| 375 | } | ||
| 376 | |||
| 377 | #500 | ||
| 378 | |||
| 379 | |||
| 380 | #600 | ||
| 381 | |||
| 382 | |||
| 383 | #700 | ||
| 384 | |||
| 385 | |||
| 386 | |||
| 387 | #Generic | ||
| 388 | |||
| 389 | sub log_and_return { | ||
| 390 | my ( $err, $opt ) = @_; | ||
| 391 | my $R = OpenInteract::Request->instance; | ||
| 392 | cluck ">> Error with code $err->{code} thrown. Info: $err->{system_msg}" unless ( $opt eq 'nowarn' ); | ||
| 393 | $R->DEBUG && $R->scrib( 2, "Trying to save errror ", Dumper( $err ) ); | ||
| 394 | OpenInteract::Error::Main->save_error( $err ); | ||
| 395 | return undef; | ||
| 396 | } | ||
| 397 | |||
| 398 | 1; | ||
| 399 | |||
| 400 | __END__ |