forked from golevelup/nestjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rabbitmq): cleanup for error handlers
- Loading branch information
1 parent
85b1b67
commit ddd4707
Showing
8 changed files
with
132 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as amqplib from 'amqplib'; | ||
|
||
export enum MessageHandlerErrorBehavior { | ||
ACK, | ||
NACK, | ||
REQUEUE, | ||
} | ||
|
||
export type MessageErrorHandler = ( | ||
channel: amqplib.Channel, | ||
msg: amqplib.ConsumeMessage, | ||
error: any | ||
) => Promise<void> | void; | ||
|
||
/** | ||
* An error handler that will ack the message which caused an error during processing | ||
*/ | ||
export const ackErrorHandler: MessageErrorHandler = (channel, msg, error) => { | ||
channel.ack(msg); | ||
}; | ||
|
||
/** | ||
* An error handler that will nack and requeue a message which created an error during processing | ||
*/ | ||
export const requeueErrorHandler: MessageErrorHandler = ( | ||
channel, | ||
msg, | ||
error | ||
) => { | ||
channel.nack(msg, false, true); | ||
}; | ||
|
||
/** | ||
* An error handler that will nack a message which created an error during processing | ||
*/ | ||
export const defaultNackErrorHandler: MessageErrorHandler = ( | ||
channel, | ||
msg, | ||
error | ||
) => { | ||
channel.nack(msg, false, false); | ||
}; | ||
|
||
export const getHandlerForLegacyBehavior = ( | ||
behavior: MessageHandlerErrorBehavior | ||
) => { | ||
switch (behavior) { | ||
case MessageHandlerErrorBehavior.ACK: | ||
return ackErrorHandler; | ||
case MessageHandlerErrorBehavior.REQUEUE: | ||
return requeueErrorHandler; | ||
default: | ||
return defaultNackErrorHandler; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.