ReportProcessor - Error handling for field mismatch

6 posts, 0 answers
  1. Casper
    Casper avatar
    11 posts
    Member since:
    May 2019

    Posted 30 Jul 2019 Link to this post

    Hi,

    When generating a PDF report using the ReportProcessor from a trdx report,
    is it possible to catch errors such as if a used field in the template is
    not present in the dataset?

    I'm using the ErrorEventHandler like this, but it is not triggered if a used field is not present in dataset.
    It just renders the report anyway and leaves the label empty.

    ...
    reportObject.Error += (innerSender, eventArgs) =>
    {
        _logger.Error(eventArgs.Exception, "Error processing report PDF", reportId, parameters);
    };


    If you don't support it today, it would be great if I could decide, whether or not the processor should trigger the error handler in cases like this.

    Cheers,
    Casper

  2. Ivan Hristov
    Admin
    Ivan Hristov avatar
    220 posts

    Posted 01 Aug 2019 Link to this post

    Hello Casper,

    Such scenario is supported by the reporting engine. If an expression uses a field that is not present in the dataset, the relevant item throws an exception with the message "The expression contains object 'object-name-here' that is not defined in the current context.". This error bubbles up to the error handler of the ReportProcessor and is reflected in Errors property of the RenderingResult object, returned by the ReportProcessor, as the following code demonstrates:

    var rs = new InstanceReportSource() { ReportDocument = new Dashboard() };
    var processor = new Telerik.Reporting.Processing.ReportProcessor();
    processor.Error += ((s, e) =>
    {
        Debug.WriteLine(e.Exception);
    });
     
    var result = processor.RenderReport("PDF", rs, null);
    if (result.HasErrors)
    {
        Debug.WriteLine(string.Join("; ", result.Errors.Select(e => e.Message)));
    }

    To test it, I used our example Dashboard report and changed the expression of the spLabelSalesPerson text box from =Fields.SalesPersonFullName to =Fields.SalesPersonFullName1, which clearly does not exist in the datasource. When the code above is run, the Debug console will contain the string: The expression contains object 'SalesPersonFullName1' that is not defined in the current context., repeated couple of times, because the textbox is evaluated a couple of times.

    If your scenario differs from the one above, please give us more details about it so we can investigate it on our side.

    Regards,
    Ivan Hristov
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. GeneralMac
    GeneralMac avatar
    2 posts
    Member since:
    Apr 2018

    Posted 26 Mar in reply to Ivan Hristov Link to this post

    @Ivan Hirstov, Looking at your code in your post above:
    1.var processor = new Telerik.Reporting.Processing.ReportProcessor();
    2.processor.Error += ((s, e) =>
    3.{
    4.    Debug.WriteLine(e.Exception);
    5.});

    Does the processor.Error ErrorEventHandler have to be defined before result.HasErrors will register any errors?

    1.var result = processor.RenderReport("PDF", rs, null);
    2.if (result.HasErrors)
    3.{
    4.    Debug.WriteLine(string.Join("; ", result.Errors.Select(e => e.Message)));
    5.}
  4. Ivan Hristov
    Admin
    Ivan Hristov avatar
    220 posts

    Posted 27 Mar Link to this post

    Hello,

    The Error event handler must be attached before the report processing commences, which happens in the RenderReport() method. The result.HasErrors property is just a shorthand to check the result.Errors, as shown in the code below:

            /// <summary>
            /// Gets a value that indicates whether the collection contains errors.
            /// </summary>
            public bool HasErrors
            {
                get { return (this.Errors.Length > 0); }
            }
    

    Regards,
    Ivan Hristov
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
  5. GeneralMac
    GeneralMac avatar
    2 posts
    Member since:
    Apr 2018

    Posted 27 Mar in reply to Ivan Hristov Link to this post

    So there isn't a default Error Handler? If there are errors that occur and there isn't an error handler defined will the HasErrors boolean be true?

    Thanks,

  6. Ivan Hristov
    Admin
    Ivan Hristov avatar
    220 posts

    Posted 27 Mar Link to this post

    Hi,

    I hope I correctly understand the question, but the default error handler works internally - captures the processing errors and puts them in the result.Errors collection.

    The workflow goes like this: when a processing error occurs, it is handled by the internal exception handling routine. There the exception is unconditionally added to the internal list of exceptions that later will be returned by the RenderingResult.Errors collection. Then the routine checks if the ReportProcessor's Error handler is assigned and if it is, calls the associated delegate - in my example this is the  Debug.WriteLine(e.Exception);

    In other words, the result.HasErrors will always be true if there are any processing errors in the report, and false if the report was processed without errors - whether or not there is an Error handler attached to the ReportProcessor instance.

    I hope my explanation helped you understand how the errors get caught and propagated to the Event handler, but in case you have further questions, please do not hesitate to ask.

    Regards,
    Ivan Hristov
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
Back to Top