Name

pkit_on_error — Can be used to catch any fatal error condition.

Synopsis


  ## inside your Common.pm, maybe also in your Modelclass to overwrite the one in Common.pm

  sub pkit_on_error {
    my ( $model, $err ) = @_;
    my $page_id = $model->pkit_get_page_id;

    if ( $err =~ /DBI/ ) {
      # prevent endless loop if the error happened on our errorpage.
      return $err if ( $page_id =~ /db_error_page$/ );
      $model->pkit_redirect('db_error_page');
      return;
    }

    $model->pkit_message( $err );
    my $default_page = $model->pkit_get_default_page;
    return $err if ( $page_id =~ /^$default_page$/ );
    $model->pkit_redirect($default_page);
    return;
  }

     

Description

pkit_on_error main purpose is to catch errors for some reason. Maybe you can not get a database connection or your page can not be delivered in a special charset or anywhere inside your modelcode an uncatched die "for unknown reason"; is executed.

If nowhere else in your code this error is handled, pkit_on_error is called with the error message as second parameter and model as the first. This is your chance to deliver a working page. At best with pkit_redirect or pkit_send.

If an error happened inside pkit_on_error it is passed thru Apache and you get the internal server error as before. pkit_on_error returns the error_msg that is logged. So you can change the messsage if you like. If the returned error_msg is '' or undef nothing is logged and pkit assumes, that the error is repaired. If you like to log anyway add a warn $error_msg"; inside your pkit_on_error routine.

Also pkit_on_error is no replacement for Apache::ErrorReport. pkit_on_error is more a runtime error catcher for most cases. A::E reports errors more clear with stacktrace and so on. Both can happy live together.

Another advantage is that you need only pkit_on_error to catch the error even if your application has millions of cases where it could fail. There is no need to catch them all.

If pkit_on_error is not defined in your modelclass or Common.pm, it is not used.

pkit_on_error catch only fatal errors (die) NOT warnings.

If your code likes to throw an error let it die.