Browse Source

Improved exception message when SkiaSharp throws the TypeInitializationException

MarcinZiabek 3 năm trước cách đây
mục cha
commit
d4103797aa

+ 4 - 5
QuestPDF/Drawing/DocumentGenerator.cs

@@ -135,11 +135,10 @@ namespace QuestPDF.Drawing
                               $"In this case, please increase the value {nameof(DocumentMetadata)}.{nameof(DocumentMetadata.DocumentLayoutExceptionThreshold)} property configured in the {nameof(IDocument.GetMetadata)} method. " +
                               $"In this case, please increase the value {nameof(DocumentMetadata)}.{nameof(DocumentMetadata.DocumentLayoutExceptionThreshold)} property configured in the {nameof(IDocument.GetMetadata)} method. " +
                               $"2) The layout configuration of your document is invalid. Some of the elements require more space than is provided." +
                               $"2) The layout configuration of your document is invalid. Some of the elements require more space than is provided." +
                               $"Please analyze your documents structure to detect this element and fix its size constraints.";
                               $"Please analyze your documents structure to detect this element and fix its size constraints.";
-                
-                throw new DocumentLayoutException(message)
-                {
-                    ElementTrace = debuggingState?.BuildTrace() ?? "Debug trace is available only in the DEBUG mode."
-                };
+
+                var elementTrace = debuggingState?.BuildTrace() ?? "Debug trace is available only in the DEBUG mode.";
+
+                throw new DocumentLayoutException(message, elementTrace);
             }
             }
         }
         }
 
 

+ 1 - 11
QuestPDF/Drawing/Exceptions/DocumentComposeException.cs

@@ -4,17 +4,7 @@ namespace QuestPDF.Drawing.Exceptions
 {
 {
     public class DocumentComposeException : Exception
     public class DocumentComposeException : Exception
     {
     {
-        public DocumentComposeException()
-        {
-            
-        }
-
-        public DocumentComposeException(string message) : base(message)
-        {
-            
-        }
-
-        public DocumentComposeException(string message, Exception inner) : base(message, inner)
+        internal DocumentComposeException(string message) : base(message)
         {
         {
             
             
         }
         }

+ 1 - 11
QuestPDF/Drawing/Exceptions/DocumentDrawingException.cs

@@ -4,17 +4,7 @@ namespace QuestPDF.Drawing.Exceptions
 {
 {
     public class DocumentDrawingException : Exception
     public class DocumentDrawingException : Exception
     {
     {
-        public DocumentDrawingException()
-        {
-            
-        }
-
-        public DocumentDrawingException(string message) : base(message)
-        {
-            
-        }
-
-        public DocumentDrawingException(string message, Exception inner) : base(message, inner)
+        internal DocumentDrawingException(string message, Exception inner) : base(message, inner)
         {
         {
             
             
         }
         }

+ 3 - 13
QuestPDF/Drawing/Exceptions/DocumentLayoutException.cs

@@ -4,21 +4,11 @@ namespace QuestPDF.Drawing.Exceptions
 {
 {
     public class DocumentLayoutException : Exception
     public class DocumentLayoutException : Exception
     {
     {
-        public string ElementTrace { get; set; }
-        
-        public DocumentLayoutException()
-        {
-            
-        }
-
-        public DocumentLayoutException(string message) : base(message)
-        {
-            
-        }
+        public string? ElementTrace { get; }
 
 
-        public DocumentLayoutException(string message, Exception inner) : base(message, inner)
+        internal DocumentLayoutException(string message, string? elementTrace = null) : base(message)
         {
         {
-            
+            ElementTrace = elementTrace;
         }
         }
     }
     }
 }
 }

+ 20 - 0
QuestPDF/Drawing/Exceptions/InitializationException.cs

@@ -0,0 +1,20 @@
+using System;
+
+namespace QuestPDF.Drawing.Exceptions
+{
+    public class InitializationException : Exception
+    {
+        internal InitializationException(string documentType, Exception innerException) : base(CreateMessage(documentType), innerException)
+        {
+            
+        }
+
+        private static string CreateMessage(string documentType)
+        {
+            return $"Cannot create the {documentType} document using the SkiaSharp library. " +
+                   $"This exception usually means that, on your operating system where you run the application, SkiaSharp requires installing additional dependencies. " +
+                   $"Such dependencies are available as additional nuget packages, for example SkiaSharp.NativeAssets.Linux. " +
+                   $"Please refer to the SkiaSharp documentation for more details.";
+        }
+    }
+}

+ 17 - 3
QuestPDF/Drawing/PdfCanvas.cs

@@ -1,4 +1,6 @@
-using System.IO;
+using System;
+using System.IO;
+using QuestPDF.Drawing.Exceptions;
 using QuestPDF.Helpers;
 using QuestPDF.Helpers;
 using SkiaSharp;
 using SkiaSharp;
 
 
@@ -7,11 +9,23 @@ namespace QuestPDF.Drawing
     internal class PdfCanvas : SkiaDocumentCanvasBase
     internal class PdfCanvas : SkiaDocumentCanvasBase
     {
     {
         public PdfCanvas(Stream stream, DocumentMetadata documentMetadata) 
         public PdfCanvas(Stream stream, DocumentMetadata documentMetadata) 
-            : base(SKDocument.CreatePdf(stream, MapMetadata(documentMetadata)))
+            : base(CreatePdf(stream, documentMetadata))
         {
         {
             
             
         }
         }
-        
+
+        private static SKDocument CreatePdf(Stream stream, DocumentMetadata documentMetadata)
+        {
+            try
+            {
+                return SKDocument.CreatePdf(stream, MapMetadata(documentMetadata));
+            }
+            catch (TypeInitializationException exception)
+            {
+                throw new InitializationException("PDF", exception);
+            }
+        }
+
         private static SKDocumentPdfMetadata MapMetadata(DocumentMetadata metadata)
         private static SKDocumentPdfMetadata MapMetadata(DocumentMetadata metadata)
         {
         {
             return new SKDocumentPdfMetadata
             return new SKDocumentPdfMetadata

+ 16 - 2
QuestPDF/Drawing/XpsCanvas.cs

@@ -1,4 +1,6 @@
-using System.IO;
+using System;
+using System.IO;
+using QuestPDF.Drawing.Exceptions;
 using QuestPDF.Helpers;
 using QuestPDF.Helpers;
 using SkiaSharp;
 using SkiaSharp;
 
 
@@ -7,9 +9,21 @@ namespace QuestPDF.Drawing
     internal class XpsCanvas : SkiaDocumentCanvasBase
     internal class XpsCanvas : SkiaDocumentCanvasBase
     {
     {
         public XpsCanvas(Stream stream, DocumentMetadata documentMetadata) 
         public XpsCanvas(Stream stream, DocumentMetadata documentMetadata) 
-            : base(SKDocument.CreateXps(stream, documentMetadata.RasterDpi))
+            : base(CreateXps(stream, documentMetadata))
         {
         {
             
             
         }
         }
+        
+        private static SKDocument CreateXps(Stream stream, DocumentMetadata documentMetadata)
+        {
+            try
+            {
+                return SKDocument.CreateXps(stream, documentMetadata.RasterDpi);
+            }
+            catch (TypeInitializationException exception)
+            {
+                throw new InitializationException("XPS", exception);
+            }
+        }
     }
     }
 }
 }

+ 1 - 0
QuestPDF/Resources/ReleaseNotes.txt

@@ -1,3 +1,4 @@
 Implemented support for the text shaping algorithm that fixes rendering more advanced languages such as Arabic. 
 Implemented support for the text shaping algorithm that fixes rendering more advanced languages such as Arabic. 
+Improved exception message when SkiaSharp throws the TypeInitializationException (when additional dependencies are needed).
 Fixed: a rare case when the Row.AutoItem does not calculate properly the width of its content.
 Fixed: a rare case when the Row.AutoItem does not calculate properly the width of its content.
 Fixed: the QuestPDF Previewer does not work with content-rich documents.
 Fixed: the QuestPDF Previewer does not work with content-rich documents.