/*******************************************************************************
* Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
* 类的信息:
* 1.程序名称:ExceptionManager.cs
* 2.功能描述:客户端逻辑处理的共通处理(同一用户登录、权限判断等等)
* 编辑履历:
* 作者 日期 版本 修改内容
* 陈冰 2014/08/30 1.00 新建
*******************************************************************************/
using System;
using System.Data;
using System.ServiceModel;
using System.Windows.Forms;
using Dongke.IBOSS.PRD.Basics.BaseResources;
using Dongke.IBOSS.PRD.Basics.Library;
namespace Dongke.IBOSS.PRD.Client.CommonModule
{
///
/// 系统异常管理
///
public class ExceptionManager
{
#region 常量定义
private const string REPEAT_LOGIN = "RepeatLogin"; // 重复登录
private const string VALIDATION_KEY = "ValidationKey"; // 私有秘钥验证失败
private const string LOGININFO_ERROR = "LoginInfoError"; // 用户登录信息验证失败
private const string SYSTEMDATETIMEERROR = "SystemDateTimeError"; // 服务器日期异常
#endregion
#region 公开方法
///
/// 对捕获的异常进行甑别,如果有验证非法则重启程序。
///
/// 捕获的异常
public static void AuthenticateFailRequest(Exception ex)
{
// 提示信息的内容
string errorMessage;
// 程序是否关闭
bool isCloseApplication = false;
if (ex is ProtocolException)
{
// 重复登录
if (ex.Message.Contains(REPEAT_LOGIN))
{
errorMessage = Messages.MSG_CMN_W008;
isCloseApplication = true;
}
else if (ex.Message.Contains(VALIDATION_KEY))
{
errorMessage = Messages.MSG_CMN_W009;
isCloseApplication = true;
}
else if (ex.Message.Contains(LOGININFO_ERROR))
{
errorMessage = Messages.MSG_CMN_W010;
isCloseApplication = true;
}
else if (ex.Message.Contains(SYSTEMDATETIMEERROR))
{
errorMessage = Messages.MSG_CMN_W022;
isCloseApplication = true;
}
else if (ex.Message.Contains("LicInfoError"))
{
errorMessage = "授权信息错误,请联系管理员。";
isCloseApplication = true;
}
else
{
throw ex;
}
}
else
{
throw ex;
}
// 写错误日志
OutputLog.Trace(LogPriority.Warning,
"Authorization",
"Authorization()",
ex.ToString());
// 提示对话框
MessageBox.Show(errorMessage, Messages.MSG_TITLE_W01,
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
if (isCloseApplication)
{
System.Diagnostics.Process.GetCurrentProcess().Close();
System.Environment.Exit(0);
}
}
///
/// 对画面最终事件的异常进行处理
///
/// 源文件名称
/// 事件名称
/// 窗体名称
/// 异常
public static void HandleEventException(string fileName, string eventName, string formTitle, Exception ex)
{
//提示信息的内容
string errorMessage = string.Empty;
MessageBoxIcon mbi = MessageBoxIcon.Warning;
#region 需要退出系统重新登录的异常处理逻辑
//程序是否关闭
bool isCloseApplication = false;
if (ex.Message.Contains(REPEAT_LOGIN))
{
errorMessage = Messages.MSG_CMN_W008;
isCloseApplication = true;
}
else if (ex.Message.Contains(VALIDATION_KEY))
{
errorMessage = Messages.MSG_CMN_W009;
isCloseApplication = true;
}
else if (ex.Message.Contains(LOGININFO_ERROR))
{
errorMessage = Messages.MSG_CMN_W010;
isCloseApplication = true;
}
else if (ex.Message.Contains(SYSTEMDATETIMEERROR))
{
errorMessage = Messages.MSG_CMN_W022;
isCloseApplication = true;
}
else if (ex.Message.Contains("LicInfoError"))
{
errorMessage = "授权信息错误,请联系管理员。";
mbi = MessageBoxIcon.Error;
isCloseApplication = true;
}
if (isCloseApplication)
{
// 写错误日志
OutputLog.Trace(LogPriority.Warning,
"Authorization",
"Authorization()",
ex.ToString());
// 提示对话框
MessageBox.Show(errorMessage, Messages.MSG_TITLE_W01,
MessageBoxButtons.OK,
mbi);
System.Diagnostics.Process.GetCurrentProcess().Close();
System.Environment.Exit(0);
return;
}
#endregion
bool isError = false;
bool isPrint = false;
if (ex is ClientPrintException)
{
ex = ex.InnerException;
isPrint = true;
}
if (ex is EndpointNotFoundException)
{
// 无法连接到远程的WEB服务器
errorMessage = Messages.MSG_CMN_E002;
}
//// 与远程WEB服务器通讯出现异常
//else if (ex is CommunicationException)
//{
// errorMessage = Messages.MSG_CMN_E003;
//}
// 与远程WEB服务器连接超时
else if (ex is TimeoutException)
{
errorMessage = Messages.MSG_CMN_E004;
}
// 内存不足
else if (ex is OutOfMemoryException)
{
errorMessage = Messages.MSG_CMN_E005;
}
else if (isPrint)
{
errorMessage = "客户端打印异常,请联系系统管理员。";
isError = true;
}
else
{
errorMessage = Messages.MSG_CMN_E001;
isError = true;
}
// 写错误日志
OutputLog.Trace(LogPriority.Error, fileName, eventName, ex.ToString());
// 提示对话框
MessageBox.Show(errorMessage, formTitle,
MessageBoxButtons.OK,
isError ? MessageBoxIcon.Error : MessageBoxIcon.Warning);
}
#endregion
}
}