winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

ImportModuleException.cs (1913B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="ImportModuleException.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.Management.Configuration.Processor.Exceptions
      8 {
      9     using System;
     10     using System.Management.Automation;
     11 
     12     /// <summary>
     13     /// Import-Module threw an exception.
     14     /// </summary>
     15     internal class ImportModuleException : Exception
     16     {
     17         /// <summary>
     18         /// Initializes a new instance of the <see cref="ImportModuleException"/> class.
     19         /// </summary>
     20         /// <param name="moduleName">Module name.</param>
     21         /// <param name="pwshEx">Inner exception.</param>
     22         public ImportModuleException(string? moduleName, Exception pwshEx)
     23             : base($"Could not import module: {moduleName?.ToString() ?? "<no module>"}", pwshEx)
     24         {
     25             this.HResult = this.GetHResult(pwshEx);
     26             this.ModuleName = moduleName;
     27         }
     28 
     29         /// <summary>
     30         /// Gets the module name.
     31         /// </summary>
     32         public string? ModuleName { get; }
     33 
     34         private int GetHResult(Exception pwshEx)
     35         {
     36             if (pwshEx.InnerException is not null)
     37             {
     38                 var scriptEx = pwshEx.InnerException as ScriptRequiresException;
     39                 if (scriptEx is not null)
     40                 {
     41                     if (scriptEx.ErrorRecord.CategoryInfo.Category == ErrorCategory.PermissionDenied)
     42                     {
     43                         return ErrorCodes.WinGetConfigUnitImportModuleAdmin;
     44                     }
     45                 }
     46             }
     47 
     48             return ErrorCodes.WinGetConfigUnitImportModule;
     49         }
     50     }
     51 }