Skip to content

Commit 4c9d6ff

Browse files
committed
fix: fix error message handling
1 parent 5ef458c commit 4c9d6ff

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

vike/node/plugin/shared/loggerNotProd.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ function assertLogger(thing: string | Error, logType: LogType): void {
206206
/* Risk of infinite loop
207207
assert(res)
208208
*/
209-
if (!res) throw new Error('Internal error, reach out to a maintainer')
209+
if (!res) throw new Error('Internal Vike error, reach out to a maintainer')
210210
const { assertMsg, showVikeVersion } = res
211211
logWithVikeTag(assertMsg, logType, category, showVikeVersion)
212212
}

vike/utils/assert.ts

+17-26
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function addPrefixProjctName(msg: string, showProjectVersion = false): string {
167167

168168
function getAssertErrMsg(thing: unknown): { assertMsg: string; showVikeVersion: boolean } | null {
169169
let errMsg: string
170-
let errStack: null | string = null
170+
let errStack: string | undefined
171171
if (typeof thing === 'string') {
172172
errMsg = thing
173173
} else if (isObject(thing) && typeof thing.message === 'string' && typeof thing.stack === 'string') {
@@ -177,32 +177,23 @@ function getAssertErrMsg(thing: unknown): { assertMsg: string; showVikeVersion:
177177
return null
178178
}
179179

180-
let assertMsg: string
181-
let isBug: boolean
182-
if (errMsg.startsWith(projectTag)) {
183-
assertMsg = errMsg.slice(projectTag.length)
184-
isBug = false
185-
} else if (errMsg.startsWith(projectTagWithVersion)) {
186-
assertMsg = errMsg.slice(projectTagWithVersion.length)
187-
isBug = true
188-
} else {
189-
return null
190-
}
191-
192-
// Append stack trace
193-
if (errStack && (isBug || globalObject.showStackTraceList.has(thing as any))) {
194-
assertMsg = `${assertMsg}\n${removeErrMsg(errStack)}`
180+
for (const tag of [projectTagWithVersion, projectTag]) {
181+
const showVikeVersion = tag.endsWith(projectTagWithVersion)
182+
const prefix = `Error: ${tag}`
183+
if (errStack?.startsWith(prefix)) {
184+
if (globalObject.showStackTraceList.has(thing as any)) {
185+
const assertMsg = errStack.slice(prefix.length)
186+
return { assertMsg, showVikeVersion }
187+
}
188+
} else if (errStack?.includes(tag)) {
189+
throw new Error('Internal Vike error')
190+
}
191+
if (errMsg?.startsWith(tag)) {
192+
const assertMsg = errMsg.slice(tag.length)
193+
return { assertMsg, showVikeVersion }
194+
}
195195
}
196-
197-
const showVikeVersion = isBug
198-
return { assertMsg, showVikeVersion }
199-
}
200-
201-
function removeErrMsg(stack: unknown): string {
202-
if (typeof stack !== 'string') return String(stack)
203-
const [firstLine, ...stackLines] = stack.split('\n')
204-
if (!firstLine!.startsWith('Error: ')) return stack
205-
return stackLines.join('\n')
196+
return null
206197
}
207198

208199
function overwriteAssertProductionLogger(logger: Logger): void {

0 commit comments

Comments
 (0)