-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patherrorHandler.js
More file actions
26 lines (24 loc) · 814 Bytes
/
Copy patherrorHandler.js
File metadata and controls
26 lines (24 loc) · 814 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import chalk from 'chalk'
import debugClient from 'debug'
import { sanitizeErrorMessage } from './client.js'
const debug = debugClient('toggl-cli-error')
/**
* Wraps a yargs command handler with error handling that catches
* unhandled errors and displays a user-friendly message instead
* of a raw stack trace with sensitive data.
*
* @param {Function} handler - The async handler function to wrap
* @returns {Function} - The wrapped handler
*/
export function withErrorHandling (handler) {
return async function (argv) {
try {
await handler(argv)
} catch (error) {
const message = sanitizeErrorMessage(error.message) || 'An unexpected error occurred.'
console.error(chalk.red(`\n✖ ${message}`))
debug('Full error details: %O', error)
process.exit(1)
}
}
}