-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.d.ts
More file actions
130 lines (120 loc) · 3.34 KB
/
main.d.ts
File metadata and controls
130 lines (120 loc) · 3.34 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import type { Styles } from 'chalk-string'
import type figures from 'figures'
/**
* Validate `beautiful-error` options
*/
export function validateOptions(options: unknown): asserts options is Options
/**
* `beautiful-error` options
*/
export interface Options {
/**
* Whether to show the `error` stack trace.
*
* @default true
*/
readonly stack?: boolean
/**
* Whether to show nested errors, i.e.
* [`error.cause`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause)
* and
* [`error.errors`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError/errors).
*
* @default true
*/
readonly cause?: boolean
/**
* Whether to show the error's additional properties.
*
* @default true
*/
readonly props?: boolean
/**
* Whether to colorize the error's message, stack trace and additional properties.
*
* Quoted strings in the error's message are printed in bold (for `"..."` and
* `'...'`) and in italic (for `` `...` ``).
*
* @default `true` in terminals, `false` otherwise
*/
readonly colors?: boolean
/**
* Icon prepended to the error's name. The available values are listed
* [here](https://github.com/sindresorhus/figures/blob/main/readme.md#figures-1).
* Can be disabled by passing an empty string.
*
* @default 'cross'
*/
readonly icon?: keyof typeof figures | ''
/**
* Color/style of the error's icon and name. The available values are listed
* [here](https://github.com/ehmicky/chalk-string#available-styles).
* Several styles can be specified by using spaces.
* Can be disabled by passing an empty string.
*
* @default 'red bold'
*/
readonly header?: Styles | ''
/**
* Name of a method to map the output. That method must take the output as a
* string argument, transform it then return it.
*
* @example
* ```js
* class ExampleError extends Error {
* beautiful(output) {
* return output.replaceAll('secret', '***')
* }
* }
*
* const error = new ExampleError('Unknown value: secret')
* const message = beautifulError(error) // 'Unknown value: ***'
* ```
*
* @default 'beautiful'
*/
readonly custom?: string | symbol
/**
* 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
* const message = beautifulError(error, {
* InputError: { icon: 'warning', stack: false },
* DatabaseError: { icon: 'info', stack: false },
* default: { icon: 'cross' },
* })
* ```
*/
readonly classes?: {
readonly [errorName: string]: Omit<Options, 'classes'>
}
}
/**
* Returns `error` as a prettified string.
*
* This never throws. Invalid `error`s are silently
* [normalized](https://github.com/ehmicky/normalize-exception).
*
* @example
* ```js
* import beautifulError from 'beautiful-error'
*
* try {
* // ...
* } catch (error) {
* const message = beautifulError(error)
* console.error(message)
* }
* ```
*/
export default function beautifulError(
error: unknown,
options?: Options,
): string