Skip to content

Commit

Permalink
Improvement: image loading exception better describes the failure rea…
Browse files Browse the repository at this point in the history
…son (QuestPDF#568)
  • Loading branch information
MarcinZiabek authored Jun 28, 2023
1 parent 09d85cc commit ce7b8c6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
27 changes: 27 additions & 0 deletions Source/QuestPDF.UnitTests/ImageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using FluentAssertions;
using NUnit.Framework;
using QuestPDF.Drawing;
using QuestPDF.Drawing.Exceptions;
using QuestPDF.Elements;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
Expand Down Expand Up @@ -63,6 +64,32 @@ public void Fluent_RecognizesImageProportions()
.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()
{
Expand Down
33 changes: 23 additions & 10 deletions Source/QuestPDF/Infrastructure/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,44 @@ internal SKImage GetVersionOfSize(GetImageVersionRequest request)

#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);
}

Expand Down

0 comments on commit ce7b8c6

Please sign in to comment.