Get Inline Error Message

Using ReportingEngine to build Word reports with inline error detection enabled via ReportBuildOptions.InlineErrorMessages.

Issue: In some cases, especially with complex expressions (e.g., comparing nullable numbers to strings), the inline error message spans across multiple Run nodes and becomes difficult to detect using doc.Range.Text.Contains("<<Error!") or even checking 3-Run sequences.

Example Expression:
<<if [bankruptcyType1 == "Chapter 07" && previous_Ch_Seven_BK_Disch_Indicator == "Y"]>>

Expected:

  • Error message like Can not apply operator '==' to operands of type 'System.Nullable<Int64>' and 'System.String' should be detected clearly after BuildReport.

I want to get error message if anything is there how can I get that if there is any error I want that

  bool success=reportingEngine.BuildReport(doc, new JsonDataSource(ms, jOpt), "ds");


  // Check for inline errors using extension
  if (!success)
  {
//  Optional: Save for debugging

 // doc.Save("C:\\Temp\\ErroredOutput.docx");

 throw new InvalidOperationException(
     $"Document generation failed due to inline errors:"
 );
  } 

above code doesn’t works for above case

@Vaibhavi_Lad

Sure, I can help you with that.

When working with ReportingEngine in Aspose.Words for .NET and having inline error detection enabled via ReportBuildOptions.InlineErrorMessages, it’s essential to capture and detect any inline errors effectively. Here’s how you can achieve this:

  1. Enable Inline Error Messages:
    Ensure that the ReportBuildOptions.InlineErrorMessages property is set to true.

  2. Check Doc Range for Errors:
    After building the report, check if any inline error messages exist in the document range.

  3. Extract and Report Any Errors:
    Retrieve error messages and handle them appropriately, such as saving the document or logging errors for later analysis.

Here’s a step-by-step approach:

Step-by-Step Instructions:

  1. Enable Inline Error Messages:

    var reportBuildOptions = new Aspose.Words.Reporting.ReportBuildOptions();
    reportBuildOptions.InlineErrorMessages = true;
    
  2. Build the Report:

    bool success = reportingEngine.BuildReport(doc, new JsonDataSource(ms, jOpt), "ds", reportBuildOptions);
    
  3. Check for Inline Errors:

    if (!success)
    {
       // Optional: Save for debugging
       doc.Save("C:\\Temp\\ErroredOutput.docx");
    
       throw new InvalidOperationException(
           $"Document generation failed due to inline errors:"
       );
    }
    else
    {
       // Check for inline errors even if BuildReport returns true
       bool hasErrors = false;
       string errorMessage = "";
    
       // Traverse through the Runs and check for error text
       foreach (Run run in doc.GetChildNodes(NodeType.Run, true))
       {
           if (run.Text.Contains("<<Error!"))
           {
               hasErrors = true;
               errorMessage += run.Text + " ";
           }
       }
    
       if (hasErrors)
       {
           // Log the error message or handle it as needed
           Console.WriteLine(errorMessage);
           throw new InvalidOperationException(
               $"Document contains inline errors: {errorMessage}"
           );
       }
    }
    

Explanation:

  1. Inline Error Messages:
    Set the InlineErrorMessages property to true before building the report to ensure any errors are embedded into the document.

  2. Build the Report:
    Use reportingEngine.BuildReport to generate the document, keeping ReportBuildOptions as an argument.

  3. Error Detection:
    Traverse through each Run node in the generated document to detect if any text contains <<Error!. Accumulate error messages and throw an exception if errors are found.

Additional Notes:

  • Always ensure that your ReportBuildOptions include InlineErrorMessages set to true to capture inline errors effectively.
  • The BuildReport method returns false if there are parsing-level errors but might not detect inline run errors when the report is built.
  • Traverse the Run nodes to catch specific errors within the text, making sure to accumulate all occurrences.

For more details, please refer to the Aspose.Words for .NET documentation: Aspose.Words for .NET Documentation - Mail Merge and Reporting.

This approach will help you manage and detect inline errors efficiently as per the requirements you’ve specified. If you have further questions or need additional assistance, please let me know.

above suggestion is not able to get error like

<<if [bankruptcyType1== “Chapter 07” && Error! Can not apply operator ‘==’ to operands of type ‘System.Nullable 1[System.Int64]’ and ‘System.String’. bankruptcyStatusType1== “Discharged” && previous_Ch_Seven_BK_Disch_Indicator== “Y” && previous_Ch_Seven_BK_Reaff_Indicator== “N”]>>

how can it detect this error ?

@Vaibhavi_Lad

Since an error may appear at any position even outside a tag, doc.Range.Text.Contains("<<Error!") should be at least changed to doc.Range.Text.Contains(" Error! "). If this does not completely resolve your issue, please consider using doc.Range.Replace with FindReplaceOptions.ReplacingCallback set to an implementation of IReplacingCallback that skips replacing and only stores error text.

Hello, can you provide sample code for this ?

@Vaibhavi_Lad You can fond code example in our documentation:
https://reference.aspose.com/words/net/aspose.words.replacing/ireplacingcallback/

Here is a simple code:

Document doc = new Document(@"C:\Temp\in.docx");
FindReplaceOptions opt = new FindReplaceOptions();
doc.Range.Replace(" Error! ", "", opt);
private class ErrorCollector : IReplacingCallback
{
    public ReplaceAction Replacing(ReplacingArgs args)
    {
        Console.WriteLine(args.Match.Value);
        return ReplaceAction.Skip;
    }
}