Name

sn_smd.SMErrorHelper

Description

No description available

Script

var SMErrorHelper = Class.create();

/**                
* Error utility section
**/
SMErrorHelper.isSMError = function(error) {
  if (gs.nil(error))
  	return false;
  if ((typeof error) != 'object' || error.getErrorType === undefined || error.getErrorLevel === undefined)
  	return false;
  return true;
};

/**
* @Deprecated - use createJavaAPIError instead
*/
SMErrorHelper.createSRAJavaAPIError = function(displayMsg, internalMsg, error) {
  return SMErrorHelper.createJavaAPIError(displayMsg, internalMsg, error);
};

/**           Following comments apply to each of the following methods in this section
* @param {String} displayMsg  : This is the message to be displayed to the user. Pass the translated message if necessary
* @param {String} internalMsg : This is a message used for debug purpose. Ex : Error in method getValue();
* @param {*} error Java exception
*/
SMErrorHelper.createJavaAPIError = function(displayMsg, internalMsg, error) {
  var newError = new SMJavaAPIError();
  SMErrorHelper._populateError(displayMsg, internalMsg, error, newError,false);
  return newError;
};


/** Use this function if you want to set context example: Environment and Environment Name to the logs
  @param {String} displayMessage: This is the message to be displayed to the user.
  @context {String} context: On which context like Environment, Environment Blueprint, Deployment.
  @contextName {String} contextName: Name of the environment, blueprint
  @error {error || sraError} this can be any error or sra error cuaght in catch block
  @doLogging : optional booloan to log immediately
*/
SMErrorHelper.createErrorWithContext = function(displayMessage, context, contextName, error, doLogging) {
  var smError = new SMScriptAPIError();
  smError.setContext(context, contextName);
  this._populateError(displayMessage, '',error, smError, doLogging);
  return smError;
};

/**
* Creates a javascript error
* @param {string} displayMsg
* @param {string=} internalMsg
* @param {string|SRAError=} error
* @param {boolean} doLogging
*/
SMErrorHelper.createScriptAPIError = function(displayMsg, internalMsg, error, doLogging) {
  var newError = new SMScriptAPIError();
  SMErrorHelper._populateError(displayMsg, internalMsg, error, newError,doLogging);
  return newError;
};

SMErrorHelper._populateError = function(displayMsg, internalMsg, oldError, newError, doLogging){
      // Always use the existing display message if any, because it will be higher in the call stack and thus more specific
  var isSMError = SMErrorHelper.isSMError(oldError);
  displayMsg = isSMError && oldError.getDisplayMessage()? oldError.getDisplayMessage() : displayMsg;
  newError.setDisplayMessage(displayMsg);
  	
  if (isSMError) {
  	newError.appendInternalMessages(oldError.getInternalMessages());
  	if (oldError.hasContext()) {
  		newError.setContext(oldError.contextIdentifier, oldError.contextName);
  	}
  }
  else if (!gs.nil(oldError)) {
  	var details;
  	var stack;
  	
  	if (typeof oldError == 'string') {
  		details = oldError;
  	}
  	// Javascript error object
  	else if (oldError.stack) {
  		details = '' + oldError.rhinoException;
  		stack = oldError.stack;
  	}
  	// Java exception
  	else {
  		details = oldError.message;
  		
  		// TODO: Talk to platform about getting java stack trace in scoped app
// 			var stackTrace = oldError.stackTrace;
//			for (var i = 0; i < stackTrace.length && i < 15; i++)
//				stack.push(stackTrace[i].toString());
  	}
  	
  	var internalMsgs = [];
  	internalMsgs.push('Details: ' + details);
  	
  	if (stack) 
  		internalMsgs = internalMsgs.concat(['Stack trace: ', stack]);
  	
  	newError.appendInternalMessages(internalMsgs);
  }
  else {
  	newError.appendInternalMessage('');
  }
  if(doLogging)
  	newError.log();
};

/**
* Returns the display error message if the passed error is a SRAError instance,
* otherwise return the passed error.
*/
SMErrorHelper.getErrorMessage = function(error) {	
  if(SMErrorHelper.isSMError(error))
  	return error.getDisplayMessage();
  return error;
};

// Returns a ScriptAPIError with a display message that the passed argument is missing
// TODO: Should deprecate this in favor of createMissingParameterError below
SMErrorHelper.createMissingArgumentError = function(methodName, argName) {
  var displayMessage = gs.getMessage('Missing argument: {0}', argName);
  return SMErrorHelper.createScriptAPIError(displayMessage, methodName);
};

SMErrorHelper.invalidArgumentError = function(methodName, argName) {
  var displayMessage = gs.getMessage('Invalid argument: {0}', argName);
  return SMErrorHelper.createScriptAPIError(displayMessage, methodName);
};

/**
* @param {String} methodName - ex. SMErrorHelper.createMissingParameterError
* @param {String|Array<String>} argNames
* @param {Boolean=} useAnd - set true if all of the argNames are missing. If false, will separate arg names with "or" instead
* @returns {SMError} - internal message is formatted like "MyClass.myFunction: Undefined arg1, arg2, and arg3"
*/
SMErrorHelper.createMissingParameterError = function(methodName, argNames, useAnd) {

  var internalMsg = methodName;

  if (argNames) {
      var argNameStr;
      var conjunction = useAnd? 'and ' : 'or ';

      if (typeof argNames == 'string')
          argNameStr = argNames;
      else {
          if (argNames.length > 1)
              argNames[argNames.length - 1] = conjunction + argNames[argNames.length - 1];

          argNameStr = argNames.join(', ');
      }

      if (argNameStr)
          internalMsg += ': Undefined ' + argNameStr;
  }

  return SMErrorHelper.createScriptAPIError(gs.getMessage("Missing parameter"), internalMsg);
};

Sys ID

bfcc693ec3612200e2ddb59af3d3ae77

Offical Documentation

Official Docs: