When KeyNotFoundException was thrown in Aspose.Words How can I know which key I missed?


#1

Hi,

We use .NET Aspose.Words.17.9 in Production in my workplace.
At present when the Dictionary miss the Keyword, Aspose.Words throw KeyNotFoundException, which is fantastic to show the user the issue to not generate the document was that they they’ve potentially misspelled a variable name.

However the challenge comes when there were much changes to a template. And when user doesn’t know which one was wrong. It becomes a tedious and time consuming task to go through one by one, removing and adding back, to see which one caused the issue.

Is there a smarter way to program the Aspose.Words use, through the .NET product, that can tell the user which exact keyword was missing? ie: The root-cause of the KeyNotfoundException?
At present I find the InnerException is null. So there is no way for me to grab that information.

This is a stopper for us to move forward.

If the library needs to change, can you let us know how much forward can I go with my current license without purchasing another license?

Regards
Madu


#2

@SydMK

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.


#3

Hi @tahir.manzoor,

See the attached file. That includes:

  • A sample word template that will help you to re-produce the error I was telling (MADU_TEST.docx).
  • And a sample output PDF. This will give you an idea of what to expect in a successful run. (When no error. Meaning when <<[dd[“key3”]]>> is not referred in the template, because I am not setting this on the C# source. See Program.cs)
  • Sample Console C# application. This will give you the KeyNotFoundException. (AsposeTestConsoleApplication1.zip)
  • And a separate document detailing the issue, with screenshot+stacktrace (AsposeWordDotNetProblemStatement.docx)

Hope now you are able to get this sorted for us soon.

Regards
Madu

PS I uploaded the Zip file twice, but I can’t find any link to that in this ticket. So I assume didn’t work.
Creating a shared Dropbox link as an alternative means: https://www.dropbox.com/s/bw9t2uct0vmqmo3/AsposeWordTest_AllInOne.zip?dl=0

Please see this conversation from StackOverflow (See the answer from @abatishchev and @Alex Wiese). They discuss how the “Key” can be presented to the user.:

But then they talk about a basic implementation, but with any Type allowance, the whole thing for Aspose may be a pretty sophisticated consideration. But that’s me from a million feet away from real Aspose code… So I’ll leave this with the experts.


#4

@SydMK

We have logged this problem in our issue tracking system as WORDSNET-18545. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.


#5

Hi @tahir.manzoor

Can you confirm the file attachment is downloaded by you guys and that I can delete now to recover my Dropbox space please?

Also is there a link online that I can get for the WORDSNET-18545 ticket?

Regards
Madu


#6

@SydMK

Yes, we download the files. You can delete the attachment.

Our issue tracking system is not public. However, you can ask for update in this thread.


#7

@SydMK

It is to inform you that we have closed the issue (WORDSNET-18545) with “Won’t Fix” resolution.

The exception is thrown upon evaluation of an expression specified in the template. The engine does not throw it on its own. So no changes at the engine’s end are required and the issue can be completely resolved in code at yours end as shown in the following code snippet.

public class VerboseKeyNotFoundException<TKey> : KeyNotFoundException
{
    public VerboseKeyNotFoundException(string message, TKey key, Exception innerException)
        : base(message, innerException)
    {
        Key = key;
    }

    public TKey Key { get; private set; }
}

public class VerboseDictionary<TKey, TValue> : Dictionary<TKey, TValue>
{
    public new TValue this[TKey key]
    {
        get
        {
            try
            {
                return base[key];
            }
            catch (KeyNotFoundException ex)
            {
                throw new VerboseKeyNotFoundException<TKey>(ex.Message, key, ex);
            }
        }
        set
        {
            base[key] = value;
        }
    }
}

Document doc = new Document(MyDir + "input.docx");

var rptEngine = new ReportingEngine();

rptEngine.Options = ReportBuildOptions.AllowMissingMembers;
rptEngine.Options |= ReportBuildOptions.InlineErrorMessages;

VerboseDictionary<string, string> dict = new VerboseDictionary<string, string>();
dict.Add("key1", "value1");
dict.Add("key2", "value2");
//dict.Add("key3", "value2");
try
{
    rptEngine.BuildReport(doc, dict, "dd");
}
catch (VerboseKeyNotFoundException<string> ex)
{
    // Process the key object from the exception as per your requirements.
    Console.WriteLine(ex.Key);
    throw;
}
doc.UpdateFields();

doc.Save(MyDir + "output.pdf");

#8

Thanks Tahir for getting back on this one. Looks like I missed something. I’ll give this a go.

Regards
Madu