[prev in list] [next in list] [prev in thread] [next in thread] 

List:       cgiapp
Subject:    Re: [cgiapp] Best way to use error mode?
From:       "Aleksandar Petrovic" <webmaster () techcode ! net>
Date:       2006-12-01 10:16:36
Message-ID: op.tjvlhyfjj5k8v1 () titan ! techcode ! net
[Download RAW message or body]

On Fri, 01 Dec 2006 01:29:41 +0100, Jamie Krasnoo <jkrasnoo@gmail.com>  
wrote:

> Hey all,
>
> I use CGI::Application::Plugin::DBH and I was wondering if a query
> were to be rejected does it set off the error mode. If it doesn't.
> What would be the best way to catch database errors and have it go
> through the error mode?

To my understanding error mode will be called for any errors that occur
in your runmode. I belive there is something like this in CGI::App code :

eval {
	call_run_mode();
}
if($@){
	#check if error mode is set and call it
}

So if you are talking about errors in your runmode - then yes it will call
what you set as error handler.

But if you have an error before/after your runmode - say in cgiapp_init  
where
you connect to DB, check session ...etc. It wont call it.

What is even more anoying is that you can't "redirect" to your error  
runmode
since you can only change the runmode from within cgiapp_prerun.

Way around it seems to be:

sub cgiapp_init {
	...
	connect_to_db() || $self->param('redirect_to_error' => "Couldn't connect  
to DB!");
	...
}

sub cgiapp_prerun {
	...
	if($self->param('redirect_to_error')){
		# change the runmode to be executed to your error handler
		# and error handler should then print out redirect_to_error
	}
}

Other way is to play around with setting $SIG{_DIE_} ...

BTW. Any errors = die/carp not warnings.


IMHO I don't really see a reason why changing runmode should be limited
to just cgiapp_prerun - while you can simply say something like:

sub my_runmode {
	....

	if(some_error){
		return $self->ERROR_HANDLER();
	}
}

It stops you from refactoring some code that can be reused. In my case it  
was code that checks if particular user should be alowed to edit  
(change/delete) particular DB record.

Now I have :

sub authorize_edit { # Not a runmode
	my $self 			= shift;
	my $listing_data		= shift;
	my $session 		= $self->session();
	
	return ( ($listing_data->{car_user_id} == $session->param('user_id')) ||
		   ($session->param('user_type') eq 'admin') );
}

sub delete_confirmed : Runmode {
	my $self	= shift;
	my $session = $self->session();
	my $form	= $self->form();
	my $dbt	= $self->param('dbt');
	
	my $listing_data = $dbt->execute(
			sql		=> 'SELECT * FROM cars WHERE car_id = ?',
			data		=> [$form->{car_id}],
			method	=> 'fetchrow_hashref'
	) || return $self->ERROR(
		'No listing with provided ID found',
		'Most likely the listing has been removed from our database.'
	);
	
	# Check if that user is the owner ... admin is always OK
	return $self->ERROR(
		'Delete listing error',
		"You are not the owner of record #:$listing_data->{car_id}"
	) unless $self->authorize_edit($listing_data);

	
	$dbt->execute(
		sql 	=> 'DELETE FROM cars WHERE car_id = ?',
		data	=> [$form->{car_id}]
	);
	
	return $self->INFO_PAGE(
		'Listing deleted',
		'Listing was sucessfully deleted.'
	);
}

And it would be better if I could simply have :

sub authorize_edit { # Not a runmode
	my $self 			= shift;
	my $listing_data		= shift;
	my $session 		= $self->session();
	
	unless ( ($listing_data->{car_user_id} == $session->param('user_id')) ||
		   ($session->param('user_type') eq 'admin') ) {

		# Change to error runmode - and perhaps just set params for error  
title/description
	}
}

sub delete_confirmed : Runmode {
	....
	$self->authorize_edit($listing_data);
	....
}

I also believe that error handler should be wraped around all user code -  
which would mean all stages of execution, not just runmodes.


Mark S. - Jesse E. - Anyone else?


Cheers,
-- 
    Aleksandar Petroviæ
     www.techcode.net
  Web Design & Development

Skype 	techcode.net
Yahoo! IM	johndoeyu
ICQ     	75863829
MSN    	johndoeyu@yahoo.com
AIM   	tchcode

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/cgiapp@lists.erlbaum.net/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: cgiapp-unsubscribe@lists.erlbaum.net
For additional commands, e-mail: cgiapp-help@lists.erlbaum.net

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic