Add retry in case of problems in dispatch. Add tests for logging errors for bad templates.

This commit is contained in:
Colin Kuskie 2010-08-04 14:10:43 -07:00
parent 25fb019a18
commit 3a9a3dd8e9
2 changed files with 29 additions and 5 deletions

View file

@ -580,14 +580,13 @@ the requested method has failed.
=cut
sub dispatch {
my ($self, $fragment, $_view) = @_;
my ($self, $fragment) = @_;
return undef if defined $fragment;
my $session = $self->session;
my $state = $self->get('state');
##Only allow interaction with assets in certain states
return if $state ne 'published' && $state ne 'archived' && !$session->var->isAdminOn;
my $func = $session->form->param('func') || 'view';
$func = 'view' if $_view;
my $viewing = $func eq 'view' ? 1 : 0;
my $sub = $self->can('www_'.$func);
if (!$sub && $func ne 'view') {
@ -595,13 +594,24 @@ sub dispatch {
$viewing = 1;
}
return undef unless $sub;
my $output = eval { $sub->(); };
my $output = eval { $self->$sub(); };
if (my $e = Exception::Class->caught('WebGUI::Error::ObjectNotFound::Template')) {
#WebGUI::Error::ObjectNotFound::Template
warn "logged an exception";
$session->log->error(sprintf "%s templateId: %s assetId: %s", $e->error, $e->templateId, $e->assetId);
}
elsif ($@) {
warn "logged a warn: $@";
$session->log->warn("Couldn't call method www_".$func." on asset for url: ".$session->url->getRequestedUrl." Root cause: ".$@);
}
return $output if $output || $viewing;
##No output, try the view method instead
$output = eval { $self->www_view };
if (my $e = Exception::Class->caught('WebGUI::Error::ObjectNotFound::Template')) {
$session->log->error(sprintf "%s templateId: %s assetId: %s", $e->error, $e->templateId, $e->assetId);
}
elsif ($@) {
$session->errorHandler->warn("Couldn't call method www_".$func." on asset for url: ".$session->url->getRequestedUrl." Root cause: ".$@);
die $@;
}
return $output;
}

View file

@ -74,7 +74,7 @@ WebGUI::Test->addToCleanup( $tag );
#----------------------------------------------------------------------------
# Tests
plan tests => 13; # Increment this number for each test you create
plan tests => 16; # Increment this number for each test you create
#----------------------------------------------------------------------------
# Test dispatch
@ -125,4 +125,18 @@ $session->var->switchAdminOn;
$output = $td->dispatch();
is $output, 'www_view', 'when admin is on, the asset can be accessed';
$td->publish();
$session->var->switchAdminOff;
$output = $td->dispatch();
is $output, 'www_view', 'asset state restored for next tests';
# Test template exceptions
$session->request->setup_body( {
func => 'brokenTemplate',
} );
WebGUI::Test->interceptLogging;
is( $td->dispatch, "www_view", "if a query method throws a Template exception, view is returned instead" );
is $WebGUI::Test::logger_error, 'Template not found templateId: This is a GUID assetId: '. $td->getId, 'logged an error';
WebGUI::Test->restoreLogging;
#vim:ft=perl