MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4#pragma warning disable 1634, 16915#pragma warning disable 565066 7using System;8using System.Management.Automation;9using System.Runtime.Serialization;10using System.Reflection;11using System.Security.Permissions;12 13namespace Microsoft.PowerShell.Commands14{15 /// <summary>16 /// The exception that is thrown when there is no help found for a topic.17 /// </summary>18 public class HelpNotFoundException : SystemException, IContainsErrorRecord19 {20 /// <summary>21 /// Initializes a new instance of the HelpNotFoundException class with the give help topic.22 /// </summary>23 /// <param name="helpTopic">The help topic for which help is not found.</param>24 public HelpNotFoundException(string helpTopic)25 : base()26 {27 _helpTopic = helpTopic;28 CreateErrorRecord();29 }30 31 /// <summary>32 /// Initializes a new instance of the HelpNotFoundException class.33 /// </summary>34 public HelpNotFoundException()35 : base()36 {37 CreateErrorRecord();38 }39 40 /// <summary>41 /// Initializes a new instance of the HelpNotFoundException class with the given help topic42 /// and associated exception.43 /// </summary>44 /// <param name="helpTopic">The help topic for which help is not found.</param>45 /// <param name="innerException">The inner exception.</param>46 public HelpNotFoundException(string helpTopic, Exception innerException)47 : base(48 (innerException != null) ? innerException.Message : string.Empty,49 innerException)50 {51 _helpTopic = helpTopic;52 CreateErrorRecord();53 }54 55 /// <summary>56 /// Creates an internal error record based on helpTopic.57 /// The ErrorRecord created will be stored in the _errorRecord member.58 /// </summary>59 private void CreateErrorRecord()60 {61 string errMessage = string.Format(HelpErrors.HelpNotFound, _helpTopic);62 63 // Don't do ParentContainsErrorRecordException(this), as this causes recursion, and creates a64 // segmentation fault on Linux65 _errorRecord = new ErrorRecord(new ParentContainsErrorRecordException(errMessage), "HelpNotFound", ErrorCategory.ResourceUnavailable, null);66 _errorRecord.ErrorDetails = new ErrorDetails(typeof(HelpNotFoundException).Assembly, "HelpErrors", "HelpNotFound", _helpTopic);67 }68 69 private ErrorRecord _errorRecord;70 71 /// <summary>72 /// Gets ErrorRecord embedded in this exception.73 /// </summary>74 /// <value>ErrorRecord instance.</value>75 public ErrorRecord ErrorRecord76 {77 get78 {79 return _errorRecord;80 }81 }82 83 private readonly string _helpTopic = string.Empty;84 85 /// <summary>86 /// Gets help topic for which help is not found.87 /// </summary>88 /// <value>Help topic.</value>89 public string HelpTopic90 {91 get92 {93 return _helpTopic;94 }95 }96 97 /// <summary>98 /// Gets exception message for this exception.99 /// </summary>100 /// <value>Error message.</value>101 public override string Message102 {103 get104 {105 if (_errorRecord != null)106 {107 return _errorRecord.ToString();108 }109 110 return base.Message;111 }112 }113 114 #region Serialization115 116 /// <summary>117 /// Initializes a new instance of the HelpNotFoundException class.118 /// </summary>119 /// <param name="info">Serialization information.</param>120 /// <param name="context">Streaming context.</param>121 [Obsolete("Legacy serialization support is deprecated since .NET 8", DiagnosticId = "SYSLIB0051")] 122 protected HelpNotFoundException(SerializationInfo info,123 StreamingContext context)124 {125 throw new NotSupportedException();126 }127 128 #endregion Serialization129 }130}131 132#pragma warning restore 56506133 