ExceptionManager.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:ExceptionManager.cs
  5. * 2.功能描述:客户端逻辑处理的共通处理(同一用户登录、权限判断等等)
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈冰 2014/08/30 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Data;
  12. using System.ServiceModel;
  13. using System.Windows.Forms;
  14. using Dongke.IBOSS.PRD.Basics.BaseResources;
  15. using Dongke.IBOSS.PRD.Basics.Library;
  16. namespace Dongke.IBOSS.PRD.Client.CommonModule
  17. {
  18. /// <summary>
  19. /// 系统异常管理
  20. /// </summary>
  21. public class ExceptionManager
  22. {
  23. #region 常量定义
  24. private const string REPEAT_LOGIN = "RepeatLogin"; // 重复登录
  25. private const string VALIDATION_KEY = "ValidationKey"; // 私有秘钥验证失败
  26. private const string LOGININFO_ERROR = "LoginInfoError"; // 用户登录信息验证失败
  27. private const string SYSTEMDATETIMEERROR = "SystemDateTimeError"; // 服务器日期异常
  28. #endregion
  29. #region 公开方法
  30. /// <summary>
  31. /// 对捕获的异常进行甑别,如果有验证非法则重启程序。
  32. /// </summary>
  33. /// <param name="ex">捕获的异常</param>
  34. public static void AuthenticateFailRequest(Exception ex)
  35. {
  36. // 提示信息的内容
  37. string errorMessage;
  38. // 程序是否关闭
  39. bool isCloseApplication = false;
  40. if (ex is ProtocolException)
  41. {
  42. // 重复登录
  43. if (ex.Message.Contains(REPEAT_LOGIN))
  44. {
  45. errorMessage = Messages.MSG_CMN_W008;
  46. isCloseApplication = true;
  47. }
  48. else if (ex.Message.Contains(VALIDATION_KEY))
  49. {
  50. errorMessage = Messages.MSG_CMN_W009;
  51. isCloseApplication = true;
  52. }
  53. else if (ex.Message.Contains(LOGININFO_ERROR))
  54. {
  55. errorMessage = Messages.MSG_CMN_W010;
  56. isCloseApplication = true;
  57. }
  58. else if (ex.Message.Contains(SYSTEMDATETIMEERROR))
  59. {
  60. errorMessage = Messages.MSG_CMN_W022;
  61. isCloseApplication = true;
  62. }
  63. else if (ex.Message.Contains("LicInfoError"))
  64. {
  65. errorMessage = "授权信息错误,请联系管理员。";
  66. isCloseApplication = true;
  67. }
  68. else
  69. {
  70. throw ex;
  71. }
  72. }
  73. else
  74. {
  75. throw ex;
  76. }
  77. // 写错误日志
  78. OutputLog.Trace(LogPriority.Warning,
  79. "Authorization",
  80. "Authorization()",
  81. ex.ToString());
  82. // 提示对话框
  83. MessageBox.Show(errorMessage, Messages.MSG_TITLE_W01,
  84. MessageBoxButtons.OK,
  85. MessageBoxIcon.Warning);
  86. if (isCloseApplication)
  87. {
  88. System.Diagnostics.Process.GetCurrentProcess().Close();
  89. System.Environment.Exit(0);
  90. }
  91. }
  92. /// <summary>
  93. /// 对画面最终事件的异常进行处理
  94. /// </summary>
  95. /// <param name="fileName">源文件名称</param>
  96. /// <param name="eventName">事件名称</param>
  97. /// <param name="formTitle">窗体名称</param>
  98. /// <param name="ex">异常</param>
  99. public static void HandleEventException(string fileName, string eventName, string formTitle, Exception ex)
  100. {
  101. //提示信息的内容
  102. string errorMessage = string.Empty;
  103. MessageBoxIcon mbi = MessageBoxIcon.Warning;
  104. #region 需要退出系统重新登录的异常处理逻辑
  105. //程序是否关闭
  106. bool isCloseApplication = false;
  107. if (ex.Message.Contains(REPEAT_LOGIN))
  108. {
  109. errorMessage = Messages.MSG_CMN_W008;
  110. isCloseApplication = true;
  111. }
  112. else if (ex.Message.Contains(VALIDATION_KEY))
  113. {
  114. errorMessage = Messages.MSG_CMN_W009;
  115. isCloseApplication = true;
  116. }
  117. else if (ex.Message.Contains(LOGININFO_ERROR))
  118. {
  119. errorMessage = Messages.MSG_CMN_W010;
  120. isCloseApplication = true;
  121. }
  122. else if (ex.Message.Contains(SYSTEMDATETIMEERROR))
  123. {
  124. errorMessage = Messages.MSG_CMN_W022;
  125. isCloseApplication = true;
  126. }
  127. else if (ex.Message.Contains("LicInfoError"))
  128. {
  129. errorMessage = "授权信息错误,请联系管理员。";
  130. mbi = MessageBoxIcon.Error;
  131. isCloseApplication = true;
  132. }
  133. if (isCloseApplication)
  134. {
  135. // 写错误日志
  136. OutputLog.Trace(LogPriority.Warning,
  137. "Authorization",
  138. "Authorization()",
  139. ex.ToString());
  140. // 提示对话框
  141. MessageBox.Show(errorMessage, Messages.MSG_TITLE_W01,
  142. MessageBoxButtons.OK,
  143. mbi);
  144. System.Diagnostics.Process.GetCurrentProcess().Close();
  145. System.Environment.Exit(0);
  146. return;
  147. }
  148. #endregion
  149. bool isError = false;
  150. bool isPrint = false;
  151. if (ex is ClientPrintException)
  152. {
  153. ex = ex.InnerException;
  154. isPrint = true;
  155. }
  156. if (ex is EndpointNotFoundException)
  157. {
  158. // 无法连接到远程的WEB服务器
  159. errorMessage = Messages.MSG_CMN_E002;
  160. }
  161. //// 与远程WEB服务器通讯出现异常
  162. //else if (ex is CommunicationException)
  163. //{
  164. // errorMessage = Messages.MSG_CMN_E003;
  165. //}
  166. // 与远程WEB服务器连接超时
  167. else if (ex is TimeoutException)
  168. {
  169. errorMessage = Messages.MSG_CMN_E004;
  170. }
  171. // 内存不足
  172. else if (ex is OutOfMemoryException)
  173. {
  174. errorMessage = Messages.MSG_CMN_E005;
  175. }
  176. else if (isPrint)
  177. {
  178. errorMessage = "客户端打印异常,请联系系统管理员。";
  179. isError = true;
  180. }
  181. else
  182. {
  183. errorMessage = Messages.MSG_CMN_E001;
  184. isError = true;
  185. }
  186. // 写错误日志
  187. OutputLog.Trace(LogPriority.Error, fileName, eventName, ex.ToString());
  188. // 提示对话框
  189. MessageBox.Show(errorMessage, formTitle,
  190. MessageBoxButtons.OK,
  191. isError ? MessageBoxIcon.Error : MessageBoxIcon.Warning);
  192. }
  193. #endregion
  194. }
  195. }