From b3faecae9b4e857626f8674874c806e4b6497f22 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Wed, 27 Jan 2010 21:42:27 -0800 Subject: [PATCH] Fix RandomAssetProxy macro. --- lib/WebGUI/Macro/RandomAssetProxy.pm | 46 +++++++++++++++------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/WebGUI/Macro/RandomAssetProxy.pm b/lib/WebGUI/Macro/RandomAssetProxy.pm index 81d417ba5..34a17ee83 100644 --- a/lib/WebGUI/Macro/RandomAssetProxy.pm +++ b/lib/WebGUI/Macro/RandomAssetProxy.pm @@ -13,6 +13,7 @@ package WebGUI::Macro::RandomAssetProxy; use strict; use WebGUI::Asset; use WebGUI::International; +use WebGUI::Exception; =head1 NAME @@ -34,28 +35,29 @@ if no asset exists at that url, or if the asset has no children. #------------------------------------------------------------------- sub process { - my $session = shift; - my $url = shift; - my $i18n = WebGUI::International->new($session,'Macro_RandomAssetProxy'); - my $asset = WebGUI::Asset->newByUrl($session, $url); - if (defined $asset) { - my $children = $asset->getLineage(["children"]); - #randomize; - my $randomAssetId = $children->[int(rand(scalar(@{$children})))]; - my $randomAsset = WebGUI::Asset->newById($session,$randomAssetId); - if (defined $randomAsset) { - if ($randomAsset->canView) { - $randomAsset->toggleToolbar; - $randomAsset->prepareView; - return $randomAsset->view; - } - return undef; - } else { - return $i18n->get('childless'); - } - } else { - return $i18n->get('invalid url'); - } + my $session = shift; + my $url = shift; + my $i18n = WebGUI::International->new($session,'Macro_RandomAssetProxy'); + my $asset = eval { WebGUI::Asset->newByUrl($session, $url); }; + if (Exception::Class->caught()) { + return $i18n->get('invalid url'); + } + + my $children = $asset->getLineage(["children"]); + #randomize; + my $randomAssetId = $children->[int(rand(scalar(@{$children})))]; + my $randomAsset = eval { WebGUI::Asset->newById($session,$randomAssetId); }; + if (Exception::Class->caught()) { + return $i18n->get('childless'); + } + elsif ($randomAsset->canView) { + $randomAsset->toggleToolbar; + $randomAsset->prepareView; + return $randomAsset->view; + } + else { + return undef; + } }