Nodejs 出错堆栈 打不到 application 中行号?
Nodejs 出错堆栈 打不到 application 中行号?
wxianfeng-2:sms wangxianfeng$ node app
Express server listening on port 3000
buffer.js:236
throw new Error('First argument needs to be a number, ’ +
^
Error: First argument needs to be a number, array or string.
at new Buffer (buffer.js:236:15)
at exports.weibo_login.base64Encode (/data/nodejs/sms/node_modules/sinalogin/lib/index.js:70:12)
at exports.weibo_login.getWeiboRsa (/data/nodejs/sms/node_modules/sinalogin/lib/index.js:79:12)
at async.iterator.fn (/data/nodejs/sms/node_modules/sinalogin/node_modules/async/lib/async.js:579:34)
at async.waterfall.wrapIterator (/data/nodejs/sms/node_modules/sinalogin/node_modules/async/lib/async.js:495:34)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
上面是我出错的 log, 堆栈始终只打印 module 中的代码, 我 项目中哪行代码导致的, 为什么没有呢?
Nodejs 出错堆栈 打不到 application 中行号?
在使用 Node.js 开发过程中,我们经常会遇到错误堆栈信息中只显示模块中的代码行号而无法找到应用程序中的具体行号的情况。这通常是因为错误发生在某些库或模块内部,而不是直接在你的应用代码中。
让我们通过一个简单的例子来说明这个问题,并提供一些解决方案。
示例代码
假设你有一个简单的 Express 应用程序,其中有一个路由处理程序,它尝试对某个字符串进行 Base64 编码:
// app.js
const express = require('express');
const { weibo_login } = require('./sinalogin');
const app = express();
app.get('/login', (req, res) => {
const username = req.query.username;
const password = req.query.password;
// 假设这里有一个错误,尝试将非字符串数据传递给 base64Encode 函数
const encodedPassword = weibo_login.base64Encode({}); // 错误发生在这里
res.send(`Encoded Password: ${encodedPassword}`);
});
app.listen(3000, () => {
console.log('Express server listening on port 3000');
});
假设 weibo_login
模块中有如下的代码:
// sinalogin/lib/index.js
exports.weibo_login = {
base64Encode(data) {
return Buffer.from(data).toString('base64'); // 这里会抛出错误
},
getWeiboRsa() {
// 其他逻辑...
}
};
出现的问题
当你访问 /login
路由时,可能会看到类似这样的错误堆栈:
buffer.js:236
throw new Error('First argument needs to be a number, array or string.');
^
Error: First argument needs to be a number, array or string.
at new Buffer (buffer.js:236:15)
at exports.weibo_login.base64Encode (/path/to/sinalogin/lib/index.js:70:12)
at exports.weibo_login.getWeiboRsa (/path/to/sinalogin/lib/index.js:79:12)
at async.iterator.fn (/path/to/sinalogin/node_modules/async/lib/async.js:579:34)
at async.waterfall.wrapIterator (/path/to/sinalogin/node_modules/async/lib/async.js:495:34)
at process._tickCallback (internal/process/next_tick.js:68:7)
解决方案
-
使用 try-catch 块:在可能引发错误的地方添加 try-catch 块,以便捕获异常并输出更详细的错误信息。
app.get('/login', (req, res) => { const username = req.query.username; const password = req.query.password; try { const encodedPassword = weibo_login.base64Encode(password); res.send(`Encoded Password: ${encodedPassword}`); } catch (error) { console.error(error); res.status(500).send('An error occurred'); } });
-
使用调试工具:使用 Node.js 的调试工具(如
node debug
或 VSCode 的调试功能)来逐步执行代码,查看变量的状态。 -
增加日志记录:在关键位置增加日志记录,帮助定位问题。
通过这些方法,你可以更好地理解和解决 Node.js 应用程序中的错误堆栈问题。
不是打出来了吗? at new Buffer (buffer.js:236:15) at exports.weibo_login.base64Encode
调用方法时错了吧,参数传错了
buffer.js 也是 module 中的, 我的程序是node app.js, 没打 app.js 中的行号.
当你在 Node.js 中遇到错误时,如果错误堆栈信息没有显示你的应用代码中的具体行号,而是显示了第三方库或 Node.js 内部文件的位置,可能是因为错误发生时上下文信息丢失。为了使堆栈跟踪包含应用程序中的行号,你需要确保正确捕获并处理错误。
示例代码如下:
假设你的应用中有如下 index.js
文件:
const express = require('express');
const app = express();
app.post('/login', (req, res) => {
const data = req.body.data;
const encodedData = Buffer.from(data, 'base64').toString(); // 错误可能发生在这里
res.json({ status: 'success', message: encodedData });
});
app.listen(3000, () => {
console.log('Express server listening on port 3000');
});
确保在需要的地方添加错误处理程序。例如,你可以使用中间件来捕获错误,并且可以打印详细的堆栈信息:
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: 'Internal Server Error' });
});
通过这样做,当错误发生时,它会被这个中间件捕获,并输出完整的堆栈跟踪,其中应该包括你的应用程序中的行号。
如果你依然无法获取到应用程序中的行号,可以考虑升级 Node.js 版本,因为一些旧版本可能存在这个问题。另外,确保你的源代码文件没有被压缩或混淆,以保证堆栈跟踪能够准确地指向你的代码位置。