MegaBites-AI/Windows-powershell
0308
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Diagnostics.CodeAnalysis;5using System.Linq;6using System.Management.Automation.Language;7using System.Runtime.Serialization;8 9namespace System.Management.Automation10{11 /// <summary>12 /// Defines the exception thrown when a syntax error occurs while parsing PowerShell script text.13 /// </summary>14 public class ParseException : RuntimeException15 {16 private const string errorIdString = "Parse";17 18 private readonly ParseError[] _errors;19 20 /// <summary>21 /// The list of parser errors.22 /// </summary>23 public ParseError[] Errors24 {25 get { return _errors; }26 }27 28 #region Serialization29 /// <summary>30 /// Initializes a new instance of the ParseException class and defines the serialization information,31 /// and streaming context.32 /// </summary>33 /// <param name="info">The serialization information to use when initializing this object.</param>34 /// <param name="context">The streaming context to use when initializing this object.</param>35 /// <returns>Constructed object.</returns>36 [Obsolete("Legacy serialization support is deprecated since .NET 8", DiagnosticId = "SYSLIB0051")]37 protected ParseException(SerializationInfo info,38 StreamingContext context)39 {40 throw new NotSupportedException();41 }42 43 #endregion Serialization44 45 #region ctor46 47 /// <summary>48 /// Initializes a new instance of the class ParseException.49 /// </summary>50 /// <returns>Constructed object.</returns>51 public ParseException() : base()52 {53 base.SetErrorId(errorIdString);54 base.SetErrorCategory(ErrorCategory.ParserError);55 }56 57 /// <summary>58 /// Initializes a new instance of the ParseException class and defines the error message.59 /// </summary>60 /// <param name="message">The error message to use when initializing this object.</param>61 /// <returns>Constructed object.</returns>62 public ParseException(string message) : base(message)63 {64 base.SetErrorId(errorIdString);65 base.SetErrorCategory(ErrorCategory.ParserError);66 }67 68 /// <summary>69 /// Initializes a new instance of the ParseException class and defines the error message and70 /// errorID.71 /// </summary>72 /// <param name="message">The error message to use when initializing this object.</param>73 /// <param name="errorId">The errorId to use when initializing this object.</param>74 /// <returns>Constructed object.</returns>75 internal ParseException(string message, string errorId) : base(message)76 {77 base.SetErrorId(errorId);78 base.SetErrorCategory(ErrorCategory.ParserError);79 }80 81 /// <summary>82 /// Initializes a new instance of the ParseException class and defines the error message,83 /// error ID and inner exception.84 /// </summary>85 /// <param name="message">The error message to use when initializing this object.</param>86 /// <param name="errorId">The errorId to use when initializing this object.</param>87 /// <param name="innerException">The inner exception to use when initializing this object.</param>88 /// <returns>Constructed object.</returns>89 internal ParseException(string message, string errorId, Exception innerException)90 : base(message, innerException)91 {92 base.SetErrorId(errorId);93 base.SetErrorCategory(ErrorCategory.ParserError);94 }95 96 /// <summary>97 /// Initializes a new instance of the ParseException class and defines the error message and98 /// inner exception.99 /// </summary>100 /// <param name="message">The error message to use when initializing this object.</param>101 /// <param name="innerException">The inner exception to use when initializing this object.</param>102 /// <returns>Constructed object.</returns>103 public ParseException(string message,104 Exception innerException)105 : base(message, innerException)106 {107 base.SetErrorId(errorIdString);108 base.SetErrorCategory(ErrorCategory.ParserError);109 }110 111 /// <summary>112 /// Initializes a new instance of the ParseException class with a collection of error messages.113 /// </summary>114 /// <param name="errors">The collection of error messages.</param>115 public ParseException(ParseError[] errors)116 {117 if (errors is null || errors.Length == 0)118 {119 throw new ArgumentNullException(nameof(errors));120 }121 122 _errors = errors;123 // Arbitrarily choose the first error message for the ErrorId.124 base.SetErrorId(_errors[0].ErrorId);125 base.SetErrorCategory(ErrorCategory.ParserError);126 127 if (errors[0].Extent != null)128 this.ErrorRecord.SetInvocationInfo(new InvocationInfo(null, errors[0].Extent));129 }130 131 #endregion ctor132 133 /// <summary>134 /// The error message to display.135 /// </summary>136 public override string Message137 {138 get139 {140 if (_errors == null)141 {142 return base.Message;143 }144 145 // Report at most the first 10 errors146 var errorsToReport = (_errors.Length > 10)147 ? _errors.Take(10).Select(static e => e.ToString()).Append(ParserStrings.TooManyErrors)148 : _errors.Select(static e => e.ToString());149 150 return string.Join(Environment.NewLine + Environment.NewLine, errorsToReport);151 }152 }153 }154 155 /// <summary>156 /// Defines the exception thrown when a incomplete parse error occurs while parsing PowerShell script text.157 /// </summary>158 /// <remarks>159 /// This is a variation on a parsing error that indicates that the parse was incomplete160 /// rather than irrecoverably wrong. A host can catch this exception and then prompt for additional161 /// input to complete the parse.162 /// </remarks>163 public class IncompleteParseException164 : ParseException165 {166 #region private167 private const string errorIdString = "IncompleteParse";168 #endregion169 170 #region ctor171 172 #region Serialization173 /// <summary>174 /// Initializes a new instance of the IncompleteParseException class and defines the serialization information,175 /// and streaming context.176 /// </summary>177 /// <param name="info">The serialization information to use when initializing this object.</param>178 /// <param name="context">The streaming context to use when initializing this object.</param>179 /// <returns>Constructed object.</returns>180 [Obsolete("Legacy serialization support is deprecated since .NET 8", DiagnosticId = "SYSLIB0051")]181 protected IncompleteParseException(SerializationInfo info,182 StreamingContext context)183 {184 throw new NotSupportedException();185 }186 #endregion Serialization187 188 /// <summary>189 /// Initializes a new instance of the class IncompleteParseException.190 /// </summary>191 /// <returns>Constructed object.</returns>192 public IncompleteParseException() : base()193 {194 // Error category is set in base constructor195 base.SetErrorId(errorIdString);196 }197 198 /// <summary>199 /// Initializes a new instance of the IncompleteParseException class and defines the error message.200 /// </summary>201 /// <param name="message">The error message to use when initializing this object.</param>202 /// <returns>Constructed object.</returns>203 public IncompleteParseException(string message) : base(message)204 {205 // Error category is set in base constructor206 base.SetErrorId(errorIdString);207 }208 209 /// <summary>210 /// Initializes a new instance of the IncompleteParseException class and defines the error message and211 /// errorID.212 /// </summary>213 /// <param name="message">The error message to use when initializing this object.</param>214 /// <param name="errorId">The errorId to use when initializing this object.</param>215 /// <returns>Constructed object.</returns>216 internal IncompleteParseException(string message, string errorId) : base(message, errorId)217 {218 // Error category is set in base constructor219 }220 221 /// <summary>222 /// Initializes a new instance of the IncompleteParseException class and defines the error message,223 /// error ID and inner exception.224 /// </summary>225 /// <param name="message">The error message to use when initializing this object.</param>226 /// <param name="errorId">The errorId to use when initializing this object.</param>227 /// <param name="innerException">The inner exception to use when initializing this object.</param>228 /// <returns>Constructed object.</returns>229 internal IncompleteParseException(string message, string errorId, Exception innerException)230 : base(message, errorId, innerException)231 {232 // Error category is set in base constructor233 }234 235 /// <summary>236 /// Initializes a new instance of the IncompleteParseException class and defines the error message and237 /// inner exception.238 /// </summary>239 /// <param name="message">The error message to use when initializing this object.</param>240 /// <param name="innerException">The inner exception to use when initializing this object.</param>241 /// <returns>Constructed object.</returns>242 public IncompleteParseException(string message,243 Exception innerException)244 : base(message, innerException)245 {246 // Error category is set in base constructor247 base.SetErrorId(errorIdString);248 }249 #endregion ctor250 }251}252 