Handle Common Template and Data Errors

Overview

Error handling is crucial when generating documents from templates and data sources. GroupDocs.Assembly for .NET provides exceptions that help you identify and resolve issues with templates, data sources, or assembly operations. Understanding common error scenarios and how to handle them improves the reliability of your document generation code.

The main exception types are:

  • DocumentAssemblerException - general assembly errors
  • Template syntax errors
  • Data source errors
  • Missing member errors

Here are the steps to handle errors:

  • Wrap assembly operations in try-catch blocks
  • Catch specific exception types
  • Validate data sources before assembly
  • Handle missing members gracefully
  • Log errors for debugging
Note
GroupDocs.Assembly provides detailed error messages that help identify the exact location and cause of template or data errors.

Basic Error Handling

Wrap assembly operations in try-catch blocks:

using GroupDocs.Assembly;
using System;

public static void BasicErrorHandling()
{
    try
    {
        var data = new { Name = "John Doe", Age = 30 };
        
        DocumentAssembler assembler = new DocumentAssembler();
        assembler.AssembleDocument("Template.docx", "Output.docx", 
            new DataSourceInfo(data, "data"));
        
        Console.WriteLine("Document assembled successfully.");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error assembling document: {ex.Message}");
        // Handle error appropriately
    }
}

Handle Template Syntax Errors

Catch and handle template syntax errors:

using GroupDocs.Assembly;
using System;

public static void HandleTemplateErrors()
{
    try
    {
        var data = new { Name = "John Doe" };
        
        DocumentAssembler assembler = new DocumentAssembler();
        assembler.AssembleDocument("Template.docx", "Output.docx", 
            new DataSourceInfo(data, "data"));
    }
    catch (DocumentAssemblerException ex)
    {
        Console.WriteLine($"Template error: {ex.Message}");
        Console.WriteLine($"Error details: {ex.InnerException?.Message}");
        // Log error, notify user, or use fallback template
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Unexpected error: {ex.Message}");
    }
}

Handle Missing Data Members

Handle cases where template references missing data members:

using GroupDocs.Assembly;
using System;

public static void HandleMissingMembers()
{
    try
    {
        // Data doesn't have all properties referenced in template
        var data = new { Name = "John Doe" };
        // Template might reference data.Email which doesn't exist
        
        DocumentAssembler assembler = new DocumentAssembler();
        
        // Option 1: Allow missing members (returns null/empty)
        assembler.Options |= DocumentAssemblyOptions.AllowMissingMembers;
        
        assembler.AssembleDocument("Template.docx", "Output.docx", 
            new DataSourceInfo(data, "data"));
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}");
    }
}

Validate Data Before Assembly

Validate data sources before assembly:

using GroupDocs.Assembly;
using System;
using System.Collections.Generic;

public static void ValidateDataBeforeAssembly()
{
    var products = new List<Product>();
    // products might be empty or null
    
    if (products == null || products.Count == 0)
    {
        Console.WriteLine("Warning: No products to process.");
        return;
    }
    
    try
    {
        DocumentAssembler assembler = new DocumentAssembler();
        assembler.AssembleDocument("Template.docx", "Output.docx", 
            new DataSourceInfo(products, "products"));
        
        Console.WriteLine("Document assembled successfully.");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}");
    }
}

Handle File Not Found Errors

Check if template file exists:

using GroupDocs.Assembly;
using System;
using System.IO;

public static void HandleFileNotFound()
{
    string templatePath = "Template.docx";
    
    if (!File.Exists(templatePath))
    {
        Console.WriteLine($"Template file not found: {templatePath}");
        return;
    }
    
    try
    {
        var data = new { Name = "John Doe" };
        
        DocumentAssembler assembler = new DocumentAssembler();
        assembler.AssembleDocument(templatePath, "Output.docx", 
            new DataSourceInfo(data, "data"));
        
        Console.WriteLine("Document assembled successfully.");
    }
    catch (FileNotFoundException ex)
    {
        Console.WriteLine($"File not found: {ex.FileName}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}");
    }
}

Handle Data Type Errors

Validate data types before assembly:

using GroupDocs.Assembly;
using System;

public class Invoice
{
    public string CustomerName { get; set; }
    public decimal? Amount { get; set; } // Nullable
}

public static void HandleDataTypeErrors()
{
    var invoice = new Invoice
    {
        CustomerName = "John Doe",
        Amount = null // Might cause issues if template expects non-null
    };
    
    try
    {
        DocumentAssembler assembler = new DocumentAssembler();
        assembler.AssembleDocument("Template.docx", "Output.docx", 
            new DataSourceInfo(invoice, "invoice"));
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Data type error: {ex.Message}");
        // Handle null values or provide defaults
    }
}

Comprehensive Error Handling

Combine multiple error handling strategies:

using GroupDocs.Assembly;
using System;
using System.IO;

public static void ComprehensiveErrorHandling()
{
    string templatePath = "Template.docx";
    string outputPath = "Output.docx";
    
    // Validate inputs
    if (string.IsNullOrEmpty(templatePath) || !File.Exists(templatePath))
    {
        Console.WriteLine("Invalid template path.");
        return;
    }
    
    try
    {
        var data = new { Name = "John Doe", Age = 30 };
        
        // Validate data
        if (data == null)
        {
            Console.WriteLine("Data source is null.");
            return;
        }
        
        DocumentAssembler assembler = new DocumentAssembler();
        
        // Configure error handling options
        assembler.Options |= DocumentAssemblyOptions.AllowMissingMembers;
        
        assembler.AssembleDocument(templatePath, outputPath, 
            new DataSourceInfo(data, "data"));
        
        Console.WriteLine("Document assembled successfully.");
    }
    catch (DocumentAssemblerException ex)
    {
        Console.WriteLine($"Assembly error: {ex.Message}");
        if (ex.InnerException != null)
        {
            Console.WriteLine($"Inner exception: {ex.InnerException.Message}");
        }
        // Log detailed error information
    }
    catch (FileNotFoundException ex)
    {
        Console.WriteLine($"File not found: {ex.FileName}");
    }
    catch (UnauthorizedAccessException ex)
    {
        Console.WriteLine($"Access denied: {ex.Message}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Unexpected error: {ex.Message}");
        Console.WriteLine($"Stack trace: {ex.StackTrace}");
    }
}

Handle Stream Errors

Properly handle stream-related errors:

using GroupDocs.Assembly;
using System;
using System.IO;

public static void HandleStreamErrors()
{
    try
    {
        using (FileStream templateStream = new FileStream("Template.docx", FileMode.Open))
        using (FileStream outputStream = new FileStream("Output.docx", FileMode.Create))
        {
            var data = new { Name = "John Doe" };
            
            DocumentAssembler assembler = new DocumentAssembler();
            assembler.AssembleDocument(templateStream, outputStream, 
                new DataSourceInfo(data, "data"));
        }
    }
    catch (IOException ex)
    {
        Console.WriteLine($"IO error: {ex.Message}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}");
    }
}

Error Handling Best Practices

  1. Always use try-catch blocks around assembly operations
  2. Validate inputs before assembly (file existence, data null checks)
  3. Use specific exception types when possible
  4. Enable AllowMissingMembers option if your data structure is dynamic
  5. Log errors with sufficient detail for debugging
  6. Provide user-friendly error messages in production code
Warning
Some errors might occur during template evaluation. Always test templates with sample data before deploying to production. Consider using template validation tools or test assemblies.

Advanced Usage Topics

To learn more about detailed error logging, template validation, custom error handling, and debugging techniques, please refer to the advanced usage section.

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online Apps

Along with the full-featured .NET library, we provide simple but powerful free online apps.

To assemble documents from templates and data sources, you can use the online apps from the GroupDocs.Assembly App Product Family.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.