Skip to content

Commit 822f27c

Browse files
committed
Improve error presentation
Fixes #34
1 parent f71aaa6 commit 822f27c

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

cli.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import process from 'node:process';
33
import meow from 'meow';
44
import {deleteAsync} from 'del';
5+
import {isPresentableError} from 'presentable-error';
56

67
const logEvent = event => {
78
if (event.path !== undefined) {
@@ -45,15 +46,25 @@ if (cli.input.length === 0) {
4546
console.error('Specify at least one path');
4647
process.exitCode = 1;
4748
} else {
48-
const {verbose, dryRun, ...flags} = cli.flags;
49+
try {
50+
const {verbose, dryRun, ...flags} = cli.flags;
4951

50-
// Only use onProgress for verbose mode when not in dry-run
51-
// In dry-run mode, we print the files at the end instead
52-
const onProgress = verbose && !dryRun ? logEvent : noop;
52+
// Only use onProgress for verbose mode when not in dry-run
53+
// In dry-run mode, we print the files at the end instead
54+
const onProgress = verbose && !dryRun ? logEvent : noop;
5355

54-
const files = await deleteAsync(cli.input, {onProgress, dryRun, ...flags});
56+
const files = await deleteAsync(cli.input, {onProgress, dryRun, ...flags});
5557

56-
if (dryRun && files.length > 0) {
57-
console.log(files.join('\n'));
58+
if (dryRun && files.length > 0) {
59+
console.log(files.join('\n'));
60+
}
61+
} catch (error) {
62+
if (isPresentableError(error)) {
63+
console.error(error.message);
64+
} else {
65+
throw error;
66+
}
67+
68+
process.exitCode = 1;
5869
}
5970
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@
5151
"cross-platform"
5252
],
5353
"dependencies": {
54-
"del": "^8.0.0",
55-
"meow": "^13.2.0"
54+
"del": "^8.0.1",
55+
"meow": "^13.2.0",
56+
"presentable-error": "^0.0.1"
5657
},
5758
"devDependencies": {
5859
"ava": "^6.1.3",

test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,16 @@ test('verbose + dry-run with multiple files', async t => {
5050
t.true(lines.includes(file1));
5151
t.true(lines.includes(file2));
5252
});
53+
54+
test('handles errors gracefully', async t => {
55+
// Test with an invalid operation that should throw an error
56+
const error = await t.throwsAsync(
57+
execa('./cli.js', ['--force', '/']),
58+
{instanceOf: Error},
59+
);
60+
61+
// Should exit with code 1
62+
t.is(error.exitCode, 1);
63+
// Should not show stack trace for system errors (they're not presentable)
64+
t.true(error.stderr.includes('EISDIR'));
65+
});

0 commit comments

Comments
 (0)