guigrpa / docx-templates

Template-based docx report creation
MIT License
882 stars 145 forks source link

Pass command code to `errorHandler` during `processCmd` #368

Open davidjb opened 4 months ago

davidjb commented 4 months ago

In an invalid structured FOR command in a document, such as:

+++FOR person+++

this raises an exception from processForIf which gets caught by the catch after this try block:

    if (ctx.options.errorHandler != null) {
      return ctx.options.errorHandler(err);
    }

Currently, only the err is passed through to the errorHandler, whereas all other calls to the errorHandler are supplied with a second argument, the command code (ref: https://github.com/guigrpa/docx-templates/#custom-error-handler).

This PR adjusts the processCmd flow slightly to ensure that the command code is available in the catch block. The moved splitCommand does not throw exceptions so this is safe to be moved up in order to make cmdRest available to the catch.

Tests have been added for the invalid FOR structure above, both with and without an errorHandler which didn't appear to be tested previously.