1 // OpenTween - Client of Twitter
2 // Copyright (c) 2022 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
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)
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
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.
25 using System.Diagnostics;
28 using System.Threading;
29 using System.Threading.Tasks;
30 using System.Windows.Forms;
31 using OpenTween.Connection;
35 public sealed class ErrorReportHandler : IDisposable
37 public bool IsDisposed { get; private set; } = false;
39 public ErrorReportHandler()
40 => this.RegisterHandlers();
42 private void RegisterHandlers()
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;
50 private void UnregisterHandlers()
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;
58 private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
61 this.OnUnhandledException(e.Exception.Flatten());
64 private void AsyncTimer_UnhandledException(object sender, ThreadExceptionEventArgs e)
65 => this.OnUnhandledException(e.Exception);
67 private void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
68 => this.OnUnhandledException(e.Exception);
70 private void AppDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
71 => this.OnUnhandledException((Exception)e.ExceptionObject);
73 private void OnUnhandledException(Exception ex)
76 if (ErrorReportHandler.IsExceptionIgnorable(ex))
80 if (MyCommon.ExceptionOut(ex))
89 this.IsDisposed = true;
90 this.UnregisterHandlers();
94 /// 無視しても問題のない既知の例外であれば true を返す
96 public static bool IsExceptionIgnorable(Exception ex)
98 if (ex is AggregateException aggregated)
99 return aggregated.InnerExceptions.All(x => IsExceptionIgnorable(x));
101 if (ex is WebException webEx)
103 // SSL/TLS のネゴシエーションに失敗した場合に発生する。なぜかキャッチできない例外
104 // https://osdn.net/ticket/browse.php?group_id=6526&tid=37432
105 if (webEx.Status == WebExceptionStatus.SecureChannelFailure)