Better template diagnostics on failure. It now also includes the template URL, and templateId

in both the onscreen and logged output.
This commit is contained in:
Colin Kuskie 2009-06-08 16:53:53 +00:00
parent 4252b359dc
commit 8a683d1aef
9 changed files with 100 additions and 73 deletions

View file

@ -20,6 +20,7 @@ use WebGUI::International;
use WebGUI::Asset::Template::HTMLTemplate;
use WebGUI::Utility;
use WebGUI::Form;
use WebGUI::Exception;
use Tie::IxHash;
use Clone qw/clone/;
use HTML::Packer;
@ -374,9 +375,9 @@ sub getEditForm {
);
my ($style, $url) = $self->session->quick(qw(style url));
$style->setScript($url->extras('yui/build/yahoo/yahoo-min.js'));
$style->setScript($url->extras('yui/build/json/json-min.js'));
$style->setScript($url->extras('yui/build/dom/dom-min.js'));
$style->setScript($url->extras('yui/build/yahoo/yahoo-min.js'), {type => 'text/javascript'});
$style->setScript($url->extras('yui/build/json/json-min.js'), {type => 'text/javascript'});
$style->setScript($url->extras('yui/build/dom/dom-min.js'), {type => 'text/javascript'});
pop(@headers);
my $scriptUrl = $url->extras('templateAttachments.js');
@ -595,7 +596,14 @@ sub process {
? $self->get('templatePacked')
: $self->get('template')
;
return $parser->process($template, $vars);
my $output;
eval { $output = $parser->process($template, $vars); };
if (my $e = Exception::Class->caught) {
$self->session->log->error(sprintf "Error processing template: %s, %s, %s", $self->getUrl, $self->getId, $e->error);
my $i18n = WebGUI::International->new($self->session, 'Asset_Template');
$output = sprintf $i18n->get('template error').$e->error, $self->getUrl, $self->getId;
}
return $output;
}

View file

@ -16,6 +16,7 @@ package WebGUI::Asset::Template::HTMLTemplate;
use strict;
use base 'WebGUI::Asset::Template::Parser';
use WebGUI::Exception;
use HTML::Template;
@ -37,7 +38,7 @@ sub getName {
=head2 process ( template, vars )
Evaluate a template replacing template commands for HTML.
Evaluate a template replacing template commands for HTML.
=head3 template
@ -50,28 +51,25 @@ A hash reference containing template variables and loops.
=cut
sub process {
my $self = shift;
my $template = shift;
my $vars = $self->addSessionVars(shift);
my $t;
eval {
$t = HTML::Template->new(
scalarref=>\$template,
global_vars=>1,
loop_context_vars=>1,
die_on_bad_params=>0,
no_includes=>1,
strict=>0
);
};
unless ($@) {
$t->param(%{$vars});
return $t->output;
} else {
$self->session->errorHandler->error("Error in template. ".$@);
return WebGUI::International->new($self->session, 'Asset_Template')->get('template error').$@;
}
my $self = shift;
my $template = shift;
my $vars = $self->addSessionVars(shift);
my $t;
eval {
$t = HTML::Template->new(
scalarref=>\$template,
global_vars=>1,
loop_context_vars=>1,
die_on_bad_params=>0,
no_includes=>1,
strict=>0
);
};
if ($@) {
WebGUI::Error::Template->throw( error => $@ );
}
$t->param(%{$vars});
return $t->output;
}
1;

View file

@ -17,6 +17,7 @@ package WebGUI::Asset::Template::HTMLTemplateExpr;
use strict;
use base 'WebGUI::Asset::Template::Parser';
use HTML::Template::Expr;
use WebGUI::Exception;
#-------------------------------------------------------------------
@ -77,25 +78,23 @@ A hash reference containing template variables and loops.
# about the template that has the error. Finding an "ERROR: Error in template"
# in the error log is not very helpful...
sub process {
my $class = shift;
my $template = shift;
my $vars = $class->addSessionVars(shift);
my $t;
eval {
$t = HTML::Template::Expr->new(scalarref=>\$template,
global_vars=>1,
loop_context_vars=>1,
die_on_bad_params=>0,
no_includes=>1,
strict=>0);
};
unless ($@) {
$t->param(%{_rewriteVars($vars)});
return $t->output;
} else {
$class->session->errorHandler->error("Error in template. ".$@);
return WebGUI::International->new($class->session,'Asset_Template')->get('template error').$@;
}
my $class = shift;
my $template = shift;
my $vars = $class->addSessionVars(shift);
my $t;
eval {
$t = HTML::Template::Expr->new(scalarref=>\$template,
global_vars=>1,
loop_context_vars=>1,
die_on_bad_params=>0,
no_includes=>1,
strict=>0);
};
if ($@) {
WebGUI::Error::Template->throw( error => $@ );
}
$t->param(%{_rewriteVars($vars)});
return $t->output;
}
1;

View file

@ -71,25 +71,22 @@ A hash reference containing template variables and loops.
# about the template that has the error. Finding an "ERROR: Error in template"
# in the error log is not very helpful...
sub process {
my $self = shift;
my $template = shift;
my $vars = $self->addSessionVars(shift);
my ($t,$output);
eval {
$t = Template->new( {
INTERPOLATE => 1, # expand "$var" in plain text
POST_CHOMP => 1, # cleanup whitespace
EVAL_PERL => 0, # evaluate Perl code blocks
});
$t->process( \$template, _rewriteVars($vars),\$output) || $self->session->errorHandler->error($t->error());
};
unless($@){
return $output;
} else {
$self->session->errorHandler->error("Error in template. ".$@);
return WebGUI::International->new($self->session,'Asset_Template')->get('template error').$@;
}
my $self = shift;
my $template = shift;
my $vars = $self->addSessionVars(shift);
my ($t,$output);
eval {
$t = Template->new({
INTERPOLATE => 1, # expand "$var" in plain text
POST_CHOMP => 1, # cleanup whitespace
EVAL_PERL => 0, # evaluate Perl code blocks
});
$t->process( \$template, _rewriteVars($vars),\$output) || $self->session->errorHandler->error($t->error());
};
if ($@) {
WebGUI::Error::Template->throw( error => $@ );
}
return $output;
}
1;

View file

@ -16,6 +16,7 @@ package WebGUI::Asset::Template::SomeTemplateType;
use strict;
use base 'WebGUI::Asset::Template::Parser';
use WebGUI::Exception;
#-------------------------------------------------------------------
@ -52,7 +53,12 @@ sub process {
my $self = shift;
my $template = shift;
my $vars = $self->addSessionVars(shift);
...
eval {
}
if ($@) {
WebGUI::Error::Template->throw( error => $@ );
}
}

View file

@ -54,6 +54,10 @@ use Exception::Class (
description => "The file you have provided has errors.",
fields => [qw{ brokenFile brokenLine }],
},
'WebGUI::Error::Template' => {
isa => 'WebGUI::Error',
description => "A template has errors that prevent it from being processed.",
},
);

View file

@ -33,9 +33,9 @@ our $I18N = {
},
'template error' => {
message => q|There is a syntax error in this template. Please correct.|,
message => q|There is a syntax error in this template, %s, %s. Please correct.|,
context => q|Error when executing template|,
lastUpdated => 1107391368,
lastUpdated => 1244476530,
},
'namespace description' => {