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

Feat: gracefully error on missing message #1575

Merged
merged 5 commits into from
Nov 14, 2024
Merged
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
12 changes: 12 additions & 0 deletions packages/core/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,18 @@ it('fail on aborted signals', async () => {
});

describe('Error handling', () => {
it('gracefully error on missing content', async () => {
// @ts-expect-error Testing an invalid behavior.
const prompt = createPrompt(function TestPrompt() {});
const { answer } = await render(prompt, {});
await expect(answer).rejects.toMatchInlineSnapshot(
`
[Error: Prompt functions must return a string.
at ${import.meta.filename}]
`,
);
});

it('surface errors in render functions', async () => {
const Prompt = () => {
throw new Error('Error in render function');
Expand Down
29 changes: 29 additions & 0 deletions packages/core/src/lib/create-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,28 @@ type ViewFunction<Value, Config> = (
done: (value: Value) => void,
) => string | [string, string | undefined];

function getCallSites() {
const _prepareStackTrace = Error.prepareStackTrace;
try {
let result: NodeJS.CallSite[] = [];
Error.prepareStackTrace = (_, callSites) => {
const callSitesWithoutCurrent = callSites.slice(1);
result = callSitesWithoutCurrent;
return callSitesWithoutCurrent;
};

// eslint-disable-next-line @typescript-eslint/no-unused-expressions, unicorn/error-message
new Error().stack;
return result;
} finally {
Error.prepareStackTrace = _prepareStackTrace;
}
}

export function createPrompt<Value, Config>(view: ViewFunction<Value, Config>) {
const callSites = getCallSites();
const callerFilename = callSites[1]?.getFileName?.();

const prompt: Prompt<Value, Config> = (config, context = {}) => {
// Default `input` to stdin
const { input = process.stdin, signal } = context;
Expand Down Expand Up @@ -75,6 +96,14 @@ export function createPrompt<Value, Config>(view: ViewFunction<Value, Config>) {
setImmediate(() => resolve(value));
});

// Typescript won't allow this, but not all users rely on typescript.
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (nextView === undefined) {
throw new Error(
`Prompt functions must return a string.\n at ${callerFilename}`,
);
}

const [content, bottomContent] =
typeof nextView === 'string' ? [nextView] : nextView;
screen.render(content, bottomContent);
Expand Down