我有一个关于 WCF 错误异常的问题。我正在实现 IErrorHandler 接口并创建一个FaultException
.
我的问题是:我什么时候创建一个FaultException
,我必须遵循一个模式来向客户发送友好的错误消息吗?
我使用该方法将实际异常记录到数据库并在该方法中HandleError
创建一个。FaultException
ProvideFault
鉴于 delow,有我的 IErrorHandler 接口的示例实现。
public bool HandleError(Exception error)
{
System.Diagnostics.Debug.WriteLine(error.Message);
// Log the error to the Db
return true;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
// Here is a sample that I was playing with.
MessageFault faultException;
if(error is DataException)
faultException = new FaultException<string>(ErrorStrings.DatabaseOpenError, new FaultReason(ErrorStrings.DatabaseOpenError)).CreateMessageFault();
else if (error is NullReferenceException)
faultException = new FaultException<string>(ErrorStrings.NullDataError, new FaultReason(ErrorStrings.NullDataError)).CreateMessageFault();
else
faultException = new FaultException<string>(ErrorStrings.GeneralError, new FaultReason(ErrorStrings.GeneralError)).CreateMessageFault();
fault = Message.CreateMessage(version, faultException, "");
}
我需要弄清楚在方法内部究竟必须做什么ProvideFault
才能向用户返回友好的故障。我还计划使用本地化来显示错误。不确定这是否将由客户端完成,或者服务是否应该发送本地化消息。
根据我阅读的内容,出于安全原因,我不应该将错误或 EntityFramework 错误(如 EntityValidation 错误等)中的堆栈信息发送给客户端。
在这种情况下,我是否需要在发送给客户之前检查异常类型并挖掘更合适的详细信息?