如何在console.log中调用一个错误 [英] How to console.log an error in node.js

查看:115
本文介绍了如何在console.log中调用一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试调试我的节点应用程序,以查找我的日志中的错误来源,仅显示为错误:发送后无法设置标题,没有跟踪信息或任何上下文。

I've been trying to debug my node app to find the source of an error in my log that shows up only as "Error: Can't set headers after they are sent", with no trace information or any context.

发生这种情况,我想我已经修复了这个...我正在使用 connect-timeout ,我正在继续处理回调传递给异步网络操作,尽管 req.timedout res.send() c>在网络操作期间被 connect-timeout 设置为true。

As it happens, I think I've now fixed this... I am using connect-timeout and I was continuing processing a callback passed to an asynchronous network operation, which callback would eventually try to do a res.send(), despite req.timedout having been set to 'true' by connect-timeout during the network operation.

但是我仍然可以不明白为什么我的日志没有显示这个错误的跟踪信息。在我的代码中返回错误的任何地方,我将其记录到控制台:

BUT I still can't understand why my log wasn't showing trace information for this error. Anywhere that an error is returned in my code, I log it to the console with:

console.log(err);

如果 err中有跟踪信息可用对象,这似乎放在 err.stack 中,不应该将上述语句转储为 > err (包括 err.stack )到控制台日志?我的理解是,我不会因为上述情况而丢失任何信息。到:

If there is trace information available in the err object, and this seems to be placed in err.stack, shouldn't the above statement dump the whole content of err (including err.stack) to the console log? My understanding is that I wouldn't be losing any information by doing the above, compared e.g. to:

console.log(err.stack);

但是像这个似乎建议另外(虽然链接的帖子现在已经更新)。

But posts like this one seem to suggest otherwise (though the linked post has now been updated).

我实际上进一步,并添加一些相关的文本来帮助找到错误:

I actually go further, and add some relevant text to help locate the error:

console.log('error in dodgyFunction:', err);

但是尽管如此,我仍然只得到错误:不能在发送之后设置标题,没有任何上下文。这是因为这个控制台错误消息是在外部库中输出的(如 express )?我认为外部图书馆应该将错误发送回主要代码,以便相应处理?

But despite this, I was still only getting "Error: Can't set headers after they are sent", without any of the context I'd put it. Would this be because this console error message is output within an external library (like express)? I thought that external libraries should send errors back to the main code to be dealt with accordingly?

编辑:这里是我将错误和超时检查放在回调函数的顶部传递给异步操作:

here's an example of where I put my error and timeout checking, at the top of the callback function passed to the async operation:

var execFile = require('child_process').execFile;
execFile('dodgycommand', options, function(error, stdout, stderr) {
    if (req.timedout) {
        console.log('timeout detected whilst running dodgycommand, so aborting...');
        return;
    }
    if (error) {
        console.log('error running dodgycommand:', error);
        res.sendStatus(400);
        return;
    }

    // ... it's safe to continue ...

}

我基本上遵循相同的模式。

I basically follow this same pattern throughout.

推荐答案

我刚刚解决了发生了什么,希望这将帮助别人避免这个初学者的错误。

I've just worked out what was going on, and I hope this will help others to avoid this beginner's error.

对于我的一些错误日志记录我正在使用以下内容,使用字符串连接构造错误消息:

For some of my error logging I was using something like the following, using string concatenation to construct the error message:

console.log('error in function abc: ' + err + ' whilst doing xyz');

而其他地方正在使用以下内容,只是将错误消息的一部分作为单独的参数传递给 console.log

whereas elsewhere I was using something like the following, just passing the pieces of the error message as separate arguments to console.log:

console.log('error in function xyz:', err, 'whilst doing abc');

我现在看到这些给出不同的结果!

I now see that these give different results!

前者必须引用 err ,以便它可以与消息的其他部分并置,并根据这个,这样做只是使用消息部分。

The former must stringify err so that it can be concatenated with the other parts of the message, and according to this, in doing so it just uses the message part.

但是,在后一种形式中, err 对象必须由 console.log 整体而言,被抛弃。

However, in the latter form the err object must be processed by console.log unadulterated, and dumped as a whole.

这就解释了为什么我有时没有看到错误的整个内容,正如我所期待的,而其他时候我是。

This explains why I was sometimes not seeing the whole content of the error, as I was expecting, and other times I was.

对于其他库中放置的控制台日志消息,要检查的其他事情是,您不会过滤掉日志中的堆栈部分日志消息查看者...原来我(为了保存日志配额...正在使用papertrail)...喔。我这样做是通过过滤掉从 ____开始的 ____在Request.self.callback

As for console log messages put there by other libraries, something else to check is that you're not filtering out the 'stack' parts of the log messages in your log viewer... turns out that I was (in order to save on log quota... am using papertrail)... d'oh. I was doing so by filtering out any lines starting with ____at (four spaces followed by 'at'), for example ____at Request.self.callback.

这篇关于如何在console.log中调用一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆