Browse Source

Improvement: image loading exception better describes the failure reason (#568)

Marcin Ziąbek 2 years ago
parent
commit
ce7b8c6901
2 changed files with 50 additions and 10 deletions
  1. 27 0
      Source/QuestPDF.UnitTests/ImageTests.cs
  2. 23 10
      Source/QuestPDF/Infrastructure/Image.cs

+ 27 - 0
Source/QuestPDF.UnitTests/ImageTests.cs

@@ -6,6 +6,7 @@ using System.Threading;
 using FluentAssertions;
 using NUnit.Framework;
 using QuestPDF.Drawing;
+using QuestPDF.Drawing.Exceptions;
 using QuestPDF.Elements;
 using QuestPDF.Fluent;
 using QuestPDF.Helpers;
@@ -63,6 +64,32 @@ namespace QuestPDF.UnitTests
                 .CheckMeasureResult(SpacePlan.FullRender(300, 100));;
         }
         
+        [Test]
+        public void ImageObject_ThrowsEncodingException_WhenImageDataIsIncorrect()
+        {
+            Func<Infrastructure.Image> action = () => Infrastructure.Image.FromBinaryData(new byte[] { 1, 2, 3 });
+            action.Should().ThrowExactly<DocumentComposeException>().WithMessage("Cannot decode the provided image.");
+        }
+        
+        [Test]
+        public void ImageObject_ThrowsEncodingException_WhenStreamIsIncorrect()
+        {
+            Func<Infrastructure.Image> action = () =>
+            {
+                using var stream = new MemoryStream(new byte[] { 1, 2, 3 });
+                return Infrastructure.Image.FromStream(stream);
+            };
+
+            action.Should().ThrowExactly<DocumentComposeException>().WithMessage("Cannot decode the provided image.");
+        }
+        
+        [Test]
+        public void ImageObject_ThrowsFileNotFoundException_FileIsNotFound()
+        {
+            Func<Infrastructure.Image> action = () => Infrastructure.Image.FromFile("non-existing-file.jpg");
+            action.Should().ThrowExactly<DocumentComposeException>().WithMessage("Cannot load provided image, file not found: *");
+        }
+
         [Test]
         public void UsingSharedImageShouldNotDrasticallyIncreaseDocumentSize()
         {

+ 23 - 10
Source/QuestPDF/Infrastructure/Image.cs

@@ -54,31 +54,44 @@ namespace QuestPDF.Infrastructure
 
         #region public constructors
 
+        private const string CannotDecodeExceptionMessage = "Cannot decode the provided image.";
+        
         internal static Image FromSkImage(SKImage image)
         {
-            return CreateImage(image);
+            return new Image(image);
         }
 
         public static Image FromBinaryData(byte[] imageData)
         {
-            return CreateImage(SKImage.FromEncodedData(imageData));
+            var image = SKImage.FromEncodedData(imageData);
+            
+            if (image == null)
+                throw new DocumentComposeException(CannotDecodeExceptionMessage);
+            
+            return new Image(image);
         }
 
         public static Image FromFile(string filePath)
         {
-            return CreateImage(SKImage.FromEncodedData(filePath));
-        }
+            var image = SKImage.FromEncodedData(filePath);
 
-        public static Image FromStream(Stream fileStream)
-        {
-            return CreateImage(SKImage.FromEncodedData(fileStream));
+            if (image == null)
+            {
+                throw File.Exists(filePath) 
+                    ? new DocumentComposeException(CannotDecodeExceptionMessage)
+                    : new DocumentComposeException($"Cannot load provided image, file not found: ${filePath}");
+            }
+            
+            return new Image(image);
         }
 
-        private static Image CreateImage(SKImage? image)
+        public static Image FromStream(Stream fileStream)
         {
+            var image = SKImage.FromEncodedData(fileStream);
+            
             if (image == null)
-                throw new DocumentComposeException("Cannot load or decode provided image.");
-
+                throw new DocumentComposeException(CannotDecodeExceptionMessage);
+            
             return new Image(image);
         }