From: Penny Leach <penny@mjollnir.org>
Date: Thu, 23 Jul 2009 16:13:17 +0000 (+0200)
Subject: Fixed some error handling
X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=7c36d47ea22390e386820dfb324090357f865857;p=tks.git

Fixed some error handling
---

diff --git a/lib/Zebra.pm b/lib/Zebra.pm
index 31b4a1d..8ae40ae 100644
--- a/lib/Zebra.pm
+++ b/lib/Zebra.pm
@@ -120,19 +120,23 @@ sub add_time {
         return 0;
     }
 
-    my $zresponse = from_json($res->content)->{command}{response};
+    my $zresponse = from_json($res->content);
 
-    if ($zresponse && $zresponse->{success} && $zresponse->{success} eq 'false') {
-        my @error = @{ from_json($res->content)->{command}{error} };
+    unless ($zresponse->{command}{response}{success} && $zresponse->{command}{response}{success} eq 'true') {
         my $errorstr = '';
-        foreach my $e (@error) {
-            if (ref($e) eq 'HASH') {
-                foreach my $key (keys %$e) {
-                    if ($key ne '@attributes') {
-                        $errorstr .= $e->{$key};
+        if ($zresponse->{command}{error}) {
+            my @error = @{ from_json($res->content)->{command}{error} };
+            foreach my $e (@error) {
+                if (ref($e) eq 'HASH') {
+                    foreach my $key (keys %$e) {
+                        if ($key ne '@attributes') {
+                            $errorstr .= $e->{$key};
+                        }
                     }
                 }
             }
+        } elsif ($zresponse->{exception}{message}) {
+            $errorstr = $zresponse->{exception}{message};
         }
         $self->{_error} = qq(Failed to post timesheet data!, error if any was: $errorstr');
         warn $self->{_error};