ErrHelper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. using Microsoft.Win32;
  2. using System.Diagnostics;
  3. using System.Text;
  4. namespace com.cloudsoft.utils
  5. {
  6. /// <summary>
  7. /// Base class for Business and data access options
  8. /// </summary>
  9. /// <remarks>
  10. /// <example> Example code
  11. /// <code>
  12. /// public class Security_nTX : com.cloudsoft.utils.ErrHelper
  13. /// {
  14. /// public Security_nTX()
  15. /// {
  16. /// zgSystem = "GSK_SECURITY";
  17. /// }
  18. /// public String AuthoriseUserAction(String zPayload)
  19. /// {
  20. /// try
  21. /// {
  22. /// Security_DA oDA = new Security_DA();
  23. /// return oDA.AuthoriseUserAction(zPayload);
  24. /// }
  25. /// catch (Exception ex)
  26. /// {
  27. /// return handleError(ex);
  28. /// }
  29. /// }
  30. /// </code>
  31. /// </example>
  32. /// </remarks>
  33. public class ErrHelper
  34. {
  35. /// <summary>
  36. /// Returns HTML for a WI Error
  37. /// </summary>
  38. /// <param name="oError">WI compliant error</param>
  39. /// <returns>HTML for a WI Error</returns>
  40. public string getParamsHTML(XMLHelper oError)
  41. {
  42. StringBuilder sbParams = new StringBuilder("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
  43. sbParams.Append("<tr><th colspan=\"2\" align=\"left\">Form values</th></tr>");
  44. if (oError.nodeCount("values/formvalues/value") <= 0)
  45. {
  46. sbParams.Append("<tr><td colspan=\"2\" align=\"left\">None</td></tr>");
  47. }
  48. else
  49. {
  50. foreach (XMLHelper value in oError.getNodes("values/formvalues/value"))
  51. {
  52. if (value.getString("@id") != "__VIEWSTATE")
  53. sbParams.Append("<tr><td>" + value.getString("@id") + "</td><td>" + value.getValue() + "</td></tr>");
  54. }
  55. }
  56. sbParams.Append("<tr><th colspan=\"2\" align=\"left\"><hr/></th></tr>");
  57. sbParams.Append("<tr><th colspan=\"2\" align=\"left\">Querystring values</th></tr>");
  58. if (oError.nodeCount("values/querystringvalues/value") <= 0)
  59. {
  60. sbParams.Append("<tr><td colspan=\"2\" align=\"left\">None</td></tr>");
  61. }
  62. else
  63. {
  64. foreach (XMLHelper value in oError.getNodes("values/querystringvalues/value"))
  65. {
  66. if (value.getString("@id") != "__VIEWSTATE")
  67. sbParams.Append("<tr><td>" + value.getString("@id") + "</td><td>" + value.getValue() + "</td></tr>");
  68. }
  69. }
  70. sbParams.Append("</table>");
  71. return sbParams.ToString();
  72. }
  73. /// <summary>
  74. /// Logs an error using WI mechanisms, emailing if configured to do so
  75. /// </summary>
  76. /// <param name="oError">WI compliant error</param>
  77. public void logError(XMLHelper oError)
  78. {
  79. short category = 4;
  80. int id = 1;
  81. // Log the entry to the event log...
  82. throw new Exception(oError.getString("message"));
  83. // EventLog.WriteEntry(zgSystem, oError.getString("message") + " \rPath: " + oError.getString("path") + " \rUser: " + oError.getString("user"), EventLogEntryType.Error, id, category, ba);
  84. }
  85. /// <summary>
  86. /// Enum for Error Types
  87. /// </summary>
  88. /// <remarks>
  89. /// <list type="bullet">
  90. /// <item><term>System</term><description> - System Error</description></item>
  91. /// <item><term>Business</term><description> - Business Error</description></item>
  92. /// <item><term>Information</term><description> - Information (not an error)</description></item>
  93. /// </list>
  94. /// </remarks>
  95. public enum ErrorType {
  96. /// <summary>System</summary>
  97. System,
  98. /// <summary>Business</summary>
  99. Business,
  100. /// <summary>Information</summary>
  101. Information
  102. };
  103. /// <summary>
  104. /// The System. Used to log events
  105. /// </summary>
  106. private String _zgSystem;
  107. public String zgSystem
  108. {
  109. get
  110. {
  111. return _zgSystem;
  112. }
  113. }
  114. /// <summary>
  115. /// Initializes a new instance of the <see cref="ErrHelper"/> class.
  116. /// </summary>
  117. public ErrHelper()
  118. : this("GSK")
  119. { }
  120. /// <summary>
  121. /// Initializes a new instance of the <see cref="ErrHelper"/> class with a system.
  122. /// </summary>
  123. public ErrHelper(String zSystem)
  124. {
  125. _zgSystem = zSystem;
  126. }
  127. /// <summary>
  128. /// Handles the error.
  129. /// </summary>
  130. /// <param name="ex">The Exception</param>
  131. /// <returns>Nothing , but rethrows the error with formatted xml as the error message</returns>
  132. public String handleError(Exception ex)
  133. {
  134. ErrorType et = ErrorType.System;
  135. if (ex.Source == "Oledb")
  136. et = ErrorType.Business;
  137. return handleError(ex, et);
  138. }
  139. /// <summary>
  140. /// Handles the error.
  141. /// </summary>
  142. /// <param name="ex">The Exception</param>
  143. /// <param name="et">The error type. <see cref="ErrorType"/></param>
  144. /// <returns>Nothing , but rethrows the error with formatted xml as the error message</returns>
  145. public String handleError(Exception ex, ErrorType et)
  146. {
  147. //WADS: Detailed exception info no longer given to the user...
  148. throw new Exception(et + " error : " + ex.Message);
  149. //XMLHelper oError = new XMLHelper();
  150. //if (oError.load(ex.Message) == false)
  151. //{
  152. // String zType = "SYSTEMERROR";
  153. // switch (et)
  154. // {
  155. // case ErrorType.Business:
  156. // zType = "BUSINESSEXCEPTION";
  157. // break;
  158. // case ErrorType.Information:
  159. // zType = "INFORMATION";
  160. // break;
  161. // }
  162. // StackTrace oST = new StackTrace();
  163. // int iFrame = 1;
  164. // String zMethod = oST.GetFrame(iFrame).GetMethod().Name;
  165. // while (zMethod == "throwError" || zMethod == "handleError" || zMethod == "validatePayload")
  166. // zMethod = oST.GetFrame(iFrame++).GetMethod().Name;
  167. // oError.createRoot("gskutils");
  168. // oError = oError.appendNode("error");
  169. // oError.appendAttribute("time", DateTime.Now.ToString("dd-MMM-yyyy hh:mm:ss"));
  170. // oError.appendAttribute("type", zType);
  171. // oError.appendAttribute("class", this.GetType().ToString());
  172. // oError.appendAttribute("method", zMethod);
  173. // oError.appendNode("source", (ex.Source == null ? this.GetType().ToString() : ex.Source));
  174. // oError.appendNode("description", ex.Message);
  175. // oError.appendNode("stack_trace", ex.StackTrace);
  176. // if (zType == "SYSTEMERROR")
  177. // writeEvent(zgSystem, oError.getRootXML(), et);
  178. //}
  179. ////WADS: Error detail...
  180. //logError(oError);
  181. //throw new Exception("WI Handled error has occured. Please report to the helpdesk.");
  182. }
  183. /// <summary>
  184. /// Throws a Business error.
  185. /// </summary>
  186. /// <param name="zMessage">The Business Error message.</param>
  187. public void throwError(String zMessage)
  188. {
  189. throwError(zMessage, ErrorType.Business);
  190. }
  191. /// <summary>
  192. /// Throws an error.
  193. /// </summary>
  194. /// <param name="zMessage">The error message.</param>
  195. /// <param name="et">The error type. <see cref="ErrorType"/></param>
  196. public void throwError(String zMessage, ErrorType et)
  197. {
  198. handleError(new Exception(zMessage), et);
  199. }
  200. /// <summary>
  201. /// Writes a debug message using the win32 api
  202. /// </summary>
  203. /// <param name="zMessage">The message.</param>
  204. public void debugMsg(Object zMessage)
  205. {
  206. String zMethod = new StackTrace().GetFrame(1).GetMethod().Name;
  207. Debug.Write(this.GetType().Name + "." + zMethod + " : " + zMessage);
  208. }
  209. public XMLHelper validatePayload(String zPayload)
  210. {
  211. XMLHelper domToLoad = new XMLHelper();
  212. if (domToLoad.load(zPayload) == false)
  213. throwError("Invalid Payload. XML is invalid");
  214. // Get the calling Method
  215. String zMethod = new StackTrace().GetFrame(1).GetMethod().Name;
  216. return validatePayload(domToLoad, zMethod);
  217. }
  218. public XMLHelper validatePayload(XMLHelper oPayload)
  219. {
  220. // Get the calling Method
  221. String zMethod = new StackTrace().GetFrame(1).GetMethod().Name;
  222. return validatePayload(oPayload, zMethod);
  223. }
  224. /// <summary>
  225. /// Validates the payload.
  226. /// </summary>
  227. /// <param name="zPayload">The payload.</param>
  228. /// <returns>Returns an XML Helper DOM of the Payload</returns>
  229. private XMLHelper validatePayload(XMLHelper oPayload, String zMethod)
  230. {
  231. XMLHelper domToLoad = oPayload;
  232. String zRequestType = domToLoad.getString("/request/@type");
  233. // Check to see that payload is right for method
  234. if (zRequestType == "")
  235. throwError("The request does not have a type attribute");
  236. // Check to see that payload is right for method
  237. if (zMethod.ToUpper() != zRequestType.ToUpper() && (zRequestType.ToUpper() != "SYSTEM"))
  238. throwError("The payload sent to the method " + zMethod + " is invalid. Not equal to " + zRequestType);
  239. // Build security payload xml
  240. String zUsername = domToLoad.getString("mud_id");
  241. if (domToLoad.getString("@sec") != "off")
  242. {
  243. XMLHelper oRequest = new XMLHelper().createStandardRequest("ValidatePayload", zUsername, domToLoad.getString("system"));
  244. oRequest.appendNode("resource", zMethod.ToUpper());
  245. oRequest.appendNode("action", "true");
  246. // Create instance of security object
  247. //Security_nTX oSecurity = new Security_nTX();
  248. //// Check user rights
  249. //XMLHelper oResponse = new XMLHelper();
  250. //oResponse.load(oSecurity.AuthoriseUserAction(oRequest.getRootXML()));
  251. //if (oResponse.getString("poReturnValue").StartsWith("TRUE") == false)
  252. // throwError("User '" + zUsername + "' does not have rights to perform the action '" + zMethod + "'.");
  253. ////throwError("Request : " + oRequest.getXML() + " Response : " + oResponse.getXML());
  254. }
  255. //Create System Audit
  256. if (zRequestType != "SYSTEM")
  257. {
  258. //Set oAudit = CreateObject("GSKUtils.Auditor")
  259. //If Len(zpPayload) < 2000 Then
  260. //oAudit.SystemAudit "CIMS", "Unknown", zpMethod, zUsername, zpPayload
  261. //Else
  262. // oAudit.SystemAudit "CIMS", "Unknown", zpMethod, zUsername
  263. //End If
  264. }
  265. return domToLoad;
  266. }
  267. /// <summary>
  268. /// Writes to the event log.
  269. /// </summary>
  270. /// <param name="zSource">The source.</param>
  271. /// <param name="zMessage">The message.</param>
  272. /// <param name="et">The Error type</param>
  273. private void writeEvent(String zSource, String zMessage, ErrorType et)
  274. {
  275. RegistryKey kLM = Registry.LocalMachine;
  276. String zLogDir;
  277. lock (kLM)
  278. {
  279. RegistryKey kSystem = kLM.OpenSubKey("Software\\GlaxoWellcome\\" + zSource);
  280. zLogDir = (String)kSystem.GetValue("ErrorLog");
  281. if (zLogDir == null)
  282. {
  283. kSystem = kLM.OpenSubKey("Software\\GlaxoWellcome\\UN");
  284. zLogDir = (String)kSystem.GetValue("ErrorLog");
  285. }
  286. }
  287. if (zLogDir != null)
  288. {
  289. try
  290. {
  291. StreamWriter osw;
  292. FileInfo ofi = new FileInfo(zLogDir + "\\error.log");
  293. if (!ofi.Exists)
  294. osw = ofi.CreateText();
  295. else
  296. osw = ofi.AppendText();
  297. osw.WriteLine(zMessage);
  298. osw.Close();
  299. }
  300. catch { }
  301. }
  302. }
  303. }
  304. }