OpenConfigurationSetException.cs (2912B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="OpenConfigurationSetException.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.WinGet.Configuration.Engine.Exceptions 8 { 9 using System; 10 using System.Text; 11 using Microsoft.Management.Configuration; 12 using Microsoft.WinGet.Resources; 13 14 /// <summary> 15 /// Exception thrown when failed to open a configuration set. 16 /// </summary> 17 public class OpenConfigurationSetException : Exception 18 { 19 /// <summary> 20 /// Initializes a new instance of the <see cref="OpenConfigurationSetException"/> class. 21 /// </summary> 22 /// <param name="openResult">Open Result.</param> 23 /// <param name="configurationFile">Configuration file.</param> 24 internal OpenConfigurationSetException(OpenConfigurationSetResult openResult, string configurationFile) 25 : base(GetMessage(openResult, configurationFile)) 26 { 27 } 28 29 private static string GetMessage(OpenConfigurationSetResult openResult, string configurationFile) 30 { 31 var sb = new StringBuilder(); 32 sb.Append($"Failed to open configuration set at {configurationFile} with error 0x{openResult.ResultCode.HResult:X} "); 33 34 switch (openResult.ResultCode.HResult) 35 { 36 case ErrorCodes.WingetConfigErrorInvalidFieldType: 37 sb.Append(string.Format(Resources.ConfigurationFieldInvalidType, openResult.Field)); 38 break; 39 case ErrorCodes.WingetConfigErrorInvalidFieldValue: 40 sb.Append(string.Format(Resources.ConfigurationFieldInvalidValue, openResult.Field, openResult.Value)); 41 break; 42 case ErrorCodes.WingetConfigErrorMissingField: 43 sb.Append(string.Format(Resources.ConfigurationFieldMissing, openResult.Field)); 44 break; 45 case ErrorCodes.WingetConfigErrorUnknownConfigurationFileVersion: 46 sb.Append(string.Format(Resources.ConfigurationFileVersionUnknown, openResult.Value)); 47 break; 48 case ErrorCodes.WingetConfigErrorInvalidConfigurationFile: 49 case ErrorCodes.WingetConfigErrorInvalidYaml: 50 default: 51 sb.Append(Resources.ConfigurationFileInvalid); 52 break; 53 } 54 55 if (openResult.Line != 0) 56 { 57 sb.Append($" {string.Format(Resources.SeeLineAndColumn, openResult.Line, openResult.Column)}"); 58 } 59 60 return sb.ToString(); 61 } 62 } 63 }