From ef4cde18c8fc82cc0c2b5ae40c0901970eb01f4e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Vladim=C3=ADr=20Vittek?= <xvittek@fi.muni.cz>
Date: Sun, 24 Mar 2024 09:36:55 +0100
Subject: [PATCH] feat(scraping-service-exceptions): added custom exceptions

---
 .../ServiceTests/ScrapingServiceTests.cs       |  5 +++--
 .../Exceptions/ArkCsvConverterException.cs     | 18 ++++++++++++++++++
 .../Exceptions/ArkCsvScrapeException.cs        | 18 ++++++++++++++++++
 StockMonitor/Services/ScrapingService.cs       | 11 +++++++++--
 4 files changed, 48 insertions(+), 4 deletions(-)
 create mode 100644 StockMonitor/Exceptions/ArkCsvConverterException.cs
 create mode 100644 StockMonitor/Exceptions/ArkCsvScrapeException.cs

diff --git a/StockMonitor.Tests/ServiceTests/ScrapingServiceTests.cs b/StockMonitor.Tests/ServiceTests/ScrapingServiceTests.cs
index b531a3e..d8d6833 100644
--- a/StockMonitor.Tests/ServiceTests/ScrapingServiceTests.cs
+++ b/StockMonitor.Tests/ServiceTests/ScrapingServiceTests.cs
@@ -1,4 +1,5 @@
-using StockMonitor.Services;
+using StockMonitor.Exceptions;
+using StockMonitor.Services;
 namespace StockMonitor.Tests;
 
 using NUnit.Framework;
@@ -105,7 +106,7 @@ public class ScrapingServiceTests
 
         Assert.Multiple(() =>
         {
-            var ex = Assert.ThrowsAsync<HttpRequestException>(async () => await scrapingService.ScrapeStocks());
+            var ex = Assert.ThrowsAsync<ArkCsvScrapeException>(async () => await scrapingService.ScrapeStocks());
             Assert.IsNotNull(ex);
             Assert.That(ex?.Message, Is.EqualTo(
                 "Failed to scrape CSV, exception: Response status code does not indicate success: 404 (Not Found)."
diff --git a/StockMonitor/Exceptions/ArkCsvConverterException.cs b/StockMonitor/Exceptions/ArkCsvConverterException.cs
new file mode 100644
index 0000000..9974164
--- /dev/null
+++ b/StockMonitor/Exceptions/ArkCsvConverterException.cs
@@ -0,0 +1,18 @@
+namespace StockMonitor.Exceptions;
+
+public class ArkCsvConverterException : Exception
+{
+    public ArkCsvConverterException()
+    {
+    }
+
+    public ArkCsvConverterException(string message)
+        : base(message)
+    {
+    }
+
+    public ArkCsvConverterException(string message, Exception inner)
+        : base(message, inner)
+    {
+    }
+}
diff --git a/StockMonitor/Exceptions/ArkCsvScrapeException.cs b/StockMonitor/Exceptions/ArkCsvScrapeException.cs
new file mode 100644
index 0000000..2b08cc5
--- /dev/null
+++ b/StockMonitor/Exceptions/ArkCsvScrapeException.cs
@@ -0,0 +1,18 @@
+namespace StockMonitor.Exceptions;
+
+public class ArkCsvScrapeException : Exception
+{
+    public ArkCsvScrapeException()
+    {
+    }
+
+    public ArkCsvScrapeException(string message)
+        : base(message)
+    {
+    }
+
+    public ArkCsvScrapeException(string message, Exception inner)
+        : base(message, inner)
+    {
+    }
+}
diff --git a/StockMonitor/Services/ScrapingService.cs b/StockMonitor/Services/ScrapingService.cs
index ad75dc7..d2e2891 100644
--- a/StockMonitor/Services/ScrapingService.cs
+++ b/StockMonitor/Services/ScrapingService.cs
@@ -2,6 +2,7 @@
 using System.Text.Json;
 using CsvHelper;
 using CsvHelper.TypeConversion;
+using StockMonitor.Exceptions;
 using StockMonitor.Models;
 
 namespace StockMonitor.Services
@@ -81,7 +82,7 @@ namespace StockMonitor.Services
             }
             catch (HttpRequestException e)
             {
-                throw new HttpRequestException($"Failed to scrape CSV, exception: {e.Message}");
+                throw new ArkCsvScrapeException($"Failed to scrape CSV, exception: {e.Message}");
             }
 
         }
@@ -95,6 +96,12 @@ namespace StockMonitor.Services
             var stocks = new List<StockModel>();
             while (csvReader.Read())
             {
+                // This specific condition is added to exclude a particular type of information
+                // present in the CSV file, ensuring it is not included in the final result.
+                if (csvReader.GetField("date").StartsWith("Investors should carefully ", StringComparison.InvariantCulture))
+                {
+                    continue;
+                }
                 try
                 {
                     StockModel stock = new()
@@ -110,7 +117,7 @@ namespace StockMonitor.Services
                 }
                 catch (TypeConverterException)
                 {
-                    continue;
+                    throw new ArkCsvConverterException();
                 }
             }
 
-- 
GitLab