ExceptionWrapper.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using SKMC.Api.Client.Config;
  2. using SKMC.Api.Client.UI;
  3. using SKMC.Api.Common.Logger;
  4. using SKMC.Api.Machine.Control;
  5. using System;
  6. namespace SKMC.Api.Common.Exceptions
  7. {
  8. /// <summary>
  9. /// 异常包装类
  10. /// </summary>
  11. public class ExceptionWrapper
  12. {
  13. private static readonly ILogger log = LogFactory.Get();
  14. private static readonly IMachineBoardControl machineBoardControl = ObjectFactory.Resolve<IMachineBoardControl>();
  15. private static readonly CommonCacher commonCacher = ObjectFactory.Resolve<CommonCacher>();
  16. public static string Throws(Exception exception, string dialogViewName = ClientConstants.Views_CommonException)
  17. {
  18. if (exception == null) return null;
  19. if (typeof(ExceptionBase).IsAssignableFrom(exception.GetType()))
  20. {
  21. ExceptionBase exceptionBase = exception as ExceptionBase;
  22. ExceptionConfig exceptionConfig = commonCacher.GetExceptionConfig(exceptionBase);
  23. exceptionBase.Level = exceptionConfig.Level;
  24. // 停机异常
  25. if (exceptionConfig.Level == 4)
  26. {
  27. machineBoardControl.IsAlarm = true;
  28. machineBoardControl.StopAction.Invoke();
  29. log.Error($"严重异常: {exception}");
  30. return ClientDialogs.CreateExceptionDialog(exceptionBase, dialogViewName);
  31. }
  32. // 普通异常
  33. if (exceptionConfig.Level == 2)
  34. {
  35. machineBoardControl.IsAlarm = true;
  36. machineBoardControl.PauseAction.Invoke();
  37. log.Error($"普通异常: {exception},{exception.StackTrace}");
  38. return ClientDialogs.CreateExceptionDialog(exceptionBase, dialogViewName);
  39. }
  40. // 单站异常
  41. if (exceptionConfig.Level == 1)
  42. {
  43. if (exceptionBase.StationId > 0)
  44. {
  45. machineBoardControl.PauseOneAction.Invoke(exceptionBase.StationId);
  46. }
  47. log.Error($"单站异常: {exception}");
  48. return ClientDialogs.CreateExceptionDialog(exceptionBase, dialogViewName);
  49. }
  50. else
  51. {
  52. log.Error($"其他异常: {exception}");
  53. return null;
  54. }
  55. }
  56. // 未知异常
  57. else
  58. {
  59. machineBoardControl.IsAlarm = true;
  60. machineBoardControl.StopAction.Invoke();
  61. log.Error($"未知异常: {exception.Message}", exception);
  62. ClientDialogs.CreateExceptionDialog(exception);
  63. return null;
  64. }
  65. }
  66. }
  67. }