gpt4 book ai didi

javascript - Bluebird 无法拒绝错误的问题。解决就好了。错误使应用程序崩溃

转载 作者:行者123 更新时间:2023-12-03 08:54:02 27 4
gpt4 key购买 nike

我正在处理 app使用 node-imap ,而且我一直在处理错误或缺少错误时遇到了困难。我有以下功能:

////imap-helpers.js
function searchBox(searchTerm, bodies){
return new Promise(function(resolve, reject){
searchTerm = searchTerm || ['ALL']
bodies = bodies || ['HEADER.FIELDS (FROM TO CC BCC SUBJECT)','TEXT']
imap.search(searchTerm, function(err, results){
if (err) reject(err)
var messages = [], newMessage = {}, count=0;
var f = imap.fetch(results, {
bodies: bodies,
struct: true
});

f.on('message', function(msg) {
newMessage = {}

msg.on('body', function(stream, info) {
//When we get a message, append the header to header, text to body.
stream.on('data', function(chunk){
if (info.which !== 'TEXT')
newMessage.rawHeader += chunk.toString('utf8')
else
newMessage.body += chunk.toString('utf8')
})

//When stream is done, strip the unparsable characters at the beginning before parsing.
//NOTE: I'm not actually sure what these unparseable characters actually are
//but this all works kosher.
stream.once('end', function() {
if (info.which !== 'TEXT'){
newMessage.rawHeader = newMessage.rawHeader.replace(/^undefined/, '')
newMessage.header = Imap.parseHeader(newMessage.rawHeader)
}
if(newMessage.body)
newMessage.body = newMessage.body.replace(/^undefined/, '')
})
})

msg.once('attributes', function(attrs) {
newMessage.attrs = attrs
})

msg.once('end', function() {
messages[count] = _.omit(newMessage, 'rawHeader')
count++
})

})

f.on('error', function(e){
reject(e)
})

f.once('end', function(){
resolve(messages)
})

})
})
}

exports.getByUID = function(boxName, uid){
boxName = boxName || 'INBOX'
var searchTerm = ['ALL',['UID',uid]]
return imap.openBoxAsync(boxName, false).then(function(){
return searchBox(searchTerm, ['HEADER.FIELDS (FROM TO CC BCC SUBJECT)']).then(function(messages){
return messages
}).catch(function(e){
throw e
})
}).catch(function(e){
return e
})
}

然后我可以在文件中调用它。
//imap-controller.js - severely amended version
getOne: function(req, res){
methods.getByUID('INBOX',req.params.uid).then(function(response){
res.json(response)
}).catch(function(e){
res.json({error: e.message})
})
}

但问题是我没有运气将错误返回给 Controller 。它只是不断地打破程序
Unhandled rejection Error: Nothing to fetch
at Connection._fetch (/Users/dkranec/github/imap/node_modules/imap/lib/Connection.js:778:11)
at Connection.fetch (/Users/dkranec/github/imap/node_modules/imap/lib/Connection.js:771:15)
at Connection.tryCatcher (/Users/dkranec/github/imap/node_modules/bluebird/js/main/util.js:26:23)
at Connection.ret [as fetchAsync] (eval at <anonymous> (/Users/dkranec/github/imap/node_modules/bluebird/js/main/promisify.js:163:12), <anonymous>:15:23)
at /Users/dkranec/github/imap/app.js:34:12
at tryCatcher (/Users/dkranec/github/imap/node_modules/bluebird/js/main/util.js:26:23)
at Promise._settlePromiseFromHandler (/Users/dkranec/github/imap/node_modules/bluebird/js/main/promise.js:503:31)
at Promise._settlePromiseAt (/Users/dkranec/github/imap/node_modules/bluebird/js/main/promise.js:577:18)
at Promise._settlePromises (/Users/dkranec/github/imap/node_modules/bluebird/js/main/promise.js:693:14)
at Async._drainQueue (/Users/dkranec/github/imap/node_modules/bluebird/js/main/async.js:123:16)
at Async._drainQueues (/Users/dkranec/github/imap/node_modules/bluebird/js/main/async.js:133:10)
at Immediate.Async.drainQueues [as _onImmediate] (/Users/dkranec/github/imap/node_modules/bluebird/js/main/async.js:15:14)
at processImmediate [as _immediateCallback] (timers.js:371:17)

请在此处查看我在此主题上发表的相关 reddit 帖子,其中包含此功能的小版本,作为我制作的应用程序,如果您认为您可以帮助解决此问题,我也会遇到问题 here .基本上是同一问题的另一个版本,但会捕获错误但不会在成功时发出 imap.fetch 发射器。我赢不了!

最佳答案

searchBox()主要要做的是return;reject(err); 之后,正如@KirillSlatin 在上面的评论中所说。

这是整个功能以及一些进一步(未经测试)的建议:

function searchBox(searchTerm, bodies) {
return new Promise(function(resolve, reject) {
searchTerm = searchTerm || ['ALL'];
imap.search(searchTerm, function(err, results) {
if (err) {
reject(err);
return; // <<<<<<<<<<<<<<<<<<<
}
var messages = [],
f = imap.fetch(results, {
bodies: bodies || ['HEADER.FIELDS (FROM TO CC BCC SUBJECT)','TEXT'],
struct: true
});
f.on('message', function(msg) {
var rawHeader = '',
newMessage = { body: '' };
msg.on('body', function(stream, info) {
stream.on('data', function(chunk) { ((info.which == 'TEXT') ? newMessage.body : rawHeader) += chunk.toString('utf8') });
// stream.once('end', function() { });//no point doing anything here providing msg.once('end') fires later.
});
msg.once('attributes', function(attrs) { newMessage.attrs = attrs; });
msg.once('end', function() {
newMessage.header = rawHeader.length ? Imap.parseHeader(rawHeader) : '';
newMessage.attrs = newMessage.attrs || [];//safety
messages.push(newMessage);
});
});
f.on('error', reject);
f.once('end', function() { resolve(messages); });
});
});
}

newMessagef.on('message') 中声明处理程序,当嵌套事件发生时,无论事件调度的性质如何 - 同步或异步,都没有消息串扰的机会。

.getByUID()将传递任何错误而无需捕获/重新抛出:
exports.getByUID = function(boxName, uid) {
return imap.openBoxAsync(boxName || 'INBOX', false).then(function() {
return searchBox(['ALL', ['UID', uid]], ['HEADER.FIELDS (FROM TO CC BCC SUBJECT)']);
});
}

关于javascript - Bluebird 无法拒绝错误的问题。解决就好了。错误使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32021977/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com