[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GT] Additional error checking in Tools.pm



The attached patch can be applied against the current trunk version of
Tools.pm and provides an additional verification for the case when a
user requests a valid timeframe for which there is no data available.

The previous verification I had submitted a few days ago covers the
case where an invalid timeframe is requested.  That validation is also
included in this patch.

To reproduce the behavior try the following:
Use the current trunk version.
In ~/.gt/options, set:
DB::available_timeframes day

Run the command:
display_indicator.pl --timeframe=hour I:Prices SYMBOL

You should get a message like:

Can't call method "count" on an undefined value at ../GT/Tools.pm line nnn.

Now apply the patch, rerun the display_indicator command.

You should now get a meaningful error message.
Index: Tools.pm
===================================================================
--- Tools.pm	(revision 644)
+++ Tools.pm	(working copy)
@@ -435,8 +435,13 @@
 die("Parameter \$db not set in get_timeframe_data") if (!defined($db));
 
 if (defined($available_timeframes)) {
-	foreach (split(',', $available_timeframes)) {
-		push @tf, GT::DateTime::name_to_timeframe($_);
+	foreach my $tf_name (split(',', $available_timeframes)) {
+	        my $tf_code = GT::DateTime::name_to_timeframe($tf_name);
+		push @tf, $tf_code;
+		if (!defined($tf_code)) {
+		    my $tfs = join("\n\t", map(GT::DateTime::name_of_timeframe($_), GT::DateTime::list_of_timeframe));
+		    die("Invalid timeframe name in available_timeframes configuration item: $tf_name\n\nValid timeframes are: \n\t" . $tfs . "\n\n");
+		}
 	}
 	@tf = sort(@tf);
 } else {
@@ -452,6 +457,12 @@
   last if ($q->count > 0);
 }
 
+if (!defined($q) || $q->count == 0) {
+my $msg="No data available to generate ".GT::DateTime::name_of_timeframe($timeframe)." data.";
+$msg.="\nAvailable timeframes are: ($available_timeframes)" if (defined($available_timeframes));
+die($msg);
+}
+
 warn ("No data is available to complete the request for $code") if ($q && $q->count == 0);
 my $calc = GT::Calculator->new($q);
 $calc->set_code($code);