Better error handling
Tomas Doran [Tue, 27 Sep 2011 15:08:22 +0000 (16:08 +0100)]
lib/CatalystX/HelpText/ViewRole.pm
t/lib/TestApp/root/main

index f06b81b..6220c6a 100644 (file)
@@ -12,13 +12,28 @@ around expose_methods => sub {
     [ $m->flatten, 'help_text' ];
 };
 
+has helptext_model => (
+    is => 'ro',
+    default => 'Help',
+);
+
 sub help_text {
     my ($self, $c, $key) = @_;
+    my $model = try { $c->model($self->helptext_model) };
+    if (!$model) {
+        $c->log->warn(sprintf("Cannot find the '%s' model for %s", $self->helptext_model, ref($self))) if $c->debug;
+        return;
+    }
+    if (!$model->can('get_help_text_for')) {
+        $c->log->warn(sprintf("Your '%s' model used by '%s' does not have a get_help_text_for method", $self->helptext_model, ref($self))) if $c->debug;
+        return;
+    }
     try {
-        return $c->model('Help')->get_help_text_for($c, $key);
+        $model->get_help_text_for($key);
     }
     catch {
-        $c->log->warn("Error retrieving help_text: ".$_);
+        $c->log->warn("Error retrieving help_text key '$key' from model: $_")
+            if $c->debug;
         return '';
     };
 }
index c578551..783474d 100644 (file)
@@ -1 +1 @@
-fnar[% helptext('fnoo') %]fnee
+fnar[% help_text('fnoo') %]fnee