Use of In-line Syntax Error Messages into Templates
Use of In-line Syntax Error Messages into Templates
Leave feedback
On this page
In-line template syntax error messages are supported for Word Processing, Presentation, Spreadsheet, Email and Plain Text documents.
Use of in-line syntax error messages
By default, DocumentAssembler throws an exception when it encounters a template syntax error. Such an exception provides information on the reason of the error and specifies the tag or expression part where the error is encountered. In most cases, this information is enough to find the place in a template causing the error and fix it. In Node.js, the exception is a JavaScript Error whose message contains the Java exception text, for example:
java.lang.IllegalStateException: An error has been encountered at the end of expression 'name]>'. An assignment operator is expected.
However, when dealing with complex templates containing a large number of tags, it becomes harder to find the exact place in a template causing an error. To make things easier, the engine supports the DocumentAssemblyOptions.INLINE_ERROR_MESSAGES option that writes a syntax error message into the report at the exact position where the error occurs.
Warning
A template syntax error message is written using a bold font to make it more apparent.
Consider the following template.
<<var [name]>>
By default, such a template causes the engine to throw an exception while building a report. However, when DocumentAssemblyOptions.INLINE_ERROR_MESSAGES is applied, no exception is thrown and the report looks as follows.
<<var [name] Error! An assignment operator is expected. >>
Warning
Only messages describing errors in template syntax can be in-lined. Messages describing errors encountered during expression evaluation are not in-lined.
When DocumentAssemblyOptions.INLINE_ERROR_MESSAGES is applied, the Boolean value returned by DocumentAssembler.assembleDocument indicates whether building of a report was finished successfully (true) or was interrupted because of a template syntax error (false). This enables you to process reports whose building succeeded or failed differently, as shown in the following code snippet.
constgroupdocs=require('@groupdocs/groupdocs.assembly');// Source template and destination PDF report
consttemplatePath="Inline Error Demo.docx";constreportPath="Inline Error Demo.pdf";// Enable in-line error messages
constassembler=newgroupdocs.DocumentAssembler();assembler.setOptions(groupdocs.DocumentAssemblyOptions.INLINE_ERROR_MESSAGES);constdataSource=newgroupdocs.JsonDataSource("ManagerData.json");// assembleDocument returns false if the template contains a syntax error
constsuccess=assembler.assembleDocument(templatePath,reportPath,newgroupdocs.LoadSaveOptions(groupdocs.FileFormat.PDF),newgroupdocs.DataSourceInfo(dataSource,"managers"));if(success){console.log("No error found in the template.");}else{console.log("Do something with a report containing a template syntax error.");}process.exit(0);
Warning
This article uses a Word Processing template. The process for the other file formats is the same.