Skip to content

Commit

Permalink
feat(core): improve error messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMason committed Sep 12, 2019
1 parent f0b07e2 commit 0061a48
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/interactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { resolve } from 'path';
import { WriteStream } from 'tty';
import { Branch, Leaf, Node } from '.';
import { renderToCli } from './lib/markdown';
import { tryPanic } from './lib/try-panic';
import { createTreeInterpreter, TreeInterpreter } from './machine/tree';
import { isBranch, isLeaf } from './machine/tree/nodes';

Expand All @@ -29,8 +30,10 @@ const getChoices = (node: Branch): Answer[] =>

export const run = async ({ sourcePath }: { sourcePath: string }) => {
const dataPath = resolve(process.cwd(), sourcePath);
const source = require(dataPath);
const tree = await source.getHelpDocument();
const REQUIRE_ERROR = `Failed to require('${dataPath}');`;
const source = tryPanic(() => require(dataPath), REQUIRE_ERROR);
const GET_DOCUMENT_ERROR = `Failed to call getHelpDocument() from ${dataPath}`;
const tree = await tryPanic(() => source.getHelpDocument(), GET_DOCUMENT_ERROR);
const interpreter = createTreeInterpreter(tree);
start(interpreter);
};
Expand Down
10 changes: 10 additions & 0 deletions src/lib/try-panic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import chalk from 'chalk';

export const tryPanic = (fn: (...args: any[]) => any, message: string) => {
try {
return fn();
} catch (err) {
console.error(chalk.red(message));
process.exit(1);
}
};
7 changes: 5 additions & 2 deletions src/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { resolve } from 'path';
import { toMarkdownFile } from './lib/markdown';
import { tryPanic } from './lib/try-panic';

export const run = async ({ sourcePath }: { sourcePath: string }) => {
const dataPath = resolve(process.cwd(), sourcePath);
const source = require(dataPath);
const tree = await source.getHelpDocument();
const REQUIRE_ERROR = `Failed to require('${dataPath}');`;
const source = tryPanic(() => require(dataPath), REQUIRE_ERROR);
const GET_DOCUMENT_ERROR = `Failed to call getHelpDocument() from ${dataPath}`;
const tree = await tryPanic(() => source.getHelpDocument(), GET_DOCUMENT_ERROR);
const markdown = await toMarkdownFile(tree);
console.log(markdown);
};

0 comments on commit 0061a48

Please sign in to comment.