-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.d.ts
More file actions
98 lines (91 loc) · 2.42 KB
/
main.d.ts
File metadata and controls
98 lines (91 loc) · 2.42 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import type { Options as BeautifulErrorOptions } from 'beautiful-error'
/**
* Validate `handle-cli-error` options
*/
export function validateOptions(options: unknown): asserts options is Options
/**
* `handle-cli-error` options
*/
export type Options = BeautifulErrorOptions & {
/**
* Process [exit code](https://en.wikipedia.org/wiki/Exit_status).
*
* We recommend values between 1 and 124 because the following exit codes have
* some special meaning:
*
* - 0: success
* - 125: invalid [`options`](#options)
* - 126 to 255: used by shells like Bash
*
* @default 1
*/
readonly exitCode?: number
/**
* Exits the process without logging anything on the console.
*
* @default false
*/
readonly silent?: boolean
/**
* The process exits gracefully: it waits for any ongoing tasks (callbacks,
* promises, etc.) to complete, up to a specific `timeout`.
*
* Special values:
* - `0`: Exits right away, without waiting for ongoing tasks
* - `Number.POSITIVE_INFINITY`: Waits for ongoing tasks forever, without
* timing out
*
* @default 5000
*/
readonly timeout?: number
/**
* Function used to print the error message.
*
* @default console.error
*/
readonly log?: (message: string) => void
/**
* Specify different options per error class. The object:
* - Keys are either the
* [`error.name`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/name),
* or `"default"` (used if no `error.name` matches)
* - Values are options objects
*
* @default {}
*
* @example
* ```js
* handleCliError(error, {
* InputError: { exitCode: 1, stack: false },
* DatabaseError: { exitCode: 2, stack: false },
* default: { exitCode: 3 },
* })
* ```
*/
readonly classes?: {
readonly [errorName: string]: Omit<Options, 'classes'>
}
}
/**
* Logs `error` on the console (`stderr`) then exits the process.
*
* This never throws. Invalid `error`s are silently
* [normalized](https://github.com/ehmicky/normalize-exception).
*
* @example
* ```js
* #!/usr/bin/env node
* import handleCliError from 'handle-cli-error'
*
* const cliMain = () => {
* try {
* // ...
* } catch (error) {
* handleCliError(error) // Logs `error` then exit the process
* }
* }
*
* cliMain()
* ```
*/
export default function handleCliError(error: unknown, options?: Options): void