Skip to content

Commit

Permalink
#341 Add result to ObjectCommandResultError
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
emilong authored Dec 28, 2023
1 parent d1713c6 commit c07c393
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/__tests__/error_handling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 3 additions & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/processTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c07c393

Please sign in to comment.