Browse Source

Added error handling for Condition

Alois Mahdal 11 years ago
parent
commit
b18e155bd0
2 changed files with 17 additions and 1 deletions
  1. 13
    1
      lib/SugarTrail/Template/Condition.pm
  2. 4
    0
      lib/SugarTrail/Template/Master.pm

+ 13
- 1
lib/SugarTrail/Template/Condition.pm View File

53
 
53
 
54
     # get name, operator, value(s)
54
     # get name, operator, value(s)
55
     my ($n, $o, $v) = $string =~ m/([a-zA-Z0-9_ ]+)([<>=~+\-*@]*)(.*)/;
55
     my ($n, $o, $v) = $string =~ m/([a-zA-Z0-9_ ]+)([<>=~+\-*@]*)(.*)/;
56
-    return unless $n;
57
 
56
 
58
     # cut inner spaces
57
     # cut inner spaces
59
     $n =~ s/\s+$//;
58
     $n =~ s/\s+$//;
64
     # split vlues
63
     # split vlues
65
     my @v = split m/\s*,\s*/, $v;
64
     my @v = split m/\s*,\s*/, $v;
66
 
65
 
66
+    unless ($string and $n and $o and @v) {
67
+        $self->{error} = "syntax error: missing param name" unless $n;
68
+        $self->{error} = "syntax error: missing operator" unless $o;
69
+        $self->{error} = "syntax error: no values" unless @v;
70
+        $self->{error} = "syntax error: no condition string" unless $string;
71
+        return
72
+    }
73
+
67
     # assign
74
     # assign
68
     $self->{n} = $n;
75
     $self->{n} = $n;
69
     $self->{o} = $o;
76
     $self->{o} = $o;
70
     $self->{v} = \@v;
77
     $self->{v} = \@v;
71
 
78
 
79
+
72
     $self->{parsed}++;
80
     $self->{parsed}++;
73
 }
81
 }
74
 
82
 
77
     my $self    = shift;
85
     my $self    = shift;
78
     my $params  = shift;
86
     my $params  = shift;
79
 
87
 
88
+    return if $self->{error};
89
+
80
     my $name    = $self->{n};       # what is the topic
90
     my $name    = $self->{n};       # what is the topic
81
     my $op      = $self->{o};       # what op to use
91
     my $op      = $self->{o};       # what op to use
82
     my $have    = $self->{v};       # what we have in this step
92
     my $have    = $self->{v};       # what we have in this step
107
 sub match {
117
 sub match {
108
     my $self    = shift;
118
     my $self    = shift;
109
     my $params  = shift;
119
     my $params  = shift;
120
+    $self->{error} = "";
110
     $self->parse();
121
     $self->parse();
111
     $self->{result} = $self->__match($params);
122
     $self->{result} = $self->__match($params);
123
+    return if $self->{error};
112
     return $self->{result}, $self->{warnings};
124
     return $self->{result}, $self->{warnings};
113
 }
125
 }
114
 
126
 

+ 4
- 0
lib/SugarTrail/Template/Master.pm View File

81
         # *   in all other cases (fail, warning), accept
81
         # *   in all other cases (fail, warning), accept
82
         COND: foreach my $condstr (@{ $master_step->{conds} }) {
82
         COND: foreach my $condstr (@{ $master_step->{conds} }) {
83
             my $c = SugarTrail::Template::Condition->new($condstr);
83
             my $c = SugarTrail::Template::Condition->new($condstr);
84
+            unless ($c) {
85
+                $self->{error} = "condition error: $c->{error}";
86
+                return;
87
+            }
84
             my ($r, $w) = $c->match($args);
88
             my ($r, $w) = $c->match($args);
85
 
89
 
86
             # if warnings, store them, prepare to accept and ignore result
90
             # if warnings, store them, prepare to accept and ignore result