diff options
author | Sergey Chebotar <s.chebotar@gmail.com> | 2021-12-07 08:48:35 +0300 |
---|---|---|
committer | Sergey Chebotar <s.chebotar@gmail.com> | 2021-12-07 08:48:35 +0300 |
commit | adf66ad2a2c358ec8d3edfc92f96ba0376888e74 (patch) | |
tree | 09500b7a8726fc9f0552976e9b2b3a526ac80787 /Source | |
parent | e897cae23efa6c2e1ef0ad9bae6782504be7d87a (diff) |
Add try...catch to GetProduct
Diffstat (limited to 'Source')
-rw-r--r-- | Source/Assistant/SkuAssist.cs | 44 |
1 files changed, 27 insertions, 17 deletions
diff --git a/Source/Assistant/SkuAssist.cs b/Source/Assistant/SkuAssist.cs index a524daa..6d72a72 100644 --- a/Source/Assistant/SkuAssist.cs +++ b/Source/Assistant/SkuAssist.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Runtime.Caching; using System.Text.RegularExpressions; using System.Threading.Tasks; +using System.Windows.Forms; namespace Rehau.Sku.Assist { @@ -30,27 +31,36 @@ namespace Rehau.Sku.Assist } public static IProduct GetProduct(IDocument document) { - string script = document - .Scripts - .Where(s => s.InnerHtml.Contains("dataLayer")) - .First() - .InnerHtml; + try + { + string script = document + .Scripts + .Where(s => s.InnerHtml.Contains("dataLayer")) + .FirstOrDefault() + .InnerHtml; - string json = script - .Substring(script.IndexOf("push(") + 5) - .TrimEnd(new[] { ')', ';', '\n', ' ' }); + string json = script + .Substring(script.IndexOf("push(") + 5) + .TrimEnd(new[] { ')', ';', '\n', ' ' }); - if (!json.Contains("impressions")) - return null; + if (!json.Contains("impressions")) + return null; - StoreResponce storeResponse = JsonConvert.DeserializeObject<StoreResponce>(json); - IProduct product = storeResponse - .Ecommerce - .Impressions - .Where(p => p.Id.IsRehauSku()) - .FirstOrDefault(); + StoreResponce storeResponse = JsonConvert.DeserializeObject<StoreResponce>(json); + IProduct product = storeResponse + .Ecommerce + .Impressions + .Where(p => p.Id.IsRehauSku()) + .FirstOrDefault(); + + return product; + } - return product; + catch (NullReferenceException e) + { + MessageBox.Show(e.Message, "Ошибка получения данных", MessageBoxButtons.OK, MessageBoxIcon.Error); + return null; + } } public static object GetProduct(string request, ProductField field) { |