diff --git a/src/helpers/Exception.ts b/src/helpers/Exception.ts index f9d6c2a..b8edb49 100644 --- a/src/helpers/Exception.ts +++ b/src/helpers/Exception.ts @@ -106,7 +106,51 @@ export class Exception extends Error { this.message = this.message.concat(`\n\n${separator}`) } - const pretty = await new Youch(this, {}).toJSON() + const youch = new Youch(this, {}) + + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + youch._getFrameSource = function (frame) { + let path = frame.file + .replace(/dist\/webpack:\//g, '') // unix + .replace(/dist\\webpack:\\/g, '') // windows + + /** + * We ignore the error when "fileURLToPath" is unable to parse + * the path, since returning the frame source is an optional + * thing + */ + try { + path = path.startsWith('file:') ? fileURLToPath(path) : path + } catch {} + + return new Promise(resolve => { + if (!path) { + resolve(null) + return + } + readFile(path, 'utf-8', (error, contents) => { + if (error) { + resolve(null) + return + } + + const lines = contents.split(/\r?\n/) + const lineNumber = frame.line + + resolve({ + pre: lines.slice( + Math.max(0, lineNumber - (this.options.preLines + 1)), + lineNumber - 1 + ), + line: lines[lineNumber - 1], + post: lines.slice(lineNumber, lineNumber + this.options.postLines) + }) + }) + }) + } + + const pretty = await youch.toJSON() pretty.error.frames = pretty.error.frames.map(frame => { frame.isApp = true