SysExCore.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using WMS.Info;
  3. namespace WMS.Core
  4. {
  5. public class SysExCore : Exception
  6. {
  7. /// <summary>
  8. /// 异常消息
  9. /// </summary>
  10. public string ExMsg { get; private set; }
  11. /// <summary>
  12. /// 内部异常
  13. /// </summary>
  14. public Exception Ex { get; private set; }
  15. /// <summary>
  16. /// 消息类型
  17. /// </summary>
  18. public ESysExType SysExType { get; private set; } = ESysExType.Exception;
  19. //带有一个字符串参数和一个内部异常信息参数的构造函数
  20. public SysExCore(ESysExType sysExType, string exMsg, Exception innerException) : base(exMsg, innerException)
  21. {
  22. Ex = innerException;
  23. ExMsg = exMsg;
  24. SysExType = sysExType;
  25. }
  26. public SysExCore(ESysExType sysExType, string exMsg) : base(exMsg)
  27. {
  28. ExMsg = exMsg;
  29. SysExType = sysExType;
  30. }
  31. /// <summary>
  32. /// 错误提示信息
  33. /// </summary>
  34. public static SysExCore ThrowFailException(string exMsg)
  35. {
  36. return new SysExCore(ESysExType.Fail, exMsg);
  37. }
  38. /// <summary>
  39. /// 错误提示信息
  40. /// </summary>
  41. public static SysExCore ThrowInEmpty()
  42. {
  43. return new SysExCore(ESysExType.Fail, "请求数据为空。");
  44. }
  45. /// <summary>
  46. /// token无效
  47. /// </summary>
  48. public static SysExCore ThrowToken()
  49. {
  50. return new SysExCore(ESysExType.LoginTimeout, "当前登录token无效,请重新登录后再操作。");
  51. }
  52. /// <summary>
  53. /// 登录超时
  54. /// </summary>
  55. public static SysExCore ThrowLoginTimeout()
  56. {
  57. return new SysExCore(ESysExType.LoginTimeout, "登录超时,请重新登录!");
  58. }
  59. /// <summary>
  60. /// 密码过期
  61. /// </summary>
  62. public static SysExCore ThrowPwdExpiration()
  63. {
  64. return new SysExCore(ESysExType.PwdExpiration, "用户密码已过期。");
  65. }
  66. /// <summary>
  67. /// 数据库连接配置文件异常
  68. /// </summary>
  69. public static SysExCore ThrowDbSetError()
  70. {
  71. return new SysExCore(ESysExType.DbSetError, "数据库连接配置文件异常,请联系管理员。");
  72. }
  73. /// <summary>
  74. /// 数据库连接配置文件异常
  75. /// </summary>
  76. public static SysExCore ThrowDbConnError(Exception ex)
  77. {
  78. return new SysExCore(ESysExType.DbConnError, ex.Message, ex);
  79. }
  80. public static SysExCore GetSysExCore(Exception ex)
  81. {
  82. if (ex is SysExCore)
  83. {
  84. return ex as SysExCore;
  85. }
  86. else
  87. {
  88. return new SysExCore(ESysExType.Exception, ex.Message, ex);
  89. }
  90. }
  91. public static ResInfo GetResErr(Exception ex, object data = null)
  92. {
  93. SysExCore ec;
  94. if (ex is SysExCore)
  95. {
  96. ec = ex as SysExCore;
  97. }
  98. else
  99. {
  100. ec = new SysExCore(ESysExType.Exception, ex.Message, ex);
  101. }
  102. EResponseCode rescode = EResponseCode.Fail;
  103. if (ec.SysExType == ESysExType.Exception)
  104. {
  105. rescode = EResponseCode.Exception;
  106. }
  107. else if (ec.SysExType == ESysExType.LoginTimeout)
  108. {
  109. rescode = EResponseCode.LoginTimeout;
  110. }
  111. else if (ec.SysExType == ESysExType.PwdExpiration)
  112. {
  113. rescode = EResponseCode.PwdExpiration;
  114. }
  115. else if (ec.SysExType == ESysExType.PwdInit)
  116. {
  117. rescode = EResponseCode.PwdInit;
  118. }
  119. else if (ec.SysExType == ESysExType.FilterIPError)
  120. {
  121. rescode = EResponseCode.FilterIPError;
  122. }
  123. else if (ec.SysExType == ESysExType.FilterTimeError)
  124. {
  125. rescode = EResponseCode.FilterTimeError;
  126. }
  127. ResInfo infos = new ResInfo();
  128. infos.code = rescode;
  129. infos.info = ex.Message;
  130. infos.data = data;
  131. return infos;
  132. //return new ResInfo() { code = rescode, info = ex.Message, data = data };
  133. }
  134. public static ResInfo GetResSucc(string Msg="", object data = null)
  135. {
  136. return new ResInfo() { code = EResponseCode.Success, data = data, info = string.IsNullOrWhiteSpace(Msg) ? "响应成功" : Msg };
  137. }
  138. }
  139. }