Skip to content

Commit d05a7a6

Browse files
authored
Catch all errors, and only show stacktraces on --debug (#768)
* Catch all errors, and only show stacktraces on --debug * Remove periods in error message display
1 parent f17c425 commit d05a7a6

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

bin/observable.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as clack from "@clack/prompts";
33
import {readConfig} from "../src/config.js";
44
import {CliError} from "../src/error.js";
55
import {enableNpmVersionResolution, enableRemoteModulePreload} from "../src/javascript/imports.js";
6-
import {red} from "../src/tty.js";
6+
import {faint, link, red} from "../src/tty.js";
77

88
const args = process.argv.slice(2);
99

@@ -26,6 +26,10 @@ const {values, positionals, tokens} = parseArgs({
2626
help: {
2727
type: "boolean",
2828
short: "h"
29+
},
30+
debug: {
31+
type: "boolean",
32+
short: "d"
2933
}
3034
},
3135
strict: false,
@@ -189,8 +193,36 @@ try {
189193
}
190194
}
191195
process.exit(error.exitCode);
196+
} else {
197+
if (command && CLACKIFIED_COMMANDS.includes(command)) {
198+
clack.log.error(`${red("Error:")} ${error.message}`);
199+
if (values.debug) {
200+
clack.outro("The full error follows");
201+
throw error;
202+
} else {
203+
clack.log.info("To see the full stack trace, run with the --debug flag.");
204+
// clack.outro doesn't handle multiple lines well, so do it manually
205+
console.log(
206+
`${faint("│\n│")} If you think this is a bug, please file an issue at\n${faint("└")} ${link(
207+
"https://github.com/observablehq/framework/issues\n"
208+
)}`
209+
);
210+
}
211+
} else {
212+
console.error(`\n${red("Unexpected error:")} ${error.message}`);
213+
if (values.debug) {
214+
console.error("The full error follows\n");
215+
throw error;
216+
} else {
217+
console.error("\nTip: To see the full stack trace, run with the --debug flag.\n");
218+
console.error(
219+
`If you think this is a bug, please file an issue at\n↳ ${link(
220+
"https://github.com/observablehq/framework/issues\n"
221+
)}`
222+
);
223+
}
224+
}
192225
}
193-
throw error;
194226
}
195227

196228
// A wrapper for parseArgs that adds --help functionality with automatic usage.
@@ -199,7 +231,11 @@ try {
199231
function helpArgs<T extends ParseArgsConfig>(command: string | undefined, config: T): ReturnType<typeof parseArgs<T>> {
200232
let result: ReturnType<typeof parseArgs<T>>;
201233
try {
202-
result = parseArgs<T>({...config, options: {...config.options, help: {type: "boolean", short: "h"}}, args});
234+
result = parseArgs<T>({
235+
...config,
236+
options: {...config.options, help: {type: "boolean", short: "h"}, debug: {type: "boolean"}},
237+
args
238+
});
203239
} catch (error: any) {
204240
if (!error.code?.startsWith("ERR_PARSE_ARGS_")) throw error;
205241
console.error(`observable: ${error.message}. See 'observable help${command ? ` ${command}` : ""}'.`);

0 commit comments

Comments
 (0)