Skip to content

Commit

Permalink
Merge pull request #84 from cubyn/feature/send-error-in-context-when-…
Browse files Browse the repository at this point in the history
…dead-lettered

Send error in context when dead-lettering
  • Loading branch information
lcrpt authored Sep 22, 2017
2 parents eb96d72 + 898f5b2 commit 1e4b301
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,10 @@ function Carotte(config) {

context['origin-consumer'] = headers['x-origin-consumer'];

if (context.error) {
context.error = deserializeError(context.error);
}

if (message.fields.redelivered) {
return carotte.handleRetry(qualifier, options, meta,
headers, context, message)(new Error('Unhandled message'));
Expand Down Expand Up @@ -584,7 +588,7 @@ function Carotte(config) {

// publish the message to the dead-letter queue
// remove exchange options because we manage this queue ourselves
return carotte.saveDeadLetterIfNeeded(message)
return carotte.saveDeadLetterIfNeeded(message, err)
.then(() => {
message.properties.headers = cleanRetryHeaders(
message.properties.headers
Expand All @@ -602,11 +606,16 @@ function Carotte(config) {
* @param {object} message - amqplib message
* @return {promise}
*/
carotte.saveDeadLetterIfNeeded = function saveDeadLetterIfNeeded(message) {
carotte.saveDeadLetterIfNeeded = function saveDeadLetterIfNeeded(message, error) {
if (config.enableDeadLetter) {
const headers = message.properties.headers;
const content = JSON.parse(message.content.toString());
content.context.error = serializeError(error);

// we use content buffer so we don't have to alter object structure
// { data: , context: }
return carotte.publish(config.deadLetterQualifier,
{ headers: message.properties.headers, isContentBuffer: true },
message.content);
{ headers, isContentBuffer: true }, Buffer.from(JSON.stringify(content)));
}
return Promise.resolve();
};
Expand Down

0 comments on commit 1e4b301

Please sign in to comment.