Web サービスの呼び出し時に発生したエラーをキャッチするためにいくつかのカスタム例外が必要なユニバーサル アプリを開発しています: NoInternetAccessException、NoJSONException、UserTimeoutException など。
これらのクラスの 1 つの例を次に示します。
public class NoInternetAccessException : Exception
{
private DateTime time;
public DateTime Time { get { return time; } }
public NoInternetAccessException(string message, DateTime time)
: base(message)
{
this.time = time;
}
}
いくつかの場所でこれらの例外をキャッチします。
JSONParserで、URI を作成して Client.GetAsync メソッドを呼び出します。
...
try
{
CancellationTokenSource cts = new CancellationTokenSource(Timeout);
response = await client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead).AsTask(cts.Token);
if (response.IsSuccessStatusCode)
return response;
}
catch (TaskCanceledException)
{
if ((_currentRetries == MaxRetries)
throw new UserTimeoutException("User Timeout Exception", DateTime.Now);
else
_currentRetries++;
}
catch (Exception e)
{
if (e.HResult == -2147012889)
throw new WrongUrlException("Wrong URL Exception", e, DateTime.Now);
}
...
すべての webservices 呼び出しを管理するWebServiceヘルパーで:
public static async Task<Infos> GetInfos(String url, List<KeyValuePair<String, String>> parameters, String protocol)
{
var response = await JSONParser.getJSONFromUrl(url, parameters, "");
Infos infos = new Infos();
try
{
WsResponse wsResponse = JsonConvert.DeserializeObject<Infos>(response.ToString());
infosCe = JsonConvert.DeserializeObject<Infos>(wsResponse.data.ToString());
return infosCe;
}
catch (Exception e)
{
throw new DeserializeException("Deserialize exception", e, DateTime.Now, "Infos");
}
}
これらの例外はすべて、Web サービスへの呼び出しの後、最終的に ViewModels によってキャッチされます。
private async Task<Infos> WebServiceGetInfos()
{
...
try
{
Infos infos = await WebServices.GetInfos(url, parameters, "");
return infosCe;
}
// Exceptions
catch (DeserializeException dE)
{
ExceptionsMsgboxHelper.MsgboxDeserialize(dE);
return null;
}
catch (NoInternetAccessException niaE)
{
ExceptionsMsgboxHelper.MsgboxNoInternetAccess(niaE, true, "");
return null;
}
catch (NoJSONException njsonE)
{
ExceptionsMsgboxHelper.MsgboxNoJSON(njsonE);
return null;
}
...
}
各例外が例外ごとに特定のメッセージ ダイアログを表示するExceptionsMsgboxHelperヘルパーを呼び出すようにしたいと思います。
public async static void MsgboxNoJSON(NoJSONException njsonE)
{
Windows.UI.Popups.MessageDialog msgbox =
new Windows.UI.Popups.MessageDialog("There is a problem when retrieving data. If the problem persists, please contact your administrator.",
"Unexpected data received");
await msgbox.ShowAsync();
}
=>しかし、メッセージダイアログがTry-Catch句で機能しないため、これは機能しません...
また、stackoverflow で解決策を探します。
Try-Catch で待機中のメッセージ ダイアログ ボックスが表示されない
しかし、いくつかのカスタム例外を使用しているため、このソリューションを私の場合に適応させる方法がわかりません。
public static async Task Foo()
{
Exception e = null;
try
{
//just something to throw an exception
int a = 0;
int n = 1 / a;
}
catch (Exception ex)
{
e = ex;
}
if (e != null)
await ShowDialog();
}
=>このコードを各カスタム例外に複製するよりも、これを行う方が良い方法ですか?
...
DeserializeException dEx = null;
NoInternetAccessException niaEx = null;
NoJSONException njsonEx = null;
try
{
Infos infos = await WebServices.GetInfos(url, parameters, "");
return infosCe;
}
// Exceptions
catch (DeserializeException dE)
{
dEx = de;
}
catch (NoInternetAccessException niaE)
{
niaEx = niaE;
}
catch (NoJSONException njsonE)
{
njsonEx=njsonE;
}
...
if (dEx != null)
{
ExceptionsMsgboxHelper.MsgboxDeserialize(dEx);
return null;
}
if (niaEx != null)
{
ExceptionsMsgboxHelper.MsgboxNoInternetAccess(niaEx);
return null;
}
if (njsonEx != null)
{
ExceptionsMsgboxHelper.MsgboxNoJSON(njsonEx);
return null;
}
...
=> 強力でも保守可能でもないようです...