[英]What is best way to throw many kind of FaultException?
I want to throw many kind of fault exception: validationFault, businessFault, internallServerErrorFault. 我想抛出多种故障异常:validationFault,businessFault,internallServerErrorFault。 What is best practice separate many different fault 什么是最佳实践,将许多不同的错误分开
validationFault - errors after validation input data to method businessFault - any business/domain excepion - no permission, login name is not free etc. internallSerrverError - any unhandled excepion validationFault-验证输入数据到方法businessFault后的错误-任何业务/域例外-没有权限,登录名不免费等。internallSerrverError-任何未处理的例外
Each fault will be set errorCode 每个故障都会被设置为errorCode
Scenario 1 One type FaultException. 方案1一种类型FaultException。 In BaseException is property with list of validationException, property for Message. BaseException中的属性是带有validationException的列表,Message的属性是列表。 Client catch this FaultException then parse errorCode and give data from properly properties 客户端捕获此FaultException,然后解析errorCode并从适当的属性提供数据
try
{
}
catch (FaultException<BaseException> ex)
{
// in this place will be all fault exception type. From error code client must have
// dedicated kind of fault - validation, business exception. BaseException will
// be has properly set data for validation or business logic exception.
}
catch (FaultException ex)
{
// internal server error
}
Scenario 2 Separate faults: ValidationFoult, BusinnesFault, internalServerFault to different faults FaultException FaultException 方案2的单独错误:ValidationFoult,BusinnesFault,internalServerFault到不同的错误FaultException FaultException
ValidationFault - will be contain data for validation error - dictionary with key - property name, value - error for this property BusinessFault - will be contain message property ValidationFault-将包含验证错误的数据-带键的字典-属性名称,值-此属性的错误BusinessFault-将包含消息属性
Client will be catch separate this fault. 客户将被单独捕获此故障。 Any fault in Fault exception will be internal server error 故障异常中的任何故障都将是服务器内部错误
try
{
}
catch (FaultException<ValidationFoult> ex)
{
}
catch (FaultException<BusinessFault> ex)
{
}
catch (FaultException ex)
{
// internal server error
}
What are another solution for this problem ? 这个问题的另一种解决方案是什么? Any sugestions ? 有任何建议吗?
Instead of separate Fault entities, just create one common entity (maybe called 'CommonFault') and share the error details through properties such as ErrorCode, ErrorMessage, StackTrace (if the service is internal). 代替创建单独的Fault实体,只需创建一个公共实体(可能称为“ CommonFault”)并通过诸如ErrorCode,ErrorMessage,StackTrace之类的属性(如果服务是内部的)共享错误详细信息即可。 Something like the below. 如下所示。
[DataContractAttribute]
public class CommonFault
{
private string errorcode;
private string errormessage;
private string stackTrace;
[DataMemberAttribute]
public string ErrorCode
{
get { return this.code; }
set { this.code = value; }
}
[DataMemberAttribute]
public string Message
{
get { return this.message; }
set { this.message = value; }
}
[DataMemberAttribute]
public string StackTrace
{
get { return this.stackTrace; }
set { this.stackTrace = value; }
}
public CommonFault()
{
}
public CommonFault(string code, string message, string stackTrace)
{
this.Code = code;
this.Message = message;
this.StackTrace = stackTrace;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.