Skip to content

Commit ef0f9b1

Browse files
committed
Refactor error handling and improve logging
Removed try-catch blocks from several methods to allow exceptions to propagate naturally and centralized error logging. Enhanced download and patching methods with more detailed tracing and logging. Moved GetPacketHash to AbstractBootstrap for reuse. Cleaned up redundant code and improved code clarity in differential update logic.
1 parent e2004ed commit ef0f9b1

File tree

7 files changed

+357
-400
lines changed

7 files changed

+357
-400
lines changed

src/c#/GeneralUpdate.ClientCore/GeneralClientBootstrap.cs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -250,25 +250,17 @@ private async Task ExecuteWorkflowAsync()
250250

251251
private async Task Download()
252252
{
253-
try
254-
{
255-
var manager = new DownloadManager(_configInfo.TempPath, _configInfo.Format, _configInfo.DownloadTimeOut);
256-
manager.MultiAllDownloadCompleted += OnMultiAllDownloadCompleted;
257-
manager.MultiDownloadCompleted += OnMultiDownloadCompleted;
258-
manager.MultiDownloadError += OnMultiDownloadError;
259-
manager.MultiDownloadStatistics += OnMultiDownloadStatistics;
260-
foreach (var versionInfo in _configInfo.UpdateVersions)
261-
{
262-
manager.Add(new DownloadTask(manager, versionInfo));
263-
}
264-
265-
await manager.LaunchTasksAsync();
266-
}
267-
catch (Exception exception)
253+
var manager = new DownloadManager(_configInfo.TempPath, _configInfo.Format, _configInfo.DownloadTimeOut);
254+
manager.MultiAllDownloadCompleted += OnMultiAllDownloadCompleted;
255+
manager.MultiDownloadCompleted += OnMultiDownloadCompleted;
256+
manager.MultiDownloadError += OnMultiDownloadError;
257+
manager.MultiDownloadStatistics += OnMultiDownloadStatistics;
258+
foreach (var versionInfo in _configInfo.UpdateVersions)
268259
{
269-
GeneralTracer.Error("The Download method in the GeneralClientBootstrap class throws an exception." , exception);
270-
EventManager.Instance.Dispatch(this, new ExceptionEventArgs(exception, exception.Message));
260+
manager.Add(new DownloadTask(manager, versionInfo));
271261
}
262+
263+
await manager.LaunchTasksAsync();
272264
}
273265

274266
private int GetPlatform()
@@ -451,8 +443,5 @@ private void OnMultiAllDownloadCompleted(object sender, MultiAllDownloadComplete
451443
EventManager.Instance.Dispatch(sender, e);
452444
}
453445

454-
private string GetPacketHash(object version) =>
455-
!GeneralTracer.IsTracingEnabled() ? string.Empty : $"[PacketHash]:{(version as VersionInfo).Hash} ";
456-
457446
#endregion Private Methods
458447
}

src/c#/GeneralUpdate.ClientCore/GeneralClientOSS.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@ await Task.Run(() =>
5151
Process.Start(appPath);
5252
Process.GetCurrentProcess().Kill();
5353
}
54-
catch (Exception ex)
54+
catch (Exception exception)
5555
{
56-
var error = ex.Message + "\n" + ex.StackTrace;
57-
GeneralTracer.Error(error);
58-
throw new Exception(error);
56+
GeneralTracer.Error("The BaseStart method in the GeneralClientOSS class throws an exception.", exception);
57+
throw exception;
5958
}
6059
});
6160
}

src/c#/GeneralUpdate.Common/Internal/Bootstrap/AbstractBootstrap.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Diagnostics;
33
using System.Threading.Tasks;
44
using GeneralUpdate.Common.Internal.Strategy;
5+
using GeneralUpdate.Common.Shared.Object;
56

67
namespace GeneralUpdate.Common.Internal.Bootstrap
78
{
@@ -60,5 +61,8 @@ public TBootstrap Option<T>(UpdateOption<T> option, T value)
6061
return default;
6162
}
6263
}
64+
65+
protected string GetPacketHash(object version) =>
66+
!GeneralTracer.IsTracingEnabled() ? string.Empty : $"[PacketHash]:{(version as VersionInfo).Hash} ";
6367
}
6468
}

src/c#/GeneralUpdate.Core/GeneralUpdateBootstrap.cs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,19 @@ public GeneralUpdateBootstrap()
5959

6060
public override async Task<GeneralUpdateBootstrap> LaunchAsync()
6161
{
62-
try
62+
StrategyFactory();
63+
var manager =
64+
new DownloadManager(_configInfo.TempPath, _configInfo.Format, _configInfo.DownloadTimeOut);
65+
manager.MultiAllDownloadCompleted += OnMultiAllDownloadCompleted;
66+
manager.MultiDownloadCompleted += OnMultiDownloadCompleted;
67+
manager.MultiDownloadError += OnMultiDownloadError;
68+
manager.MultiDownloadStatistics += OnMultiDownloadStatistics;
69+
foreach (var versionInfo in _configInfo.UpdateVersions)
6370
{
64-
StrategyFactory();
65-
var manager =
66-
new DownloadManager(_configInfo.TempPath, _configInfo.Format, _configInfo.DownloadTimeOut);
67-
manager.MultiAllDownloadCompleted += OnMultiAllDownloadCompleted;
68-
manager.MultiDownloadCompleted += OnMultiDownloadCompleted;
69-
manager.MultiDownloadError += OnMultiDownloadError;
70-
manager.MultiDownloadStatistics += OnMultiDownloadStatistics;
71-
foreach (var versionInfo in _configInfo.UpdateVersions)
72-
{
73-
manager.Add(new DownloadTask(manager, versionInfo));
74-
}
75-
76-
await manager.LaunchTasksAsync();
77-
}
78-
catch (Exception exception)
79-
{
80-
GeneralTracer.Error("The LaunchAsync method in the GeneralUpdateBootstrap class throws an exception.", exception);
81-
EventManager.Instance.Dispatch(this, new ExceptionEventArgs(exception, exception.Message));
71+
manager.Add(new DownloadTask(manager, versionInfo));
8272
}
73+
74+
await manager.LaunchTasksAsync();
8375
return this;
8476
}
8577

@@ -140,13 +132,25 @@ private GeneralUpdateBootstrap AddListener<TArgs>(Action<object, TArgs> callback
140132
}
141133

142134
private void OnMultiDownloadStatistics(object sender, MultiDownloadStatisticsEventArgs e)
143-
=> EventManager.Instance.Dispatch(sender, e);
135+
{
136+
var message = GetPacketHash(e.Version);
137+
GeneralTracer.Info($"Multi download statistics, {message}[BytesReceived]:{e.BytesReceived} [ProgressPercentage]:{e.ProgressPercentage} [Remaining]:{e.Remaining} [TotalBytesToReceive]:{e.TotalBytesToReceive} [Speed]:{e.Speed}");
138+
EventManager.Instance.Dispatch(sender, e);
139+
}
144140

145141
private void OnMultiDownloadCompleted(object sender, MultiDownloadCompletedEventArgs e)
146-
=> EventManager.Instance.Dispatch(sender, e);
142+
{
143+
var message = GetPacketHash(e.Version);
144+
GeneralTracer.Info($"Multi download completed, {message}[IsComplated]:{e.IsComplated}.");
145+
EventManager.Instance.Dispatch(sender, e);
146+
}
147147

148148
private void OnMultiDownloadError(object sender, MultiDownloadErrorEventArgs e)
149-
=> EventManager.Instance.Dispatch(sender, e);
149+
{
150+
var message = GetPacketHash(e.Version);
151+
GeneralTracer.Error($"Multi download error {message}.", e.Exception);
152+
EventManager.Instance.Dispatch(sender, e);
153+
}
150154

151155
private void OnMultiAllDownloadCompleted(object sender, MultiAllDownloadCompletedEventArgs e)
152156
{

src/c#/GeneralUpdate.Core/GeneralUpdateOSS.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Diagnostics;
33
using System.Text.Json;
44
using System.Threading.Tasks;
5+
using GeneralUpdate.Common.Internal;
56
using GeneralUpdate.Common.Internal.Bootstrap;
67
using GeneralUpdate.Common.Internal.JsonContext;
78
using GeneralUpdate.Common.Shared.Object;
@@ -43,7 +44,8 @@ private static async Task BaseStart()
4344
}
4445
catch (Exception exception)
4546
{
46-
throw new Exception(exception.Message + "\n" + exception.StackTrace);
47+
GeneralTracer.Error("The BaseStart method in the GeneralUpdateOSS class throws an exception.",exception);
48+
throw exception;
4749
}
4850
}
4951

0 commit comments

Comments
 (0)