mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 22:25:12 +00:00
improve error source maps
This commit is contained in:
parent
36ee372b0b
commit
05a7595701
@ -1,6 +1,5 @@
|
||||
const Youch = require('@nuxtjs/youch')
|
||||
const { SourceMapConsumer } = require('source-map')
|
||||
const { join, resolve } = require('path')
|
||||
const { join, resolve, relative, isAbsolute } = require('path')
|
||||
const { readFile } = require('fs-extra')
|
||||
|
||||
module.exports = function errorMiddleware(err, req, res, next) {
|
||||
@ -56,81 +55,39 @@ module.exports = function errorMiddleware(err, req, res, next) {
|
||||
}
|
||||
|
||||
async function readSource(frame) {
|
||||
const serverBundle = this.resources.serverBundle
|
||||
|
||||
// Remove webpack:/// & query string from the end
|
||||
const sanitizeName = name => name ? name.replace('webpack:///', '').split('?')[0] : ''
|
||||
|
||||
// SourceMap Support for SSR Bundle
|
||||
if (serverBundle && serverBundle.maps[frame.fileName]) {
|
||||
// Initialize smc cache
|
||||
if (!serverBundle.$maps) {
|
||||
serverBundle.$maps = {}
|
||||
}
|
||||
|
||||
// Read SourceMap object
|
||||
const smc = serverBundle.$maps[frame.fileName] || new SourceMapConsumer(serverBundle.maps[frame.fileName])
|
||||
serverBundle.$maps[frame.fileName] = smc
|
||||
|
||||
// Try to find original position
|
||||
const { line, column, name, source } = smc.originalPositionFor({
|
||||
line: frame.getLineNumber() || 0,
|
||||
column: frame.getColumnNumber() || 0,
|
||||
bias: SourceMapConsumer.LEAST_UPPER_BOUND
|
||||
})
|
||||
if (line) {
|
||||
frame.lineNumber = line
|
||||
}
|
||||
/* istanbul ignore if */
|
||||
if (column) {
|
||||
frame.columnNumber = column
|
||||
}
|
||||
/* istanbul ignore if */
|
||||
if (name) {
|
||||
frame.functionName = name
|
||||
}
|
||||
if (source) {
|
||||
frame.fileName = sanitizeName(source)
|
||||
|
||||
// Source detected, try to get original source code
|
||||
const contents = smc.sourceContentFor(source)
|
||||
if (contents) {
|
||||
frame.contents = contents
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return if fileName is still unknown
|
||||
if (!frame.fileName) {
|
||||
return
|
||||
}
|
||||
|
||||
const sanitizeName = name => name ? name.replace('webpack:///', '').split('?')[0] : null
|
||||
frame.fileName = sanitizeName(frame.fileName)
|
||||
|
||||
// Try to read from SSR bundle files
|
||||
if (serverBundle && serverBundle.files[frame.fileName]) {
|
||||
frame.contents = serverBundle.files[frame.fileName]
|
||||
// Return if fileName is unknown
|
||||
if (!frame.fileName) {
|
||||
return
|
||||
}
|
||||
|
||||
// Possible paths for file
|
||||
const searchPath = [
|
||||
this.options.srcDir,
|
||||
this.options.rootDir,
|
||||
join(this.options.buildDir, 'dist'),
|
||||
this.options.srcDir,
|
||||
this.options.buildDir
|
||||
]
|
||||
|
||||
// Scan filesystem for real path
|
||||
// Scan filesystem for real source
|
||||
for (let pathDir of searchPath) {
|
||||
let fullPath = resolve(pathDir, frame.fileName)
|
||||
let source = await readFile(fullPath, 'utf-8').catch(() => null)
|
||||
if (source) {
|
||||
if (!frame.contents) {
|
||||
frame.contents = source
|
||||
}
|
||||
frame.fullPath = fullPath
|
||||
if (isAbsolute(frame.fileName)) {
|
||||
frame.fileName = relative(this.options.rootDir, fullPath)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: use server bundle
|
||||
if (!frame.contents) {
|
||||
frame.contents = this.resources.serverBundle.files[frame.fileName]
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user