Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
RuntimeException.cs300 linesDownload Raw Back to utils
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Management.Automation.Language;5using System.Runtime.Serialization;6 7namespace System.Management.Automation8{9    /// <summary>10    /// RuntimeException is the base class for exceptions likely to occur11    /// while a PowerShell command is running.12    /// </summary>13    /// <remarks>14    /// PowerShell scripts can trap RuntimeException using the15    /// "trap (exceptionclass) {handler}" script construct.16    ///17    /// Instances of this exception class are usually generated by the18    /// PowerShell Engine.  It is unusual for code outside the PowerShell Engine19    /// to create an instance of this class.20    /// </remarks>21    public class RuntimeException22            : SystemException, IContainsErrorRecord23    {24        #region ctor25        /// <summary>26        /// Initializes a new instance of the RuntimeException class.27        /// </summary>28        /// <returns>Constructed object.</returns>29        public RuntimeException()30            : base()31        {32        }33 34        #region Serialization35        /// <summary>36        /// Initializes a new instance of the RuntimeException class37        /// using data serialized via38        /// <see cref="ISerializable"/>39        /// </summary>40        /// <param name="info">Serialization information.</param>41        /// <param name="context">Streaming context.</param>42        /// <returns>Constructed object.</returns>43        [Obsolete("Legacy serialization support is deprecated since .NET 8", DiagnosticId = "SYSLIB0051")]44        protected RuntimeException(SerializationInfo info,45                           StreamingContext context)46        {47            throw new NotSupportedException();48        }49        #endregion Serialization50 51        /// <summary>52        /// Initializes a new instance of the RuntimeException class.53        /// </summary>54        /// <param name="message"></param>55        /// <returns>Constructed object.</returns>56        public RuntimeException(string message)57            : base(message)58        {59        }60 61        /// <summary>62        /// Initializes a new instance of the RuntimeException class.63        /// </summary>64        /// <param name="message"></param>65        /// <param name="innerException"></param>66        /// <returns>Constructed object.</returns>67        public RuntimeException(string message,68                                Exception innerException)69                : base(message, innerException)70        {71        }72 73        /// <summary>74        /// Initializes a new instance of the RuntimeException class75        /// starting with an already populated error record.76        /// </summary>77        /// <param name="message"></param>78        /// <param name="innerException"></param>79        /// <param name="errorRecord"></param>80        /// <returns>Constructed object.</returns>81        public RuntimeException(string message,82            Exception innerException,83            ErrorRecord errorRecord)84                : base(message, innerException)85        {86            _errorRecord = errorRecord;87        }88 89        internal RuntimeException(ErrorCategory errorCategory,90            InvocationInfo invocationInfo,91            IScriptExtent errorPosition,92            string errorIdAndResourceId,93            string message,94            Exception innerException)95            : base(message, innerException)96        {97            SetErrorCategory(errorCategory);98            SetErrorId(errorIdAndResourceId);99 100            if ((errorPosition == null) && (invocationInfo != null))101            {102                errorPosition = invocationInfo.ScriptPosition;103            }104 105            if (invocationInfo == null)106            {107                return;108            }109 110            _errorRecord = new ErrorRecord(111              new ParentContainsErrorRecordException(this),112                _errorId,113                _errorCategory,114                _targetObject);115            _errorRecord.SetInvocationInfo(new InvocationInfo(invocationInfo.MyCommand, errorPosition));116        }117 118        #endregion ctor119 120        #region ErrorRecord121        // If RuntimeException subclasses need to do more than change122        // the ErrorId, ErrorCategory and TargetObject, they can access123        // the ErrorRecord property and make changes directly.  However,124        // not that calling SetErrorId, SetErrorCategory or SetTargetObject125        // will clean the cached ErrorRecord and erase any other changes,126        // so the ErrorId etc. should be set first.127        /// <summary>128        /// Additional information about the error.129        /// </summary>130        /// <value></value>131        /// <remarks>132        /// Note that ErrorRecord.Exception is133        /// <see cref="System.Management.Automation.ParentContainsErrorRecordException"/>.134        /// </remarks>135        public virtual ErrorRecord ErrorRecord136        {137            get138            {139                _errorRecord ??= new ErrorRecord(140                    new ParentContainsErrorRecordException(this),141                    _errorId,142                    _errorCategory,143                    _targetObject);144 145                return _errorRecord;146            }147        }148 149        private ErrorRecord _errorRecord;150        private string _errorId = "RuntimeException";151        private ErrorCategory _errorCategory = ErrorCategory.NotSpecified;152        private object _targetObject = null;153 154        /// <summary>155        /// Subclasses can use this method to set the ErrorId.156        /// Note that this will clear the cached ErrorRecord, so be sure157        /// to change this before writing to ErrorRecord.ErrorDetails158        /// or the like.159        /// </summary>160        /// <param name="errorId">Per ErrorRecord constructors.</param>161        internal void SetErrorId(string errorId)162        {163            if (_errorId != errorId)164            {165                _errorId = errorId;166                _errorRecord = null;167            }168        }169 170        /// <summary>171        /// Subclasses can use this method to set the ErrorCategory.172        /// Note that this will clear the cached ErrorRecord, so be sure173        /// to change this before writing to ErrorRecord.ErrorDetails174        /// or the like.175        /// </summary>176        /// <param name="errorCategory">177        /// per ErrorRecord.CategoryInfo.Category178        /// </param>179        internal void SetErrorCategory(ErrorCategory errorCategory)180        {181            if (_errorCategory != errorCategory)182            {183                _errorCategory = errorCategory;184                _errorRecord = null;185            }186        }187 188        /// <summary>189        /// Subclasses can use this method to set or update the TargetObject.190        /// This convenience function doesn't clobber the error record if it191        /// already exists...192        /// </summary>193        /// <param name="targetObject">194        /// per ErrorRecord.TargetObject195        /// </param>196        internal void SetTargetObject(object targetObject)197        {198            _targetObject = targetObject;199            _errorRecord?.SetTargetObject(targetObject);200        }201        #endregion ErrorRecord202 203        #region Internal204        internal static string RetrieveMessage(ErrorRecord errorRecord)205        {206            if (errorRecord == null)207                return string.Empty;208            if (errorRecord.ErrorDetails != null &&209                !string.IsNullOrEmpty(errorRecord.ErrorDetails.Message))210            {211                return errorRecord.ErrorDetails.Message;212            }213 214            if (errorRecord.Exception == null)215                return string.Empty;216            return errorRecord.Exception.Message;217        }218 219        internal static string RetrieveMessage(Exception e)220        {221            if (e == null)222                return string.Empty;223 224            if (e is not IContainsErrorRecord icer)225                return e.Message;226            ErrorRecord er = icer.ErrorRecord;227            if (er == null)228                return e.Message;229            ErrorDetails ed = er.ErrorDetails;230            if (ed == null)231                return e.Message;232            string detailsMessage = ed.Message;233            return (string.IsNullOrEmpty(detailsMessage)) ? e.Message : detailsMessage;234        }235 236        internal static Exception RetrieveException(ErrorRecord errorRecord)237        {238            if (errorRecord == null)239                return null;240            return errorRecord.Exception;241        }242 243        /// <summary>244        /// </summary>245        public bool WasThrownFromThrowStatement246        {247            get248            {249                return _thrownByThrowStatement;250            }251 252            set253            {254                _thrownByThrowStatement = value;255                if (_errorRecord?.Exception is RuntimeException exception)256                {257                    exception.WasThrownFromThrowStatement = value;258                }259            }260        }261 262        private bool _thrownByThrowStatement;263 264        internal bool WasRethrown { get; set; }265 266        /// <summary>267        /// Fix for BUG: Windows Out Of Band Releases: 906263 and 906264268        /// The interpreter prompt CommandBaseStrings:InquireHalt269        /// should be suppressed when this flag is set.  This will be set270        /// when this prompt has already occurred and Break was chosen,271        /// or for ActionPreferenceStopException in all cases.272        /// </summary>273        internal bool SuppressPromptInInterpreter274        {275            get { return _suppressPromptInInterpreter; }276 277            set { _suppressPromptInInterpreter = value; }278        }279 280        private bool _suppressPromptInInterpreter;281 282        #endregion Internal283 284        private Token _errorToken;285 286        internal Token ErrorToken287        {288            get289            {290                return _errorToken;291            }292 293            set294            {295                _errorToken = value;296            }297        }298    }299}300