Add try...catch to GetProduct

This commit is contained in:
Sergey Chebotar 2021-12-07 08:48:35 +03:00
parent e897cae23e
commit adf66ad2a2

View File

@ -7,6 +7,7 @@ using System.Linq;
using System.Runtime.Caching; using System.Runtime.Caching;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms;
namespace Rehau.Sku.Assist namespace Rehau.Sku.Assist
{ {
@ -29,11 +30,13 @@ namespace Rehau.Sku.Assist
return GetProduct(documentTask.Result); return GetProduct(documentTask.Result);
} }
public static IProduct GetProduct(IDocument document) public static IProduct GetProduct(IDocument document)
{
try
{ {
string script = document string script = document
.Scripts .Scripts
.Where(s => s.InnerHtml.Contains("dataLayer")) .Where(s => s.InnerHtml.Contains("dataLayer"))
.First() .FirstOrDefault()
.InnerHtml; .InnerHtml;
string json = script string json = script
@ -52,6 +55,13 @@ namespace Rehau.Sku.Assist
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) public static object GetProduct(string request, ProductField field)
{ {
IProduct product; IProduct product;