2017-02-15 73 views
0

我想覆蓋從我的服務調用拋出的異常到我自己的自定義異常。根據服務調用返回的StatusCode,我想使用兩個自定義異常。 ServerException爲500+或ClientException爲400+錯誤。用新的自定義期望覆蓋拋出的異常

catch (ApiException e) 
{ 
    if ((int)e.StatusCode >= 400 && (int)e.StatusCode < 500) 
    { 
     throw new ClientException(e.StatusCode.ToString(), e.Message, e.Uri.ToString(), e.HttpMethod, e.Content, e.StatusCode, e.InnerException.Message); 
    } 
    else 
    { 
     throw new ServerException(e.StatusCode.ToString(), e.Message, e.Uri.ToString(), e.HttpMethod, e.Content, e.StatusCode, e.InnerException.Message); 
    } 
} 

我需要重寫示例中捕獲的ApiException。出於某種原因,該示例返回一個帶有ApiException異常對象作爲內部異常。不返回客戶端或服務器異常。

到服務呼叫:

catch(ClientException e) 
{} 
catch(ServerException e) 
{} 
catch (ApiException e) 
{} 
catch (Exception e) 
{ 
    //Always caught here only 
} 

ClientException類,縮短了可讀性:

public class ClientException : Exception 
{ 
    public string Code { get; private set; } 
    public string ReasonPhrase { get; private set; } 
    public ClientException(string code, string message) : base(message) 
    { 
     Code = code; 
     ReasonPhrase = message; 
    } 
} 
+0

是不是異步的封閉方法? (即基於任務) –

+0

使用refit進行服務調用,所以服務調用本身是異步的,但不是保存調用的方法。 – cfl

+0

你確定你確實在捕捉異常嗎? – FINDarkside

回答

1

我覺得你的代碼拋出ApiException像

public void foo(){ 
.... 
throe new ApiException(); 
} 

,並在你的代碼中有

try 
{ 
    foo();//throe ApiException 
} 
catch(ClientException e) 
{} 
catch(ServerException e) 
{} 
catch (ApiException e) 
{ 
    var ex=ExceptionLocator.Get(ExceptionCode);//use ExceptionLocator to locate your custom exception 
    throw ex; 
} 
catch (Exception e) 
{ 
    //Always caught here only 
} 

public abstract class BusinessException : Exception 
{ 
    public string Code { get; private set; } 
    public string ReasonPhrase { get; private set; } 
    public ClientException(string code, string message) : base(message) 
    { 
     Code = code; 
     ReasonPhrase = message; 
    } 
} 

public class ClientException:BusinessException 
{ 

} 

public class ServerException:BusinessException 
{ 

} 

public static class ExceptionLocator{ 
    static Dictionary<int,BusinessException> locator=new Dictionary<int,BusinessException>(); 

    public ExceptionLocator() 
    { 
     locator.Add(400,new ClientException()); 
     locator.Add(500,new ServerException()); 
     .... 
    } 
    public static BusinessException Get(int code) 
    { 
     return locator[code]; 
    } 
} 

您可以使用ExceptionLocator找到您的自定義異常或在Foo()中決定拋出ClientException或Serv erException

+0

你使用ExceptionLocator的庫是什麼?謝謝 – cfl

+1

@cfl我更新了我的答案 –

0

您沒有定義方法接受您的參數

throw new ClientException(
    e.StatusCode.ToString(), 
    e.Message, 
    e.Uri.ToString(), 
    e.HttpMethod, 
    e.Content, 
    e.StatusCode, 
    e.InnerException.Message); 

你需要一個在課堂上的方法,如

public ClientException(
    string StatusCode, 
    string Message, 
    string Uri 
    string HttpMethod, 
    string Content, 
    string StatusCode, 
    string InnerExceptionMessage) : base(...) 
{ 
    ... 
} 

或者,你可以使用

public ClientException(params object[] args) 
     : base(...) { } 
相關問題