diff --git a/src/__tests__/error_handling.test.ts b/src/__tests__/error_handling.test.ts index b45af09..01538bf 100644 --- a/src/__tests__/error_handling.test.ts +++ b/src/__tests__/error_handling.test.ts @@ -238,6 +238,30 @@ if (process.env.DEBUG) setDebugLogSink(console.log); ).rejects.toThrowErrorMatchingSnapshot(); }); + it('attaches the result to ObjectCommandResultError', async () => { + const template = await fs.promises.readFile( + path.join(__dirname, 'fixtures', 'objectCommandResultError.docx') + ); + + await expect( + createReport({ + noSandbox, + template, + data: { + companies: { + one: 'FIRST', + two: 'SECOND', + three: 'THIRD', + }, + }, + }) + ).rejects.toHaveProperty('result', { + one: 'FIRST', + two: 'SECOND', + three: 'THIRD', + }); + }); + it('Incomplete conditional statement: missing END-IF', async () => { const template = await fs.promises.readFile( path.join(__dirname, 'fixtures', 'missingEndIf.docx') diff --git a/src/errors.ts b/src/errors.ts index 9355f37..4bf18a2 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -19,10 +19,12 @@ export class NullishCommandResultError extends Error { */ export class ObjectCommandResultError extends Error { command: string; - constructor(command: string) { + result: any; + constructor(command: string, result: any) { super(`Result of command '${command}' is an object`); Object.setPrototypeOf(this, ObjectCommandResultError.prototype); this.command = command; + this.result = result; } } diff --git a/src/processTemplate.ts b/src/processTemplate.ts index ff289ef..2ef90a6 100755 --- a/src/processTemplate.ts +++ b/src/processTemplate.ts @@ -580,7 +580,7 @@ const processCmd: CommandProcessor = async ( return ''; } if (typeof result === 'object' && !Array.isArray(result)) { - const nerr = new ObjectCommandResultError(cmdRest); + const nerr = new ObjectCommandResultError(cmdRest, result); if (ctx.options.errorHandler != null) { result = await ctx.options.errorHandler(nerr, cmdRest); } else {