OSDN Git Service

TwitterApiConnection.GetStreamAsyncメソッドを削除
[opentween/open-tween.git] / OpenTween / ErrorReportHandler.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2022 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU General public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 #nullable enable
23
24 using System;
25 using System.Diagnostics;
26 using System.Linq;
27 using System.Net;
28 using System.Threading;
29 using System.Threading.Tasks;
30 using System.Windows.Forms;
31 using OpenTween.Connection;
32
33 namespace OpenTween
34 {
35     public sealed class ErrorReportHandler : IDisposable
36     {
37         public bool IsDisposed { get; private set; } = false;
38
39         public ErrorReportHandler()
40             => this.RegisterHandlers();
41
42         private void RegisterHandlers()
43         {
44             TaskScheduler.UnobservedTaskException += this.TaskScheduler_UnobservedTaskException;
45             AsyncTimer.UnhandledException += this.AsyncTimer_UnhandledException;
46             Application.ThreadException += this.Application_ThreadException;
47             AppDomain.CurrentDomain.UnhandledException += this.AppDomain_UnhandledException;
48         }
49
50         private void UnregisterHandlers()
51         {
52             TaskScheduler.UnobservedTaskException -= this.TaskScheduler_UnobservedTaskException;
53             AsyncTimer.UnhandledException -= this.AsyncTimer_UnhandledException;
54             Application.ThreadException -= this.Application_ThreadException;
55             AppDomain.CurrentDomain.UnhandledException -= this.AppDomain_UnhandledException;
56         }
57
58         private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
59         {
60             e.SetObserved();
61             this.OnUnhandledException(e.Exception.Flatten());
62         }
63
64         private void AsyncTimer_UnhandledException(object sender, ThreadExceptionEventArgs e)
65             => this.OnUnhandledException(e.Exception);
66
67         private void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
68             => this.OnUnhandledException(e.Exception);
69
70         private void AppDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
71             => this.OnUnhandledException((Exception)e.ExceptionObject);
72
73         private void OnUnhandledException(Exception ex)
74         {
75 #if !DEBUG
76             if (ErrorReportHandler.IsExceptionIgnorable(ex))
77                 return;
78 #endif
79
80             if (MyCommon.ExceptionOut(ex))
81                 Application.Exit();
82         }
83
84         public void Dispose()
85         {
86             if (this.IsDisposed)
87                 return;
88
89             this.IsDisposed = true;
90             this.UnregisterHandlers();
91         }
92
93         /// <summary>
94         /// 無視しても問題のない既知の例外であれば true を返す
95         /// </summary>
96         public static bool IsExceptionIgnorable(Exception ex)
97         {
98             if (ex is AggregateException aggregated)
99                 return aggregated.InnerExceptions.All(x => IsExceptionIgnorable(x));
100
101             if (ex is WebException webEx)
102             {
103                 // SSL/TLS のネゴシエーションに失敗した場合に発生する。なぜかキャッチできない例外
104                 // https://osdn.net/ticket/browse.php?group_id=6526&tid=37432
105                 if (webEx.Status == WebExceptionStatus.SecureChannelFailure)
106                     return true;
107             }
108
109             return false;
110         }
111     }
112 }