Node.js 如何显示导致错误的行

Node.js 如何显示导致错误的行

在本文中,我们将学习如何使错误堆栈更详细和可读,并打印导致Node.js程序出错的准确行。

问题陈述: 我们正在尝试创建一个示例来引发错误,以便更清楚地理解这个主题:

main.js

const fs = require('fs') 
const helper = require('./helper') 
  
try { 
    // Missing 3rd parameter 
    const values = helper(1, 2)  
    console.log(values) 
} catch (e) { 
    console.warn('Error:', e) 
}

helper.js

module.exports = (a, b, c) => { 
    if (typeof a === 'undefined') 
        throw new Error("Parameter A is not set") 
    if (typeof b === 'undefined') 
        throw new Error("Parameter B is not set") 
    if (typeof c === 'undefined') 
        throw new Error("Parameter C is not set") 
          
    return { a, b, c } 
}

运行应用的步骤: 现在使用以下命令运行 main.js 文件:

node main.js

输出:

Node.js 如何显示导致错误的行

在这里,文件 helper.js 抛出了一个错误,因为在文件 main.js 中调用该函数时未传递参数“c”。

现在,让我们讨论如何打印导致错误的确切行。

方法和解决方案: 通过查看错误堆栈并找到抛出错误的确切行和行号来解决上述问题。

示例1: 这个示例将说明如何在NodeJS中显示导致错误的行:

main.js

const fs = require('fs') 
const helper = require('./helper') 
  
try { 
    // Missing 3rd parameter 
    const values = helper(1, 2) 
    console.log(values) 
} catch (e) { 
    console.warn('Error:', e.message) 
  
    // We iterate through all the files  
    // mentioned in the error stack and  
    // find the line and line number  
    // that resulted in the error 
    e.stack 
        .split('\n') 
        .slice(1) 
        .map(r => r.match(/\((?<file>.*):(?<line>\d+):(?<pos>\d+)\)/)) 
        .forEach(r => { 
            if (r && r.groups && r.groups.file.substr(0, 8) !== 'internal')  
            { 
                const { file, line, pos } = r.groups 
                const f = fs.readFileSync(file, 'utf8').split('\n') 
                console.warn('  ', file, 'at', line + ':' + pos) 
                console.warn('    ', f[line - 1].trim()) 
            } 
        }) 
} 

helper.js

module.exports = (a, b, c) => { 
    if (typeof a === 'undefined') 
        throw new Error("Parameter A is not set") 
    if (typeof b === 'undefined') 
        throw new Error("Parameter B is not set") 
    if (typeof c === 'undefined') 
        throw new Error("Parameter C is not set") 
          
    return { a, b, c } 
}

运行应用程序的步骤:

现在使用以下命令运行 main.js 文件:

node main.js

输出:

Node.js 如何显示导致错误的行

示例2:这个示例将演示如何在NodeJS中显示导致错误的行:

main.js

const fs = require('fs') 
const multiplyBy2 = require('./helper') 
  
try { 
    const values = multiplyBy2() 
    console.log(values) 
} catch (e) { 
    e.stack 
        .split('\n') 
        .slice(1) 
        .map(r => r.match(/\((?<file>.*):(?<line>\d+):(?<pos>\d+)\)/)) 
        .forEach(r => { 
            if (r && r.groups && r.groups.file.substr(0, 8) !== 'internal')  
            { 
                const { file, line, pos } = r.groups 
                const f = fs.readFileSync(file, 'utf8').split('\n') 
                console.warn('  ', file, 'at', line + ':' + pos) 
                console.warn('    ', f[line - 1].trim()) 
            } 
        }) 
}

helper.js

module.exports = (num) => { 
    if (!num) 
        throw new Error("Parameter num is not set") 
          
    return num * 2; 
}

运行应用程序的步骤: 现在使用以下命令运行 main.js 文件:

node main.js

输出:

Node.js 如何显示导致错误的行

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程