Tests for methods that die. Strange tom-foolery to get $@ into the logged message...

This commit is contained in:
Colin Kuskie 2010-08-04 19:57:44 -07:00
parent 3a9a3dd8e9
commit 8a448e8f96
2 changed files with 17 additions and 11 deletions

View file

@ -572,11 +572,6 @@ the output from the www_view method.
Any leftover part of the requested URL. Any leftover part of the requested URL.
=head3 _view
This option should only be used internally, when trying to call the view method when
the requested method has failed.
=cut =cut
sub dispatch { sub dispatch {
@ -597,12 +592,11 @@ sub dispatch {
my $output = eval { $self->$sub(); }; my $output = eval { $self->$sub(); };
if (my $e = Exception::Class->caught('WebGUI::Error::ObjectNotFound::Template')) { if (my $e = Exception::Class->caught('WebGUI::Error::ObjectNotFound::Template')) {
#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); $session->log->error(sprintf "%s templateId: %s assetId: %s", $e->error, $e->templateId, $e->assetId);
} }
elsif ($@) { elsif ($@) {
warn "logged a warn: $@"; my $message = $@;
$session->log->warn("Couldn't call method www_".$func." on asset for url: ".$session->url->getRequestedUrl." Root cause: ".$@); $session->log->warn("Couldn't call method www_".$func." on asset for url: ".$session->url->getRequestedUrl." Root cause: ".$message);
} }
return $output if $output || $viewing; return $output if $output || $viewing;
##No output, try the view method instead ##No output, try the view method instead
@ -611,7 +605,9 @@ sub dispatch {
$session->log->error(sprintf "%s templateId: %s assetId: %s", $e->error, $e->templateId, $e->assetId); $session->log->error(sprintf "%s templateId: %s assetId: %s", $e->error, $e->templateId, $e->assetId);
} }
elsif ($@) { elsif ($@) {
$session->errorHandler->warn("Couldn't call method www_".$func." on asset for url: ".$session->url->getRequestedUrl." Root cause: ".$@); warn "logged another warn: $@";
my $message = $@;
$session->log->warn("Couldn't call method www_view on asset for url: ".$session->url->getRequestedUrl." Root cause: ".$@);
} }
return $output; return $output;
} }

View file

@ -66,6 +66,11 @@ sub www_brokenTemplate {
); );
} }
sub www_dies {
my $self = shift;
die "...aside from that bullet\n";
}
package main; package main;
my $tag = WebGUI::VersionTag->getWorking( $session ); my $tag = WebGUI::VersionTag->getWorking( $session );
@ -74,7 +79,7 @@ WebGUI::Test->addToCleanup( $tag );
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# Tests # Tests
plan tests => 16; # Increment this number for each test you create plan tests => 18; # Increment this number for each test you create
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# Test dispatch # Test dispatch
@ -136,7 +141,12 @@ $session->request->setup_body( {
} ); } );
WebGUI::Test->interceptLogging; WebGUI::Test->interceptLogging;
is( $td->dispatch, "www_view", "if a query method throws a Template exception, view is returned instead" ); 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'; is $WebGUI::Test::logger_error, 'Template not found templateId: This is a GUID assetId: '. $td->getId, '... and logged an error';
$session->request->setup_body( {
func => 'dies',
} );
is( $td->dispatch, "www_view", "if a query method dies, view is returned instead" );
is $WebGUI::Test::logger_warns, "Couldn't call method www_dies on asset for url: Root cause: ...aside from that bullet\n", '.. and logged a warn';
WebGUI::Test->restoreLogging; WebGUI::Test->restoreLogging;
#vim:ft=perl #vim:ft=perl