PC1.2: How should/can errors be handled?

Hi,

while beginning to understand how PReS Connect works when creating documents, I wonder if there are any proposals how to handle errors.

Example: using “loadhtml” to load an HTML-snippet is a fine option, but what, if that fails? First question: how can this be detected? Second question: how can the current document be “canceled” or suppressed to be not printed, sent out etc.?

Is this typically something that needs to be done in the DataMapper before the Layout-process takes place or in Designer during formatting?

Horrido,

Thomas

1 Like

To answer your first question. In order to check if the result object of the loadhtml returned information you could use the following code:

var snippetObj = loadhtml('snippets/foo.html');

if(!isEmpty(snippetObj)){
    results.after(snippetObj);
}

function isEmpty(obj) {
    for (var key in obj) {
        if (hasOwnProperty.call(obj, key)) return false;
    }
    return true;
}

The Designer/Merge Engine will be extended in a future version with standard options to handle errors and skip records. At this stage you could use a Control script to implement error handling and skip print records (actually disabling sections and works only for the Print context). As Control scripts run before the pagination process this approach only works when you can predict that certain code may encounter issues or create conditions based on data values. The following code implements the above check in a control script. Subsequently we call a PlanetPress/PReS workflow using the loadhtml() command and pass the ID of the current record (you could add more stuff). The workflow starts with a HTTP Server Input and could write the incoming info to disk/csv/database.

var snippetObj = loadhtml('snippets/foo.html');

if(isEmpty(snippetObj)){
    // Call a pp workflow process called 'error', pass info about the data record
    var error = loadhtml('http://localhost:8080/error/?id=' + record.fields.id);
    // Skip our print section
    merge.template.contexts.PRINT.sections['Section 1'].enabled = false;
}

function isEmpty(obj) {
    for (var key in obj) {
        if (hasOwnProperty.call(obj, key)) return false;
    }
    return true;
}

Hope this is of some help,

Erik

Hi Erik,

thanks for this explanation.

If we need to handle errors right now, we know how to do this, while waiting for the integrated way.

Horrido,

Thomas