amqplib, checkQueue()崩溃我的应用程序时,错误命中
amqplib, checkQueue() crashing my app when error hit
我正在用express写一个服务投资者,接受请求并将它们传递给rabbit MQ。
我似乎无法优雅地绕过checkQueue()错误。每次我点击它们,我的整个应用都会因为这个错误而崩溃。
events.js:141
throw er; // Unhandled 'error' event
^
Error: Channel closed by server: 404 (NOT-FOUND) with message "NOT_FOUND - no queue 'asdfasdfb' in vhost '/'"
at Channel.C.accept (/app/node_modules/amqplib/lib/channel.js:406:17)
at Connection.mainAccept [as accept] (/app/node_modules/amqplib/lib/connection.js:63:33)
at Socket.go (/app/node_modules/amqplib/lib/connection.js:476:48)
at emitNone (events.js:67:13)
at Socket.emit (events.js:166:7)
at emitReadable_ (_stream_readable.js:411:10)
at emitReadable (_stream_readable.js:405:7)
at readableAddChunk (_stream_readable.js:157:11)
at Socket.Readable.push (_stream_readable.js:110:10)
at TCP.onread (net.js:523:20)
这是让我悲伤的AMQP代码
.post(function(req, res) {
var promise = new Promise(function(resolve, reject){
var queue = req.body.queue;
if ( typeof(queue) == 'undefined' || queue.length == 0 ) return reject("No Queue Specified");
// need to add VALID JSON CHECK
var message = req.body.message;
if ( typeof(message) == 'undefined' || message.length == 0 ) reject("No Message Specified");
// select the Awknoledgement of the queue.
var noAck = req.body.ack || true;
// select the durabiltiy of the queue. Custing issues in the below if different hten when created.
var durableOpt = req.body.durable || false;
var assertOpt = {durable: durableOpt};
var sendOptions = {
noAck: noAck,
contentType: 'application/json' };
amqp.connect( process.env.QUEUE_HOST , function(err, conn) {
if (err) return reject(err);
conn.createChannel(function(err, ch) {
if (err) return reject(err);
var check = new Promise(function(resolve, reject){
ch.checkQueue(queue, function(err,ok){
if (err != null){
return reject(err);
} else {
return resolve(true);
}
})
})
var assert = new Promise(function(resolve, reject){
ch.assertQueue( queue , assertOpt, function(err,ok){
if (err != null){
return reject(err);
} else {
return resolve(true);
}
})
})
Promise.all( [ check , assert ] ).then(function( values ){
for (var i = 0; i < values.length; i++) {
if (values[i] !== true){
reject(values[i])
}
}
if( ch.sendToQueue(queue, new Buffer( JSON.stringify( message ) ) , sendOptions) == true){
return resolve('message added');
}
}) // end Promise.all
}) // end conn.createChannel
}) // end amqp.connect
}) // end of promise.
promise.then(
function(response){
res.status(200).json(response);
},
function( error ){
res.status(400).json(error);
})
})
如果我曾经在checkQueue中遇到错误,或者我实际上在AssertQueue中有错误。它总是崩溃并出现上述错误。通道总是关闭,并得到一个events.js:141错误。有什么方法可以避免呼叫错误导致我的频道中断吗?或者当错误发生时,我需要在运行中重新连接它吗?
我找到了导致问题的原因…conn抛出了一个错误,我没有"正确地"捕获,我尝试了一个标准的尝试/捕获,但它也不喜欢。
在文档中挖掘了一段时间后,我在"事件"下找到了它,我想这是有意义的,但是如果标记了错误处理就好了。
conn.on('error', function(handle){
reject(handle);
})
我不确定,但似乎你把return reject(err);
称为承诺之外。
试试这个代码
amqp.connect( process.env.QUEUE_HOST , function(err, conn) {
if (err) return console.log(err);
conn.createChannel(function(err, ch) {
if (err) return console.log(err);
var check = new Promise(function(resolve, reject){
<<< SPECIFICALLY THIS >>>
ch.checkQueue(queue, function(err,ok){
if (err != null){
return reject(err);
} else{
return resolve(true);
}
})
})
var assert = new Promise(function(resolve, reject){
<<< AND THIS >>>
ch.assertQueue( queue , assertOpt, function(err,ok){
if (err != null){
return reject(err);
} else {
return resolve(true);
}
});
})
Promise.all( [check,assert] ).then(function(values){
console.log(values);
if( ch.sendToQueue(queue, new Buffer( JSON.stringify( message ) ) , sendOptions) == true){
return resolve('message added');
}
})
});
})
相关文章:
- 错误:$injector:modulerr模块错误(我的第一个SPA应用程序)
- 动态代码中存在系统应用程序地址错误
- Rails第一个应用程序错误——Errno::ENOENT
- 为什么Highchart在我的rails应用程序中加载了两次?(未捕获的Highcharts错误#16)
- 尝试在expressJS应用程序中使用haml咖啡时出现奇怪错误
- 应用程序在“关闭”之后停止工作(控制台中没有错误);咕哝的构造”;
- 流星应用程序中的按钮位置错误
- Windows 8应用程序正在运行,现在得到0x800a1391-JavaScript运行时错误:'WinJS&
- JSHint 错误:未定义我的应用程序 (W117)
- Rails 应用程序(狂欢) - 突然的“未捕获的类型错误”
- 角度应用程序中 src 项的 404 错误
- Javascript Sencha cmd 应用程序构建错误发生在执行此行时
- 应用程序.js未在开发中加载(未捕获的引用错误:未定义 $)
- 当前 Play 应用程序的 Heroku 部署错误
- Javascript 运行时错误:“应用程序未定义”
- 类型错误: 应用程序电缆未定义
- 未捕获的引用错误:应用程序未在 Angularjs 中定义
- 引发错误应用程序
- Phonegap - JNI错误(应用程序错误):本地引用表溢出(max=512)
- Facebook网页错误:应用程序可能无效