Skip to content

Commit 99601be

Browse files
committed
fix(core): Set prototype & name for custom errors
1 parent e329e9c commit 99601be

File tree

5 files changed

+19
-14
lines changed

5 files changed

+19
-14
lines changed

src/core/InternalError.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const genericMessage =
44
export class InternalError extends Error {
55
constructor(message: string) {
66
super(`${message}. ${genericMessage}`)
7-
// Object.setPrototypeOf(this, InternalError.prototype)
7+
Object.setPrototypeOf(this, InternalError.prototype)
88
}
99
}
10+
InternalError.prototype.name = InternalError.name

src/core/validateAndNormalizeConfig.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,12 @@ function validateDependencyTree(services: {
130130
}
131131

132132
export class ConfigValidationError extends Error {
133-
name = 'ConfigValidationError'
133+
constructor(message: string) {
134+
super(message)
135+
Object.setPrototypeOf(this, ConfigValidationError.prototype)
136+
}
134137
}
138+
ConfigValidationError.prototype.name = ConfigValidationError.name
135139

136140
function validateType(checker: Checker, reportedPath: string, value: any) {
137141
checker.setReportedPath(reportedPath)

test/integration/crashing.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ describe('crashing', () => {
6767
`
6868
proc = new CompositeProcess(script)
6969
await proc.ended
70-
expect(redactStackTraces(proc.flushOutput().slice(4)))
71-
.toMatchInlineSnapshot(`
70+
const output = redactStackTraces(proc.flushOutput())
71+
output.shift() // ignore first line like "<file path>:<line number>"
72+
expect(output).toMatchInlineSnapshot(`
7273
Array [
74+
" throw new ConfigValidationError('\`config.services\` has no entries');",
75+
" ^",
76+
"",
7377
"ConfigValidationError: \`config.services\` has no entries",
7478
"<stack trace>",
75-
" name: 'ConfigValidationError'",
76-
"}",
7779
"",
7880
"",
7981
]

test/integration/helpers/composite-process.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export class CompositeProcessCrashError extends Error {
7171
'--------------\n',
7272
].join('\n'),
7373
)
74-
// Object.setPrototypeOf(this, CompositeProcessCrashError.prototype)
74+
Object.setPrototypeOf(this, CompositeProcessCrashError.prototype)
7575
}
7676
}
77+
CompositeProcessCrashError.prototype.name = CompositeProcessCrashError.name

test/integration/helpers/redact.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* This file contains helpers to redact bits of text from console `lines`
22
so that we have something consistent that we can snapshot. */
33

4-
import path from 'path'
5-
64
export function redactStackTraces(lines: string[]) {
75
const output = [...lines]
86
let stackTrace: StackTrace | false = false
@@ -30,13 +28,12 @@ function findStackTrace(lines: string[]): StackTrace | false {
3028
return { start, length }
3129
}
3230

33-
export function redactEscapedCwdInstances(lines: string[]) {
34-
const cwd = path.join(process.cwd(), path.sep)
35-
const escapedCwd = JSON.stringify(cwd).slice(1, -1)
31+
export function redactCwd(lines: string[]) {
32+
const cwd = process.cwd()
3633
return lines.map(line => {
3734
let result = line
38-
while (result.includes(escapedCwd)) {
39-
result = result.replace(escapedCwd, '<cwd>')
35+
while (result.includes(cwd)) {
36+
result = result.replace(cwd, '<cwd>')
4037
}
4138
return result
4239
})

0 commit comments

Comments
 (0)