Skip to content

Commit afd3f76

Browse files
committed
Fix bug
1 parent 4ba4d0e commit afd3f76

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/main.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ test.serial('"exitCode" defaults to 1', (t) => {
7878
})
7979

8080
test.serial('Can pass "stack"', (t) => {
81-
t.false(errorExit(error, { stack: false }).consoleArg.includes('at '))
81+
const { consoleArg } = errorExit(error, { stack: false })
82+
t.false(consoleArg.includes('at '))
8283
})
8384

8485
test.serial('"stack" defaults to true', (t) => {

src/main.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import handleCliError, {
33
validateOptions,
44
type Options as HandleCliErrorOptions,
55
} from 'handle-cli-error'
6-
import type { Info, Plugin } from 'modern-errors'
6+
import type { ErrorInstance, Info, Plugin } from 'modern-errors'
77

88
/**
99
* Options of `modern-errors-cli`
@@ -50,7 +50,13 @@ const getOptions = (options: Options = {}) => {
5050
* ```
5151
*/
5252
const exit = ({ error, options }: Info<Options>['instanceMethods']) => {
53-
handleCliError(error, { ...options, custom: 'pretty' })
53+
removePretty(error)
54+
55+
try {
56+
handleCliError(error, { ...options, custom: 'pretty' })
57+
} finally {
58+
restorePretty(error)
59+
}
5460
}
5561

5662
// Uses `.pretty()` to avoid conflict with `modern-errors-beautiful`.
@@ -60,20 +66,28 @@ const pretty = ({
6066
// eslint-disable-next-line @typescript-eslint/no-unused-vars
6167
options: { exitCode, silent, timeout, log, ...beautifulErrorOptions },
6268
}: Info<Options>['instanceMethods']) => {
69+
removePretty(error)
70+
71+
try {
72+
return beautifulError(error, beautifulErrorOptions)
73+
} finally {
74+
restorePretty(error)
75+
}
76+
}
77+
78+
const removePretty = (error: ErrorInstance) => {
6379
// eslint-disable-next-line fp/no-mutating-methods
6480
Object.defineProperty(error, 'pretty', {
6581
value: undefined,
6682
enumerable: false,
6783
writable: true,
6884
configurable: true,
6985
})
86+
}
7087

71-
try {
72-
return beautifulError(error, beautifulErrorOptions)
73-
} finally {
74-
// eslint-disable-next-line fp/no-delete
75-
delete (error as Error & { pretty?: undefined }).pretty
76-
}
88+
const restorePretty = (error: ErrorInstance & { pretty?: undefined }) => {
89+
// eslint-disable-next-line fp/no-delete
90+
delete error.pretty
7791
}
7892

7993
/**

0 commit comments

Comments
 (0)