Announcement

Collapse
No announcement yet.

Response Transformer Sample (you may find this helpful)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Response Transformer Sample (you may find this helpful)

    I would like to share some example code for use in a response transformer.

    Maybe some day Mirth will create a forum section for sharing code snippets (other than the sadly ignored Mirth Exchange).

    We recently ran into an issue where we wanted to do special handling of messages that failed with specific errors. With a bit of help from Mirth, we came up with this simple bit of code.

    The key to getting it to work is to set the inbound and outbound data types of the Response portion of the Destination Connector to raw. To set this, click on the Set Data Types button in the Summary tab of your channel and expand the desired destination.

    In this example, we're looking for messages that have a responseStatus of ERROR and specific text in the response, however, you may find other uses for it as well. I left in the debugging code (commented). Feel free to remove it.

    I hope you find it helpful. Suggestions / Corrections are always welcome.

    Enjoy!

    Code:
    // Message Response Processing
    // This code will only work in the response transformer of a destination.
    // Note: *** response transformer will need to be set to raw in data sources for this to work ****
    // Keep in mind, the status of QUEUED and the use of connectorMessage.getSendAttempts() is only functional if you have queuing enabled on you channel destination.
    // Check processor response from the destination and for those that produce an error,
    // check if it includes specific errors and for these set the response status back to QUEUED to
    // re-attempt message send or set to SENT to ignore the error.
    if (responseStatus == ERROR) {
    	//logMessageWithSuffix('Message error', 'E', channelName, 'Response XForm', false);
    	//logger.info(channelName + ': responseStatus == ERROR');
    	//var errorMsg = response.getError();
    	var errorMsg = response.getStatusMessage(); // This is the respnse object from Mirth Results. Change this to reflect the object you are expecting
    	//logger.info(channelName + ': responseStatusMessage = ' + errorMsg);
    	//logger.info("errorMsg = "+ errorMsg);
    
    	var result = errorMsg.indexOf('Text of Error you want to handle');
    	if (result != -1) { // -1 indicates the text was not found
    		var sendAttempts = connectorMessage.getSendAttempts();
    		logger.info(channelName + ': sendAttempts = ' + sendAttempts);
    		//logMessageWithSuffix('Message error. Attempts: ' + sendAttempts.toString(), 'E', channelName, 'Response XForm', false);
    		// If this attempts 100 times for this message without success, then let it stay as ERROR
    		if (sendAttempts > 100) {
    			logger.info(channelName + ': sendAttempts > 100. Erroring message');
    		} else {
    			responseStatus = QUEUED; // We want to requeue this failure and let it be resent
    			//logMessageWithSuffix('Message error. Attempts: ' + sendAttempts.toString() + '. Requeuing...', 'E', channelName, 'Response XForm', false);
    		}
    	} else if (errorMsg.indexOf('Some fatal Exception') != -1) {
    		responseStatus = SENT; // This is a fatal error but we don't want it to error. Just flag it as sent.
    		// It would be a good idea to log these events if you want to track them
    		logMessageWithSuffix('Message error was "Some fatal Exception". Reset to SENT', 'E', channelName, 'Response XForm', false);
    	} else if (errorMsg.indexOf('You get the idea, right?' != -1)) {
    		responseStatus = SENT;
    		logMessageWithSuffix('Message error was "Another fatal condition". Reset to SENT', 'E', channelName, 'Response XForm', false);
    	}// else {
    	//	logMessageWithSuffix('Message error not target error.', 'E', channelName, 'Response XForm', false);
    	//}
    }
    
    // Helper functions needed for the logMessageWithSuffix function
    // If you use this, consider moving these functions to the Code Templates.
    /**
    	logMessageWithSuffix
    	
    	Sends the supplied text to the logger. Parameters: text - str message to log; type - str [D]ebug,
    	[E]rror, [I]nfo (default); channelName - str (if empty will get it); channelNameSuffix - str;
    	includeDestName - bool; destChannelName - str
    */
    // Sends the supplied text to the logger. Parameters: text - str message to log; type - str [D]ebug, [E]rror, [I]nfo (default); channelName - str (if empty will get it); channelNameSuffix - str; includeDestName - bool; destChannelName - str
    // Formats:
    // ChannelName: channelNameSuffix: Message to log
    // ChannelName: channelNameSuffix: destChannelName: Message to log
    function logMessageWithSuffix(text, type, channelName, channelNameSuffix, includeDestName, destChannelName) {
    	var prefix = '';
    
    	if (typeof(includeDestName) != 'boolean')
    		includeDestName = false;
    
    	if (channelNameSuffix != '' && isEmpty(channelNameSuffix)) {
    		channelNameSuffix = '';
    	}
    	if (includeDestName == true && isEmpty(destChannelName)) {
    		if (connectorMessage) {
    			var destID = connectorMessage.getMetaDataId();
    			if (destID > 0)
    				destChannelName = connectorMessage.getConnectorName();
    			else
    				includeDestName = false;
    		} else
    			includeDestName = false;
    	}
    	if (isEmpty(channelName)) {
    		prefix = ChannelUtil.getDeployedChannelName(channelId) + ': ';
    	}
    	else if (endsWith(channelName, ': ')) {
    		prefix = channelName;
    	} else {
    		prefix = channelName + ': ';
    	}
    	if (includeDestName == true) {
    		prefix += destChannelName + ': ';
    	}
    	if (channelNameSuffix.length > 0) {
    		channelNameSuffix += ': ';
    	}
    	text = prefix + channelNameSuffix + text;
    	switch (type) {
    		case 'D':
    			logger.debug(text);
    			break;
    		case 'E':
    			logger.error(text);
    			break;
    		default:
    			logger.info(text);
    			break
    	}
    }
    
    /**
    	isEmpty. Parameters str (string). Returns true if str is not a string type,  is null, is undefined
    	or has a length of 0.
    */
    // isEmpty. Parameters str (string). Returns true if str is not a string type,  is null, is undefined or has a length of 0.
    function isEmpty(str) {
        return (!str || 0 === str.length);
    }
Working...
X