errorhandler-nxg-cg
v1.1.8
Published
Library to connect to a message broker (like rabbitmq) and create a specific queue for error handling purposes.
Downloads
35
Maintainers
Readme
errorhandler-nxg-cg
[
1. Introduction
This code has the objective to send messages to a message broker (like rabbitmq) queues for the N3xGen Platform (NXGP). This messages primarily will send an error message from NXGP components to be handled.
This library is useful to add into the NXGP components developed by their owners in order to have information for errors at flows execution time.
As components are used in the NXGP flows regardless that the library should be added on component code, when the flow is running, an exchange and some queues are created using the ID flow (assigned from NXGP). In the case of the queues, two queues are created for each step in the NXGP flow.
The queue naming structure is as follows:
flow�flowID:stepName:messages
flow�flowID:stepName:rebounds
Using the same structure for the NXGP error handler queue (for each step) the next queue is created, internally, by the errorhandler-nxg-cg library.
flow�flowID:stepName:deadletter
Where "flow" is a constant word, the "flowID" is the ID assigned by the NXGP to the flow, the "stepName" is the name configured to each node in the flow and the words "message"/"rebounds"/"deadletter" are constants.
And it is configured an exchange (the same used by the other queues).
Once this configuration is created by the library, messages can be sent to this queue.
2. Library Methods
The library can be installed from npm page with the next:
npm install errorhandler-nxg-cg
, npm i errorhandler-nxg-cg
or yarn install errorhandler-nxg-cg
_2.1. errorQueueListener
- Args: No arguments are required.
- Description: This method prepares the queue (deadletter) and the relation to the exchange, creating the queues and/or exchange if do not exist. Once the code is set, at run time each time it occurs an error, a message is sent to the configured queue.
- Sample:
//Sample applied into a component function for NXGP
module.exports.process = async function processTrigger(msg, cfg, snapshot = {}) {
try {
await msgbkr.prepareErrorQueue();
//Your code here...
} catch (e) {
console.error(`ERROR: ${e}`);
this.emit('error', e);
}
};
Resultant sample:
* Note: The library requires that the NXGP has the "ELASTICIO_LISTEN_MESSAGES_ON
" environment variable. If this variable is not available, the library defines an auto-generated queue and exchange.
2.2. producerMessage
- Args:
- Message: A String that contains the message to be sent to the deadletter queue.
- QueueName: The given name of the queue that will receive the messages to be handled.
- Description: This method prepares the queue (deadletter) and the relation to the exchange, creating the queues and/or exchange if do not exist. Once the code is set, at the time the code line is executed, the message content is sent to the queue.
- Sample:
//Sample applied into a component function for NXGP
module.exports.process = async function processTrigger(msg, cfg, snapshot = {}) {
try {
await msgbkr.producerMessage(msg,'myQueueName');
//Your code here...
} catch (e) {
console.error(`ERROR: ${e}`);
this.emit('error', e);
}
};
Resultant sample:
* Note: The library requires that the NXGP has the "ELASTICIO_LISTEN_MESSAGES_ON
" environment variable. If this variable is not available, the library defines an auto-generated queue and exchange (using the QueueName argument).
2.3. producerErrorMessage
- Args:
- Payload: The content of a request.
- Error: The error message.
- Description: This method prepares the queue (deadletter) and the relation to the exchange, creating the queues and/or exchange if do not exist. Once the code is set, at the time an error is catched and the code is executed a message is sent to the queue with the payload content and with the error message.
- Sample:
//Sample applied into a component function for NXGP
module.exports.process = async function processTrigger(msg, cfg, snapshot = {}) {
try {
let {data} = msg;
//Your code here...
} catch (e) {
console.error(`ERROR: ${e}`);
this.emit('error', e);
await msgbkr.producerErrorMessage(msg, e);
}
};
Resultant sample:
* Note: The library requires that the NXGP has the "ELASTICIO_LISTEN_MESSAGES_ON
" environment variable. If this variable is not available, the library defines an auto-generated queue and exchange.
3. Testing Code
The library contains a testing code that allows the user the behavior of the original code. This sample code shows to the user how to use the methods the library has.
The user must keep in mind that this code is developed to work without the NXGP, and according to this the code creates the default queues as explained at the end of each method explanation (at section 2).
To run the testing code the next command can be used (the starting location must be the code folder of this library):
npm test