MuchinfoException.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System.Collections;
  2. using System.Dynamic;
  3. using Muchinfo.MTPClient.Resources;
  4. using System;
  5. namespace Muchinfo.MTPClient.CustomException
  6. {
  7. public class MuchinfoException : System.Exception
  8. {
  9. public static CommonResx ErrorResx
  10. {
  11. get; set;
  12. }
  13. /// <summary>
  14. /// 服务端返回码
  15. /// </summary>
  16. public int ReturnCode { get; set; }
  17. /// <summary>
  18. /// 服务端返回信息描述
  19. /// </summary>
  20. public string ReturnDesc
  21. {
  22. get;
  23. set;
  24. }
  25. /// <summary>
  26. ///
  27. /// </summary>
  28. public Type TypeClass { get; set; }
  29. /// <summary>
  30. /// 异常信息
  31. /// </summary>
  32. /// <value>The exception message.</value>
  33. public Exception InnerExcption { get; set; }
  34. public MuchinfoException(int returnCode)
  35. {
  36. ReturnCode = returnCode;
  37. }
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="MuchinfoException"/> class.
  40. /// </summary>
  41. /// <param name="returnCode">The return code.</param>
  42. public MuchinfoException(int returnCode, string returnDesc)
  43. {
  44. ReturnCode = returnCode;
  45. ReturnDesc = returnDesc;
  46. }
  47. public MuchinfoException(int returnCode, string returnDesc, Type type)
  48. {
  49. ReturnCode = returnCode;
  50. ReturnDesc = returnDesc;
  51. TypeClass = type;
  52. }
  53. /// <summary>
  54. /// Initializes a new instance of the <see cref="MuchinfoException" /> class.
  55. /// </summary>
  56. /// <param name="returnCode">The return code.</param>
  57. /// <param name="innerException">The inner exception.</param>
  58. public MuchinfoException(int returnCode, Exception innerException)
  59. : base(innerException == null ? string.Empty : innerException.ToString(), innerException)
  60. {
  61. ReturnCode = returnCode;
  62. }
  63. /// <summary>
  64. /// 获取描述当前异常的消息。
  65. /// </summary>
  66. /// <returns>解释异常原因的错误消息或空字符串 ("")。</returns>
  67. public override string Message
  68. {
  69. get
  70. {
  71. var message = ErrorDesc(ReturnCode);
  72. message = string.IsNullOrWhiteSpace(message) ? ReturnDesc : message ;
  73. if (string.IsNullOrWhiteSpace(message))
  74. {
  75. message = Client_Resource.CustomException_UnknowCode + ReturnCode;
  76. }
  77. else
  78. {
  79. message += Environment.NewLine + Client_Resource.CustomException_Code + ReturnCode;
  80. }
  81. return message;
  82. }
  83. }
  84. public static string ErrorDesc(int returnCode)
  85. {
  86. var retStr = string.Empty;
  87. var strCode = returnCode + string.Empty;
  88. if (ErrorResx != null && ErrorResx.ResourceHashtable != null &&
  89. ErrorResx.ResourceHashtable.ContainsKey(strCode))
  90. {
  91. retStr = ErrorResx.ResourceHashtable[strCode] as string;
  92. }
  93. else
  94. {
  95. retStr = ErrorResources.ResourceManager.GetString(returnCode + string.Empty);
  96. }
  97. if (string.IsNullOrEmpty(retStr))
  98. {
  99. retStr = ClientErrorResources.ResourceManager.GetString(returnCode + string.Empty);
  100. }
  101. return retStr;
  102. }
  103. /// <summary>
  104. /// 初始化错误资源
  105. /// </summary>
  106. public static void InitErrorResx()
  107. {
  108. var errorCodeManager=new ErrorCodeManager();
  109. ErrorResx = errorCodeManager.GetErrorCode();
  110. }
  111. /// <summary>
  112. /// 保存并设置资源内容
  113. /// </summary>
  114. /// <param name="resxHashtable"></param>
  115. /// <param name="ver"></param>
  116. public static void ErrorResxSaveRep(Hashtable resxHashtable,ulong ver)
  117. {
  118. var errorCodeManager = new ErrorCodeManager();
  119. ErrorResx = new CommonResx() { ResourceHashtable = resxHashtable, Version = ver };
  120. errorCodeManager.SaveErrorCode(resxHashtable,ver);
  121. }
  122. }
  123. }