Browse Source

Added error handling for Condition

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

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

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