From c07c39327b7191dd2e2f65fc604fbd9ff0b04c70 Mon Sep 17 00:00:00 2001 From: Emil Ong Date: Thu, 28 Dec 2023 07:29:19 -0800 Subject: [PATCH] #341 Add result to ObjectCommandResultError When a ObjectCommandResultError is thrown, this change attaches the computed result to the error in case a custom error handler wants to use it for some reason. --- src/__tests__/error_handling.test.ts | 24 ++++++++++++++++++++++++ src/errors.ts | 4 +++- src/processTemplate.ts | 2 +- 3 files changed, 28 insertions(+), 2 deletions(-) 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 {