简体   繁体   中英

What is best way to throw many kind of FaultException?

I want to throw many kind of fault exception: 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

Each fault will be set errorCode

Scenario 1 One type FaultException. In BaseException is property with list of validationException, property for Message. Client catch this FaultException then parse errorCode and give data from properly properties

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

ValidationFault - will be contain data for validation error - dictionary with key - property name, value - error for this property BusinessFault - will be contain message property

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). 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM