Exception 在谷歌云函数中写入控制台内部catch时,不会写入任何内容
我在谷歌云功能中有一系列的工作要做。如果我在本地运行脚本,它将生成到Exception 在谷歌云函数中写入控制台内部catch时,不会写入任何内容,exception,google-cloud-functions,Exception,Google Cloud Functions,我在谷歌云功能中有一系列的工作要做。如果我在本地运行脚本,它将生成到控制台的输出,但不在云函数中 // this is logged console.error('An error'); for (var i=0; i<chunks.length; i++) { const work = chunks[i].map(c => createOrUpdateTable(c, timestamp)); await Promise.all(work) // this is n
控制台的输出
,但不在云函数中
// this is logged
console.error('An error');
for (var i=0; i<chunks.length; i++) {
const work = chunks[i].map(c => createOrUpdateTable(c, timestamp));
await Promise.all(work)
// this is not logged
.catch(e => console.error(e.message));
}
//这是记录的
console.error(“一个错误”);
对于(var i=0;i createOrUpdateTable(c,timestamp));
等待承诺。全部(工作)
//这是没有记录的
.catch(e=>console.error(e.message));
}
我试着把
catch
放在函数中,还有很多其他的东西,但都是相同的行为。如何让错误出现在日志中?根据官方文档,您可以从云函数向
Stackdriver错误报告:
// These WILL be reported to Stackdriver Error Reporting
console.error(new Error('I failed you'));
console.error('I failed you', new Error('I failed you too'));
throw new Error('I failed you'); // Will cause a cold start if not caught
// These will NOT be reported to Stackdriver Error Reporting
console.info(new Error('I failed you')); // Logging an Error object at the info level
console.error('I failed you'); // Logging something other than an Error object
throw 1; // Throwing something other than an Error object
callback('I failed you');
res.status(500).send('I failed you');
根据官方文档,您可以从云函数向 Stackdriver错误报告:
// These WILL be reported to Stackdriver Error Reporting
console.error(new Error('I failed you'));
console.error('I failed you', new Error('I failed you too'));
throw new Error('I failed you'); // Will cause a cold start if not caught
// These will NOT be reported to Stackdriver Error Reporting
console.info(new Error('I failed you')); // Logging an Error object at the info level
console.error('I failed you'); // Logging something other than an Error object
throw 1; // Throwing something other than an Error object
callback('I failed you');
res.status(500).send('I failed you');
您应该使用
try/catch
块,如下所示:
for (var i=0; i<chunks.length; i++) {
const work = chunks[i].map(c => createOrUpdateTable(c, timestamp));
try {
await Promise.all(work)
} catch (e) {
console.error(e.message));
return { error: err }
}
}
for(var i=0;i createOrUpdateTable(c,timestamp));
试一试{
等待承诺。全部(工作)
}捕获(e){
控制台错误(e.message));
返回{error:err}
}
}
您应该使用try/catch
块,如下所示:
for (var i=0; i<chunks.length; i++) {
const work = chunks[i].map(c => createOrUpdateTable(c, timestamp));
try {
await Promise.all(work)
} catch (e) {
console.error(e.message));
return { error: err }
}
}
for(var i=0;i createOrUpdateTable(c,timestamp));
试一试{
等待承诺。全部(工作)
}捕获(e){
控制台错误(e.message));
返回{error:err}
}
}
我再现了该问题,并记录了错误,但不同
第一个功能:
exports.helloError = (data, context, callback) => {
// [START functions_helloworld_error]
// These WILL be reported to Stackdriver Error Reporting
console.error(new Error('I failed you'));
console.error('I failed you', new Error('I failed you too'));
throw new Error('I failed you'); // Will cause a cold start if not caught
// [END functions_helloworld_error]
};
exports.helloError = (data, context, callback) => {
try{
throw new Error('I failed you'); // Will cause a cold start if not caught
}catch(e){
console.log(e.message);
};
};
在stackdriver日志记录中,它显示为错误强>
severity: "ERROR"
textPayload: "Error: I failed you
at exports.helloError (/srv/index.js:4:17)
at /worker/worker.js:783:7
at /worker/worker.js:766:11
第二个功能是:
exports.helloError = (data, context, callback) => {
// [START functions_helloworld_error]
// These WILL be reported to Stackdriver Error Reporting
console.error(new Error('I failed you'));
console.error('I failed you', new Error('I failed you too'));
throw new Error('I failed you'); // Will cause a cold start if not caught
// [END functions_helloworld_error]
};
exports.helloError = (data, context, callback) => {
try{
throw new Error('I failed you'); // Will cause a cold start if not caught
}catch(e){
console.log(e.message);
};
};
它被报告为信息λ
severity: "INFO"
textPayload: "I failed you"
我怀疑,由于在第二个错误中处理了该错误,因此函数按预期工作,因此不会将其报告为错误,而是报告性能信息。我已重现该问题,并记录了错误,但记录方式不同 第一个功能:
exports.helloError = (data, context, callback) => {
// [START functions_helloworld_error]
// These WILL be reported to Stackdriver Error Reporting
console.error(new Error('I failed you'));
console.error('I failed you', new Error('I failed you too'));
throw new Error('I failed you'); // Will cause a cold start if not caught
// [END functions_helloworld_error]
};
exports.helloError = (data, context, callback) => {
try{
throw new Error('I failed you'); // Will cause a cold start if not caught
}catch(e){
console.log(e.message);
};
};
在stackdriver日志记录中,它显示为错误强>
severity: "ERROR"
textPayload: "Error: I failed you
at exports.helloError (/srv/index.js:4:17)
at /worker/worker.js:783:7
at /worker/worker.js:766:11
第二个功能是:
exports.helloError = (data, context, callback) => {
// [START functions_helloworld_error]
// These WILL be reported to Stackdriver Error Reporting
console.error(new Error('I failed you'));
console.error('I failed you', new Error('I failed you too'));
throw new Error('I failed you'); // Will cause a cold start if not caught
// [END functions_helloworld_error]
};
exports.helloError = (data, context, callback) => {
try{
throw new Error('I failed you'); // Will cause a cold start if not caught
}catch(e){
console.log(e.message);
};
};
它被报告为信息λ
severity: "INFO"
textPayload: "I failed you"
我怀疑,由于在第二个错误中处理了错误,因此函数按预期工作,因此不会将其报告为错误,而是报告性能信息。是的,尝试过。未使用
控制台写入日志。错误
或控制台。日志
在本地运行良好,所有日志都写入catch Local中。您可以共享云功能的全部代码吗?是的,尝试过。不使用控制台写入日志。错误
或控制台。日志
在本地运行良好,所有日志都在catch local中写入。您可以共享您的云功能的全部代码吗?可能,但问题是为什么它们没有在catch
中发布到stackdriver,但问题是为什么它们没有张贴到catch