OSDN Git Service

無視できる既知の例外についてReleaseビルドではエラーダイアログを表示しない
authorKimura Youichi <kim.upsilon@bucyou.net>
Sat, 12 Aug 2017 05:40:53 +0000 (14:40 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Sat, 12 Aug 2017 05:40:53 +0000 (14:40 +0900)
https://osdn.net/ticket/browse.php?group_id=6526&tid=37432
https://osdn.net/ticket/browse.php?group_id=6526&tid=37433

OpenTween/ApplicationEvents.cs

index 0fc07fd..628cea8 100644 (file)
@@ -205,12 +205,54 @@ namespace OpenTween
 
         private static void OnUnhandledException(Exception ex)
         {
+            if (CheckIgnorableError(ex))
+                return;
+
             if (MyCommon.ExceptionOut(ex))
             {
                 Application.Exit();
             }
         }
 
+        /// <summary>
+        /// 無視しても問題のない既知の例外であれば true を返す
+        /// </summary>
+        private static bool CheckIgnorableError(Exception ex)
+        {
+#if DEBUG
+            return false;
+#else
+            if (ex is AggregateException aggregated)
+            {
+                if (aggregated.InnerExceptions.Count != 1)
+                    return false;
+
+                ex = aggregated.InnerExceptions.Single();
+            }
+
+            switch (ex)
+            {
+                case System.Net.WebException webEx:
+                    // SSL/TLS のネゴシエーションに失敗した場合に発生する。なぜかキャッチできない例外
+                    // https://osdn.net/ticket/browse.php?group_id=6526&tid=37432
+                    if (webEx.Status == System.Net.WebExceptionStatus.SecureChannelFailure)
+                        return true;
+                    break;
+                case System.Threading.Tasks.TaskCanceledException cancelEx:
+                    // ton.twitter.com の画像でタイムアウトした場合、try-catch で例外がキャッチできない
+                    // https://osdn.net/ticket/browse.php?group_id=6526&tid=37433
+                    var stackTrace = new System.Diagnostics.StackTrace(cancelEx);
+                    var lastFrameMethod = stackTrace.GetFrame(stackTrace.FrameCount - 1).GetMethod();
+                    if (lastFrameMethod.ReflectedType == typeof(Connection.TwitterApiConnection) &&
+                        lastFrameMethod.Name == nameof(Connection.TwitterApiConnection.GetStreamAsync))
+                        return true;
+                    break;
+            }
+
+            return false;
+#endif
+        }
+
         public static void InitCulture()
         {
             var currentCulture = CultureInfo.CurrentUICulture;