607A Yorktown Place, Statesboro, GA 30461
http://www.chrissterling.me — chris@chrissterling.me
770-689-6679

Return to Resume

public static GetResponse _GetResponseException(Exception ex, bool sendemail = false, int frame=1)
{
	DateTime now = DateTime.Now;
	//start the basic get response that I return to the user. I include what version of the API they are on
	//what's the server name (because we have load balanced the API)
	//start the basic exception info, I'll throw more detailed information later in the code
	//and the exact time
	GetResponse getResponse = new GetResponse
	{
		ServiceVersion = WebConfigurationManager.AppSettings["ServiceVersion"],
		ServerName = Environment.MachineName,
		ExceptionInfo = new APIException {ErrorString = ex.ToString(), ErrorMessage = ex.Message},
		ResponseTime = now.ToString("F")
	};
	//is there an inner exception, of so, lets see it as well
	if (ex.InnerException != null)
	{
		getResponse.ExceptionInfo.ErrorMessage += ". Inner Exception: " + ex.InnerException;
	}
	//I tell the user what type of exception it is
	getResponse.ExceptionInfo.ExceptionType = ex.GetType().ToString();
	//I start with an "unknown exception" this basically means that I haven't encountered it before
	getResponse.ExceptionInfo.ResponseCode = ResponseCode.UNKNOWN_EXCEPTION;
	//custom exception that I created for the API, handles stuff like Banner (our oracle database) is down, or that the ID
	//that was "claimed" to be an eagle ID, that was passed, was in valid, or something else
	if (ex is MyPortalException)
	{
		if (getResponse.ExceptionInfo.ErrorMessage.IndexOf("Banner", StringComparison.Ordinal) > 0)
		{
			getResponse.ExceptionInfo.ResponseCode = ResponseCode.BANNER_DOWN;
		}
		else if (getResponse.ExceptionInfo.ErrorMessage.IndexOf("EagleID", StringComparison.Ordinal) > 0)
		{
			getResponse.ExceptionInfo.ResponseCode = ResponseCode.INVALID_EAGLE_ID;
		}
	}
	//can't connect to the oracle database
	else if (ex is OracleException)
	{
		OracleException exc = (OracleException)ex;
		getResponse.ExceptionInfo.ErrorCode = exc.Number;
		//I return the common errors that are reported, in most cases, bad password and account locked
		switch (exc.Number)
		{
			case 1017:
				getResponse.ExceptionInfo.ResponseCode = ResponseCode.ORACLE_BAD_PASSWORD;
				break;
			case 28000:
				getResponse.ExceptionInfo.ResponseCode = ResponseCode.ORACLE_ACCOUNT_LOCKED;
				break;
			default:
				getResponse.ExceptionInfo.ResponseCode = ResponseCode.ORACLE_EXCEPTION;
				break;
		}
	}
	//I have a mysql error
	else if (ex is MySqlException)
	{
		MySqlException exc = (MySqlException)ex;
		getResponse.ExceptionInfo.ErrorCode = exc.ErrorCode;
		getResponse.ExceptionInfo.ResponseCode = ResponseCode.MYSQL_EXCEPTION;
	}
	//I had an issue taking the json string and converting to an object. I interact with some API's that return json
	else if (ex is JsonException)
	{
		getResponse.ExceptionInfo.ResponseCode = ResponseCode.JSON_DESERIALIZATION_ERROR;
	}
	//I have an issue connecting to LDAP (Active Directory in our case)
	else if (ex is LdapException)
	{
		LdapException exc = (LdapException)ex;
		getResponse.ExceptionInfo.ErrorCode = exc.ErrorCode;
		//was it bad password or something else
		getResponse.ExceptionInfo.ResponseCode = exc.ErrorCode == 49 ? ResponseCode.LDAP_PASSWORD : ResponseCode.LDAP_EXCEPTION;
	}
	//I had an issue making a web call to a 3rd party API
	else if (ex is WebException)
	{
		WebException exc = (WebException)ex;
		getResponse.ExceptionInfo.ErrorCode = (int) exc.Status;
		getResponse.ExceptionInfo.ResponseCode = ResponseCode.WEB_EXCEPTION;
	}
	//I did this to help some legacy code
	getResponse.ResponseCode = getResponse.ExceptionInfo.ResponseCode;
	//do I need to send an email?
	if (sendemail)
	{
		string message = "Exception Occured" + Environment.NewLine;
		message += "Service Version: " + getResponse.ServiceVersion + Environment.NewLine;
		message += "Error Message: " + getResponse.ExceptionInfo.ErrorMessage +" ("+ex+") "+ Environment.NewLine;
		message += "Response Code: " + getResponse.ExceptionInfo.ResponseCode + Environment.NewLine;
		message += "Exception Type: " + getResponse.ExceptionInfo.ExceptionType + Environment.NewLine;
		message += "Error Code: " + getResponse.ExceptionInfo.ErrorCode+ Environment.NewLine;
		//get exactly where in the code the exception happened as well as some other data
		StackTrace stackTrace = new StackTrace();
		// get calling method name
		string methodName = stackTrace.GetFrame(frame).GetMethod().Name;
		HttpContext request = HttpContext.Current;
		if (request == null) throw new ArgumentNullException("request was somehow null");
		string requestTime = request.Timestamp.ToString();
		string callUri = request.Request.Path;
		string callMethod = request.Request.RequestType;
		string userIp = request.Request.UserHostAddress;
		message += Environment.NewLine + "DEBUG INFO: " + Environment.NewLine;
		message += "Method Name: " + methodName + Environment.NewLine;
		message += "Request Time: " + requestTime + Environment.NewLine;
		message += "Call URI: " + callUri  + Environment.NewLine;
		message += "Call Method: " + callMethod + Environment.NewLine;
		message += "User IP: " + userIp + Environment.NewLine;
		//send an email
		EASEmail.SendEmail(WebConfigurationManager.AppSettings["ConactEmail"], "mygsu-support@georgiasouthern.edu", "API.GSU Exception", message, true, "Freedom11 - WebService");
	}
	//are we currently logging calls? We had an issue with a database that was preventing the API from returning quickly
	//I did this so I could quickly disable logging. I also lowered the connection timeout to help connection time outs
	if (LogCalls)
	{
		Log.LogError(getResponse.ExceptionInfo.ErrorMessage);
	}
	return getResponse;
}
Return to Resume