Skip to content

Commit 5d325de

Browse files
committed
Added the ThrowIfNotInstalled() function to the WinGet class and used it in all methods that use a winget process except the CheckVersion function of the winget class and removed catching of Win32Exception because it is no longer needed
1 parent 2000594 commit 5d325de

4 files changed

Lines changed: 154 additions & 267 deletions

File tree

‎src/WGet.NET/Components/WinGet.cs‎

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// https://github.com/basicx-StrgV/ //
44
//--------------------------------------------------//
55
using System;
6-
using System.ComponentModel;
76
using System.Threading.Tasks;
87
using WGetNET.Models;
98
using WGetNET.Exceptions;
@@ -145,17 +144,15 @@ public bool ReQueryInstallation()
145144
/// </exception>
146145
public string ExportSettings()
147146
{
147+
ThrowIfNotInstalled();
148+
148149
try
149150
{
150151
ProcessResult result =
151152
_processManager.ExecuteWingetProcess(_exportSettingsCmd);
152153

153154
return ProcessOutputReader.ExportOutputToString(result);
154155
}
155-
catch (Win32Exception)
156-
{
157-
throw new WinGetNotInstalledException();
158-
}
159156
catch (Exception e)
160157
{
161158
throw new WinGetActionFailedException("Exporting sources failed.", _exportSettingsCmd, e);
@@ -178,17 +175,15 @@ public string ExportSettings()
178175
/// </exception>
179176
public async Task<string> ExportSettingsAsync()
180177
{
178+
ThrowIfNotInstalled();
179+
181180
try
182181
{
183182
ProcessResult result =
184183
await _processManager.ExecuteWingetProcessAsync(_exportSettingsCmd);
185184

186185
return ProcessOutputReader.ExportOutputToString(result);
187186
}
188-
catch (Win32Exception)
189-
{
190-
throw new WinGetNotInstalledException();
191-
}
192187
catch (Exception e)
193188
{
194189
throw new WinGetActionFailedException("Exporting sources failed.", _exportSettingsCmd, e);
@@ -242,16 +237,14 @@ public void ExportSettingsToFile(string file)
242237
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(file, "file");
243238
ArgsHelper.ThrowIfPathIsInvalid(file);
244239

240+
ThrowIfNotInstalled();
241+
245242
ProcessResult result;
246243

247244
try
248245
{
249246
result = _processManager.ExecuteWingetProcess(_exportSettingsCmd);
250247
}
251-
catch (Win32Exception)
252-
{
253-
throw new WinGetNotInstalledException();
254-
}
255248
catch (Exception e)
256249
{
257250
throw new WinGetActionFailedException("Exporting sources failed.", _exportSettingsCmd, e);
@@ -307,16 +300,14 @@ public async Task ExportSettingsToFileAsync(string file)
307300
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(file, "file");
308301
ArgsHelper.ThrowIfPathIsInvalid(file);
309302

303+
ThrowIfNotInstalled();
304+
310305
ProcessResult result;
311306

312307
try
313308
{
314309
result = await _processManager.ExecuteWingetProcessAsync(_exportSettingsCmd);
315310
}
316-
catch (Win32Exception)
317-
{
318-
throw new WinGetNotInstalledException();
319-
}
320311
catch (Exception e)
321312
{
322313
throw new WinGetActionFailedException("Exporting sources failed.", _exportSettingsCmd, e);
@@ -340,6 +331,8 @@ public async Task ExportSettingsToFileAsync(string file)
340331
/// </exception>
341332
public WinGetInfo GetInfo()
342333
{
334+
ThrowIfNotInstalled();
335+
343336
try
344337
{
345338
ProcessResult result =
@@ -361,10 +354,6 @@ public WinGetInfo GetInfo()
361354

362355
return ProcessOutputReader.ToWingetInfo(result.Output, actionVersionId);
363356
}
364-
catch (Win32Exception)
365-
{
366-
throw new WinGetNotInstalledException();
367-
}
368357
catch (Exception e)
369358
{
370359
throw new WinGetActionFailedException("Getting data failed.", _infoCmd, e);
@@ -387,6 +376,8 @@ public WinGetInfo GetInfo()
387376
/// </exception>
388377
public async Task<WinGetInfo> GetInfoAsync()
389378
{
379+
ThrowIfNotInstalled();
380+
390381
try
391382
{
392383
ProcessResult result =
@@ -408,10 +399,6 @@ public async Task<WinGetInfo> GetInfoAsync()
408399

409400
return ProcessOutputReader.ToWingetInfo(result.Output, actionVersionId);
410401
}
411-
catch (Win32Exception)
412-
{
413-
throw new WinGetNotInstalledException();
414-
}
415402
catch (Exception e)
416403
{
417404
throw new WinGetActionFailedException("Getting data failed.", _infoCmd, e);
@@ -442,6 +429,20 @@ protected bool CheckWinGetVersion(Version minVersion, Version? maxVersion = null
442429
return false;
443430
}
444431

432+
/// <summary>
433+
/// Throws a <see cref="WGetNET.Exceptions.WinGetNotInstalledException"/> if winget installation could not be found.
434+
/// </summary>
435+
/// <exception cref="WinGetNotInstalledException">
436+
/// WinGet is not installed or not found on the system.
437+
/// </exception>
438+
protected void ThrowIfNotInstalled()
439+
{
440+
if (!_isInstalled)
441+
{
442+
throw new WinGetNotInstalledException();
443+
}
444+
}
445+
445446
/// <summary>
446447
/// Checks the winget version and returns it as a <see cref="System.String"/>.
447448
/// </summary>

0 commit comments

Comments
 (0)