Kaynağa Gözat

Add JSON error format handling.
Fix for failures during batch ordering.

Brett Credo 8 yıl önce
ebeveyn
işleme
49c7dcb6a7

+ 9 - 0
BulkPrinting/BulkPrinting/Models.cs

@@ -25,6 +25,15 @@ namespace BulkPrinting
25 25
         Online = 2
26 26
     }
27 27
 
28
+    public class MaxException
29
+    {
30
+        [JsonProperty("code")]
31
+        public int? Code { get; set; }
32
+
33
+        [JsonProperty("error")]
34
+        public string Error { get; set; }
35
+    }
36
+
28 37
 
29 38
     public class LoginData
30 39
     {

+ 8 - 0
BulkPrinting/BulkPrinting/OrderForm.cs

@@ -165,6 +165,14 @@ namespace BulkPrinting
165 165
                         OrderData.InternalReference = InternalReference;
166 166
                         BatchListing OrderedBatch = new BatchListing();
167 167
                         bool OrderResult = Utility.RESTRequest<OrderPlacementData, BatchListing>(OrderData, ref OrderedBatch, "/api/batches/");
168
+                        if (OrderedBatch.Batch == null) {
169
+                            string ErrorMessage = "An error occurred while attempting to order " + OrderedItem.Quantity.ToString() + " of " + OrderedItem.ProductDescription;
170
+                            if (i < (SelectedProducts.Count - 1)){
171
+                                ErrorMessage += "\nPress OK to continue attempting to order remaining items.";
172
+                            }
173
+                            MessageBox.Show(ErrorMessage ,"Error Ordering",MessageBoxButtons.OK,MessageBoxIcon.Error);
174
+                            continue;
175
+                        }
168 176
                         //TODO: Handle failed order
169 177
                         Utility.DownloadBatch(OrderedBatch.Batch);
170 178
                         Globals.SessionData.Credentials.Payload.User.Account.Balance = OrderedBatch.RemainingBalance;

+ 0 - 4
BulkPrinting/BulkPrinting/UserLoginForm.cs

@@ -131,10 +131,6 @@ namespace BulkPrinting
131 131
                         Utility.SyncLogs(); //Perform log sync before any logging happens to ensure synchronicity with server
132 132
                     }
133 133
                 }
134
-                else
135
-                {
136
-                    MessageBox.Show("The login attempt failed. Please try again.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
137
-                }
138 134
             }
139 135
             if (LoginSuccessful == true)
140 136
             {

+ 31 - 36
BulkPrinting/BulkPrinting/Utility.cs

@@ -92,7 +92,24 @@ namespace BulkPrinting
92 92
 
93 93
                 return true;
94 94
             }
95
-            catch (WebException) {
95
+            catch (WebException ex)
96
+            {
97
+                if (ex != null && ex.Response != null)
98
+                {
99
+                    var response = ex.Response;
100
+                    var stream = response.GetResponseStream();
101
+                    var reader = new StreamReader(stream);
102
+                    var message = reader.ReadToEnd();
103
+                    MaxException MaxError = JsonConvert.DeserializeObject<MaxException>(message);
104
+                    if (MaxError.Code != null)
105
+                    {
106
+                        MessageBox.Show("Error " + MaxError.Code.ToString() + ": " + MaxError.Error, "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
107
+                    }
108
+                    else
109
+                    {
110
+                        MessageBox.Show("Login Failed. Please try again.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
111
+                    }
112
+                }
96 113
                 return false;
97 114
             }
98 115
         }
@@ -135,45 +152,23 @@ namespace BulkPrinting
135 152
                 Result = JsonConvert.DeserializeObject<R>(responseString);
136 153
                 return true;
137 154
             }
138
-            catch (WebException)
155
+            catch (WebException ex)
139 156
             {
157
+                if (ex != null && ex.Response != null)
158
+                {
159
+                    var response = ex.Response;
160
+                    var stream = response.GetResponseStream();
161
+                    var reader = new StreamReader(stream);
162
+                    var message = reader.ReadToEnd();
163
+                    MaxException MaxError = JsonConvert.DeserializeObject<MaxException>(message);
164
+                    if (MaxError.Code != null)
165
+                    {
166
+                        MessageBox.Show("Error " + MaxError.Code.ToString() + ": " + MaxError.Error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
167
+                    }
168
+                }
140 169
                 return false;
141 170
             }
142 171
         }
143
-        /*
144
-        private static bool RegSave(string Key, string Value)
145
-        {
146
-            bool Result;
147
-            try
148
-            {
149
-                RegistryKey MaxRegistry = Registry.CurrentUser.OpenSubKey("Software\\M@X\\BulkPrint");
150
-                Registry.SetValue(MaxRegistry.Name, Key, Value);
151
-                Registry.CurrentUser.Close();
152
-                Result = true;
153
-            }
154
-            catch (Exception)
155
-            {
156
-                Result = false;
157
-            }
158
-            return Result;
159
-        }
160
-
161
-        public static string RegFetch(string Key)
162
-        {
163
-            string Result;
164
-            try
165
-            {
166
-                RegistryKey MaxRegistry = Registry.CurrentUser.OpenSubKey("Software\\M@X\\BulkPrint");
167
-                Result = Registry.GetValue(MaxRegistry.Name, Key, "").ToString();
168
-                Registry.CurrentUser.Close();
169
-            }
170
-            catch (Exception)
171
-            {
172
-                Result = "";
173
-            }
174
-            return Result;
175
-        }
176
-        */
177 172
 
178 173
         private static bool SaveSetting(string Key, string Value)
179 174
         {