Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass command code to errorHandler during processCmd #368

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/__tests__/__snapshots__/error_handling.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,40 @@ exports[`noSandbox custom ErrorHandler properly handles InvalidCommandError 2`]
]
`;

exports[`noSandbox custom ErrorHandler properly handles nested InvalidCommandError from invalid FOR 1`] = `
"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:oel="http://schemas.microsoft.com/office/2019/extlst" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14">
<w:body>
<w:p w14:paraId="04486982" w14:textId="7826500D" w:rsidR="00413949" w:rsidRPr="004E179E" w:rsidRDefault="004E179E" w:rsidP="004E179E">
<w:r>
<w:t xml:space="preserve">Error: Invalid FOR command: FOR person (person)</w:t>
</w:r>
<w:r>
<w:t xml:space="preserve"></w:t>
</w:r>
</w:p>
<w:sectPr w:rsidR="00413949" w:rsidRPr="004E179E">
<w:pgSz w:w="12240" w:h="15840"/>
<w:pgMar w:top="1417" w:right="1701" w:bottom="1417" w:left="1701" w:header="708" w:footer="708" w:gutter="0"/>
<w:cols w:space="708"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>"
`;

exports[`noSandbox custom ErrorHandler properly handles nested InvalidCommandError from invalid FOR 2`] = `
[
[Error: Invalid FOR command: FOR person],
]
`;

exports[`noSandbox custom ErrorHandler properly handles nested InvalidCommandError from invalid FOR 3`] = `
[
"person",
]
`;

exports[`noSandbox rejectNullish setting IMAGE 1`] = `"Result of command qr(\`some random string\`) is null or undefined and rejectNullish is set"`;

exports[`sandbox custom ErrorHandler allows graceful handling of NullishCommandResultError 1`] = `
Expand Down Expand Up @@ -288,6 +322,40 @@ exports[`sandbox custom ErrorHandler properly handles InvalidCommandError 2`] =
]
`;

exports[`sandbox custom ErrorHandler properly handles nested InvalidCommandError from invalid FOR 1`] = `
"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:oel="http://schemas.microsoft.com/office/2019/extlst" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14">
<w:body>
<w:p w14:paraId="04486982" w14:textId="7826500D" w:rsidR="00413949" w:rsidRPr="004E179E" w:rsidRDefault="004E179E" w:rsidP="004E179E">
<w:r>
<w:t xml:space="preserve">Error: Invalid FOR command: FOR person (person)</w:t>
</w:r>
<w:r>
<w:t xml:space="preserve"></w:t>
</w:r>
</w:p>
<w:sectPr w:rsidR="00413949" w:rsidRPr="004E179E">
<w:pgSz w:w="12240" w:h="15840"/>
<w:pgMar w:top="1417" w:right="1701" w:bottom="1417" w:left="1701" w:header="708" w:footer="708" w:gutter="0"/>
<w:cols w:space="708"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>"
`;

exports[`sandbox custom ErrorHandler properly handles nested InvalidCommandError from invalid FOR 2`] = `
[
[Error: Invalid FOR command: FOR person],
]
`;

exports[`sandbox custom ErrorHandler properly handles nested InvalidCommandError from invalid FOR 3`] = `
[
"person",
]
`;

exports[`sandbox rejectNullish setting IMAGE 1`] = `"Result of command qr(\`some random string\`) is null or undefined and rejectNullish is set"`;

exports[`throw when result of INS command is an object 1`] = `"Result of command 'companies' is an object"`;
Expand Down
40 changes: 40 additions & 0 deletions src/__tests__/error_handling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,33 @@ const getError = async <TError>(call: () => unknown): Promise<TError> => {
})
).rejects.toThrow('yeah, no!');
});

it('properly handles nested InvalidCommandError from invalid FOR', async () => {
const template = await fs.promises.readFile(
path.join(__dirname, 'fixtures', 'invalidForCmd.docx')
);

const errs: Error[] = [];
const cmds: (string | undefined)[] = [];
expect(
await createReport(
{
noSandbox,
template,
data: {},
errorHandler: (err, code) => {
errs.push(err);
cmds.push(code);
return `${err} (${code})`;
},
},
'XML'
)
).toMatchSnapshot();

expect(errs).toMatchSnapshot();
expect(cmds).toMatchSnapshot();
});
});
});

Expand Down Expand Up @@ -336,6 +363,19 @@ const getError = async <TError>(call: () => unknown): Promise<TError> => {
`Unterminated FOR-loop ('FOR c'). Make sure each FOR loop has a corresponding END-FOR command.`
);
});

it('Incomplete loop statement: invalid FOR', async () => {
const template = await fs.promises.readFile(
path.join(__dirname, 'fixtures', 'invalidForCmd.docx')
);
await expect(
createReport({
noSandbox,
template,
data: {},
})
).rejects.toThrow('Invalid FOR command: FOR person');
});
});

describe('errors from different realms', () => {
Expand Down
Binary file added src/__tests__/fixtures/invalidForCmd.docx
Binary file not shown.
4 changes: 2 additions & 2 deletions src/processTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,8 @@ const processCmd: CommandProcessor = async (
): Promise<undefined | string | Error> => {
const cmd = getCommand(ctx.cmd, ctx.shorthands, ctx.options.fixSmartQuotes);
ctx.cmd = ''; // flush the context
const { cmdName, cmdRest } = splitCommand(cmd);
try {
const { cmdName, cmdRest } = splitCommand(cmd);
if (cmdName !== 'CMD_NODE') logger.debug(`Processing cmd: ${cmd}`);
// Seeking query?
if (ctx.fSeekQuery) {
Expand Down Expand Up @@ -719,7 +719,7 @@ const processCmd: CommandProcessor = async (
} catch (err) {
if (!isError(err)) throw err;
if (ctx.options.errorHandler != null) {
return ctx.options.errorHandler(err);
return ctx.options.errorHandler(err, cmdRest);
}
return err;
}
Expand Down
Loading