Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const isExpectedError = err => {
// IllegalOperationError happens when we are draining a broken channel; ignore
if (err instanceof amqplib.IllegalOperationError) {
return true;
}
// similarly, an error with this text is sent in some failure modes. See
// https://github.com/streadway/amqp/issues/409 for a request for a better
// way to recognize this
if (err.message.match(/no reply will be forthcoming/)) {
return true;
}
};
return this.withConnection(async conn => {
const method = confirmChannel ? 'createConfirmChannel' : 'createChannel';
const channel = await conn.amqp[method]();
// any errors on this channel will be handled as exceptions thrown within `fn`,
// so the events can be ignored
channel.on('error', () => {});
try {
return await fn(channel);
} finally {
try {
await channel.close();
} catch (err) {
if (!(err instanceof amqplib.IllegalOperationError)) {
// IllegalOperationError happens when we are closing a broken
// channel; any other error trying to close the channel suggests
// the connection is dead, so mark it failed.
conn.failed();
}
}
}
});
}