Skip to content

Commit

Permalink
Merge pull request #808 from chromaui/matt/add-on-task-error-option
Browse files Browse the repository at this point in the history
Add `onTaskError` option to report errors to node consumers
  • Loading branch information
Matthew Weeks authored Sep 5, 2023
2 parents 5b928dc + 2d12660 commit a5e8204
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
21 changes: 21 additions & 0 deletions node-src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ const getContext = (
},
packagePath: '',
statsPath: 'preview-stats.json',
options: {},
...parseArgs(argv),
} as any;
};
Expand All @@ -351,6 +352,26 @@ it('fails on missing project token', async () => {
expect(ctx.testLogger.errors[0]).toMatch(/Missing project token/);
});

// Note this tests options errors, but not fatal task or runtime errors.
it('passes options error to onTaskError', async () => {
const ctx = getContext([]);
ctx.options = {
onTaskError: jest.fn(),
} as any;

ctx.options.onTaskError = jest.fn();
ctx.env.CHROMATIC_PROJECT_TOKEN = '';
await runBuild(ctx);

await expect(ctx.options.onTaskError).toHaveBeenCalledWith(
expect.anything(), // Context
expect.objectContaining({
formattedError: expect.stringContaining('Missing project token'), // Long formatted error fatalError https://github.com/chromaui/chromatic-cli/blob/217e77671179748eb4ddb8becde78444db93d067/node-src/ui/messages/errors/fatalError.ts#L11
originalError: expect.any(Error),
})
);
});

it('runs in simple situations', async () => {
const ctx = getContext(['--project-token=asdf1234']);
await runBuild(ctx);
Expand Down
5 changes: 5 additions & 0 deletions node-src/runBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export async function runBuild(ctx: Context, extraOptions?: Partial<Options>) {
} catch (e) {
ctx.log.info('');
ctx.log.error(fatalError(ctx, [e]));
ctx.options.onTaskError?.(ctx, { formattedError: fatalError(ctx, [e]), originalError: e });
setExitCode(ctx, exitCodes.INVALID_OPTIONS, true);
return;
}
Expand Down Expand Up @@ -71,6 +72,10 @@ export async function runBuild(ctx: Context, extraOptions?: Partial<Options>) {
}
} catch (error) {
const errors = [].concat(error); // GraphQLClient might throw an array of errors
ctx.options.onTaskError?.(ctx, {
formattedError: fatalError(ctx, errors),
originalError: error,
});

if (errors.length && !ctx.userError) {
ctx.log.info('');
Expand Down
6 changes: 6 additions & 0 deletions node-src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ export interface Options {
/** A callback that is called at the completion of each task */
onTaskComplete?: (ctx: Context) => void;

/** A callback that is called if a task fails */
onTaskError?: (
ctx: Context,
{ formattedError, originalError }: { formattedError: string; originalError: Error | Error[] }
) => void;

/** A callback that is called during tasks that have incremental progress */
onTaskProgress?: (
ctx: Context,
Expand Down

0 comments on commit a5e8204

Please sign in to comment.