PageRenderTime 62ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Microsoft.PowerShell.LocalAccounts/LocalAccounts/Exceptions.cs

https://gitlab.com/unofficial-mirrors/PowerShell
C# | 688 lines | 270 code | 47 blank | 371 comment | 2 complexity | 531747fb9b05c0669cade59fb4881e3d MD5 | raw file
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License.
  3. using System;
  4. using System.Management.Automation;
  5. using System.Management.Automation.SecurityAccountsManager;
  6. using System.Runtime.Serialization;
  7. using Microsoft.PowerShell.LocalAccounts;
  8. namespace Microsoft.PowerShell.Commands
  9. {
  10. /// <summary>
  11. /// Base class for cmdlet-specific exceptions.
  12. /// </summary>
  13. public class LocalAccountsException : Exception
  14. {
  15. #region Public Properties
  16. /// <summary>
  17. /// Gets the <see cref="System.Management.Automation.ErrorCategory"/>
  18. /// value for this exception.
  19. /// </summary>
  20. public ErrorCategory ErrorCategory
  21. {
  22. get;
  23. private set;
  24. }
  25. /// <summary>
  26. /// Gets the target object for this exception. This is used as
  27. /// the TargetObject member of a PowerShell
  28. /// <see cref="System.Management.Automation.ErrorRecord"/> object.
  29. /// </summary>
  30. public object Target
  31. {
  32. get;
  33. private set;
  34. }
  35. /// <summary>
  36. /// Gets the error name. This is used as the ErrorId parameter when
  37. /// constructing a PowerShell <see cref="System.Management.Automation.ErrorRecord"/>
  38. /// oject.
  39. /// </summary>
  40. public string ErrorName
  41. {
  42. get
  43. {
  44. string exname = "Exception";
  45. var exlen = exname.Length;
  46. var name = this.GetType().Name;
  47. if (name.EndsWith(exname, StringComparison.OrdinalIgnoreCase) && name.Length > exlen)
  48. name = name.Substring(0, name.Length - exlen);
  49. return name;
  50. }
  51. }
  52. #endregion Public Properties
  53. internal LocalAccountsException(string message, object target, ErrorCategory errorCategory)
  54. : base(message)
  55. {
  56. ErrorCategory = errorCategory;
  57. Target = target;
  58. }
  59. /// <summary>
  60. /// Compliance Constructor
  61. /// </summary>
  62. public LocalAccountsException() : base() { }
  63. /// <summary>
  64. /// Compliance Constructor
  65. /// </summary>
  66. /// <param name="message"></param>
  67. public LocalAccountsException(String message) : base(message) { }
  68. /// <summary>
  69. /// Compliance Constructor
  70. /// </summary>
  71. /// <param name="message"></param>
  72. /// <param name="ex"></param>
  73. public LocalAccountsException(String message, Exception ex) : base(message, ex) { }
  74. /// <summary>
  75. /// Compliance Constructor
  76. /// </summary>
  77. /// <param name="info"></param>
  78. /// <param name="ctx"></param>
  79. protected LocalAccountsException(SerializationInfo info, StreamingContext ctx) : base(info, ctx) { }
  80. }
  81. /// <summary>
  82. /// Exception indicating an error occurred during one of the internal
  83. /// operations such as opening or closing a handle.
  84. /// </summary>
  85. public class InternalException : LocalAccountsException
  86. {
  87. #region Public Properties
  88. /// <summary>
  89. /// Gets the NTSTATUS code for this exception.
  90. /// </summary>
  91. public UInt32 StatusCode
  92. {
  93. get;
  94. private set;
  95. }
  96. #endregion Public Properties
  97. internal InternalException(UInt32 ntStatus,
  98. string message,
  99. object target,
  100. ErrorCategory errorCategory = ErrorCategory.NotSpecified)
  101. : base(message, target, errorCategory)
  102. {
  103. StatusCode = ntStatus;
  104. }
  105. internal InternalException(UInt32 ntStatus,
  106. object target,
  107. ErrorCategory errorCategory = ErrorCategory.NotSpecified)
  108. : this(ntStatus,
  109. StringUtil.Format(Strings.UnspecifiedErrorNtStatus, ntStatus),
  110. target,
  111. errorCategory)
  112. {
  113. }
  114. /// <summary>
  115. /// Compliance Constructor
  116. /// </summary>
  117. public InternalException() : base() { }
  118. /// <summary>
  119. /// Compliance Constructor
  120. /// </summary>
  121. /// <param name="message"></param>
  122. public InternalException(String message) : base(message) { }
  123. /// <summary>
  124. /// Compliance Constructor
  125. /// </summary>
  126. /// <param name="message"></param>
  127. /// <param name="ex"></param>
  128. public InternalException(String message, Exception ex) : base(message, ex) { }
  129. /// <summary>
  130. /// Compliance Constructor
  131. /// </summary>
  132. /// <param name="info"></param>
  133. /// <param name="ctx"></param>
  134. protected InternalException(SerializationInfo info, StreamingContext ctx) : base(info, ctx) { }
  135. }
  136. /// <summary>
  137. /// Exception indicating an error occurred when a native function
  138. /// is called that returns a Win32 error code as opposed to an
  139. /// NT Status code.
  140. /// </summary>
  141. public class Win32InternalException : LocalAccountsException
  142. {
  143. #region Public Properties
  144. /// <summary>
  145. /// The Win32 error code for this exception.
  146. /// </summary>
  147. public int NativeErrorCode
  148. {
  149. get;
  150. private set;
  151. }
  152. #endregion Public Properties
  153. internal Win32InternalException(int errorCode,
  154. string message,
  155. object target,
  156. ErrorCategory errorCategory = ErrorCategory.NotSpecified)
  157. : base(message, target, errorCategory)
  158. {
  159. NativeErrorCode = errorCode;
  160. }
  161. internal Win32InternalException(int errorCode,
  162. object target,
  163. ErrorCategory errorCategory = ErrorCategory.NotSpecified)
  164. : this(errorCode,
  165. StringUtil.Format(Strings.UnspecifiedErrorWin32Error, errorCode),
  166. target,
  167. errorCategory)
  168. {
  169. }
  170. /// <summary>
  171. /// Compliance Constructor
  172. /// </summary>
  173. public Win32InternalException() : base() {}
  174. /// <summary>
  175. /// Compliance Constructor
  176. /// </summary>
  177. /// <param name="message"></param>
  178. public Win32InternalException(String message) : base(message) { }
  179. /// <summary>
  180. /// Compliance Constructor
  181. /// </summary>
  182. /// <param name="message"></param>
  183. /// <param name="ex"></param>
  184. public Win32InternalException(String message, Exception ex) : base(message, ex) { }
  185. /// <summary>
  186. /// Compliance Constructor
  187. /// </summary>
  188. /// <param name="info"></param>
  189. /// <param name="ctx"></param>
  190. protected Win32InternalException(SerializationInfo info, StreamingContext ctx) : base(info, ctx) { }
  191. }
  192. /// <summary>
  193. /// Exception indicating an invalid password.
  194. /// </summary>
  195. public class InvalidPasswordException : LocalAccountsException
  196. {
  197. /// <summary>
  198. /// Generates with a default invalid password message.
  199. /// </summary>
  200. public InvalidPasswordException()
  201. : base(Strings.InvalidPassword, null, ErrorCategory.InvalidArgument)
  202. {
  203. }
  204. /// <summary>
  205. /// Generates the exception with the specified message.
  206. /// </summary>
  207. /// <param name="message"></param>
  208. public InvalidPasswordException(string message)
  209. : base(message, null, ErrorCategory.InvalidArgument)
  210. {
  211. }
  212. /// <summary>
  213. /// Creates a message from the specified error code.
  214. /// </summary>
  215. /// <param name="errorCode"></param>
  216. public InvalidPasswordException(uint errorCode)
  217. : base(StringUtil.GetSystemMessage(errorCode), null, ErrorCategory.InvalidArgument)
  218. {
  219. }
  220. /// <summary>
  221. /// Compliance Constructor
  222. /// </summary>
  223. /// <param name="message"></param>
  224. /// <param name="ex"></param>
  225. public InvalidPasswordException(String message, Exception ex) : base(message, ex) { }
  226. /// <summary>
  227. /// Compliance Constructor
  228. /// </summary>
  229. /// <param name="info"></param>
  230. /// <param name="ctx"></param>
  231. protected InvalidPasswordException(SerializationInfo info, StreamingContext ctx) : base(info, ctx) { }
  232. }
  233. /// <summary>
  234. /// Exception thrown when invalid parameter pairing is detected.
  235. /// </summary>
  236. public class InvalidParametersException : LocalAccountsException
  237. {
  238. /// <summary>
  239. /// Creates InvalidParametersException using the specified message.
  240. /// </summary>
  241. /// <param name="message"></param>
  242. public InvalidParametersException(string message)
  243. : base(message, null, ErrorCategory.InvalidArgument)
  244. {
  245. }
  246. internal InvalidParametersException(string parameterA, string parameterB)
  247. : this(StringUtil.Format(Strings.InvalidParameterPair, parameterA, parameterB))
  248. {
  249. }
  250. /// <summary>
  251. /// Compliance Constructor
  252. /// </summary>
  253. public InvalidParametersException() : base() { }
  254. /// <summary>
  255. /// Compliance Constructor
  256. /// </summary>
  257. /// <param name="message"></param>
  258. /// <param name="ex"></param>
  259. public InvalidParametersException(String message, Exception ex) : base(message, ex) { }
  260. /// <summary>
  261. /// Compliance Constructor
  262. /// </summary>
  263. /// <param name="info"></param>
  264. /// <param name="ctx"></param>
  265. protected InvalidParametersException(SerializationInfo info, StreamingContext ctx) : base(info, ctx) { }
  266. }
  267. /// <summary>
  268. /// Exception indicating permission denied.
  269. /// </summary>
  270. public class AccessDeniedException : LocalAccountsException
  271. {
  272. internal AccessDeniedException(object target)
  273. : base(Strings.AccessDenied, target, ErrorCategory.PermissionDenied)
  274. {
  275. }
  276. /// <summary>
  277. /// Compliance Constructor
  278. /// </summary>
  279. public AccessDeniedException() : base() { }
  280. /// <summary>
  281. /// Compliance Constructor
  282. /// </summary>
  283. /// <param name="message"></param>
  284. public AccessDeniedException(String message) : base(message) { }
  285. /// <summary>
  286. /// Compliance Constructor
  287. /// </summary>
  288. /// <param name="message"></param>
  289. /// <param name="ex"></param>
  290. public AccessDeniedException(String message, Exception ex) : base(message, ex) { }
  291. /// <summary>
  292. /// Compliance Constructor
  293. /// </summary>
  294. /// <param name="info"></param>
  295. /// <param name="ctx"></param>
  296. protected AccessDeniedException(SerializationInfo info, StreamingContext ctx) : base(info, ctx) { }
  297. }
  298. /// <summary>
  299. /// Exception indicating that the name of a user or group is invalid.
  300. /// </summary>
  301. public class InvalidNameException : LocalAccountsException
  302. {
  303. internal InvalidNameException(string name, object target)
  304. : base(StringUtil.Format(Strings.InvalidName, name), target, ErrorCategory.InvalidArgument)
  305. {
  306. }
  307. /// <summary>
  308. /// Compliance Constructor
  309. /// </summary>
  310. public InvalidNameException() : base() { }
  311. /// <summary>
  312. /// Compliance Constructor
  313. /// </summary>
  314. /// <param name="message"></param>
  315. public InvalidNameException(String message) : base(message) { }
  316. /// <summary>
  317. /// Compliance Constructor
  318. /// </summary>
  319. /// <param name="message"></param>
  320. /// <param name="ex"></param>
  321. public InvalidNameException(String message, Exception ex) : base(message, ex) { }
  322. /// <summary>
  323. /// Compliance Constructor
  324. /// </summary>
  325. /// <param name="info"></param>
  326. /// <param name="ctx"></param>
  327. protected InvalidNameException(SerializationInfo info, StreamingContext ctx) : base(info, ctx) { }
  328. }
  329. /// <summary>
  330. /// Exception indicating that the specified name is already in use.
  331. /// </summary>
  332. public class NameInUseException : LocalAccountsException
  333. {
  334. internal NameInUseException(string name, object target)
  335. : base(StringUtil.Format(Strings.NameInUse, name), target, ErrorCategory.InvalidArgument)
  336. {
  337. }
  338. /// <summary>
  339. /// Compliance Constructor
  340. /// </summary>
  341. public NameInUseException() : base() { }
  342. /// <summary>
  343. /// Compliance Constructor
  344. /// </summary>
  345. /// <param name="message"></param>
  346. public NameInUseException(String message) : base(message) { }
  347. /// <summary>
  348. /// Compliance Constructor
  349. /// </summary>
  350. /// <param name="message"></param>
  351. /// <param name="ex"></param>
  352. public NameInUseException(String message, Exception ex) : base(message, ex) { }
  353. /// <summary>
  354. /// Compliance Constructor
  355. /// </summary>
  356. /// <param name="info"></param>
  357. /// <param name="ctx"></param>
  358. protected NameInUseException(SerializationInfo info, StreamingContext ctx) : base(info, ctx) { }
  359. }
  360. /// <summary>
  361. /// Exception indicating that an entity of some kind was not found.
  362. /// Also serves as a base class for more specific object-not-found errors.
  363. /// </summary>
  364. public class NotFoundException : LocalAccountsException
  365. {
  366. internal NotFoundException(string message, object target)
  367. : base(message, target, ErrorCategory.ObjectNotFound)
  368. {
  369. }
  370. /// <summary>
  371. /// Compliance Constructor
  372. /// </summary>
  373. public NotFoundException() : base() { }
  374. /// <summary>
  375. /// Compliance Constructor
  376. /// </summary>
  377. /// <param name="message"></param>
  378. public NotFoundException(String message) : base(message) { }
  379. /// <summary>
  380. /// Compliance Constructor
  381. /// </summary>
  382. /// <param name="message"></param>
  383. /// <param name="ex"></param>
  384. public NotFoundException(String message, Exception ex) : base(message, ex) { }
  385. /// <summary>
  386. /// Compliance Constructor
  387. /// </summary>
  388. /// <param name="info"></param>
  389. /// <param name="ctx"></param>
  390. protected NotFoundException(SerializationInfo info, StreamingContext ctx) : base(info, ctx) { }
  391. }
  392. /// <summary>
  393. /// Exception indicating that a principal was not Found
  394. /// </summary>
  395. public class PrincipalNotFoundException : NotFoundException
  396. {
  397. internal PrincipalNotFoundException(string principal, object target)
  398. : base(StringUtil.Format(Strings.PrincipalNotFound, principal), target)
  399. {
  400. }
  401. /// <summary>
  402. /// Compliance Constructor
  403. /// </summary>
  404. public PrincipalNotFoundException() : base() { }
  405. /// <summary>
  406. /// Compliance Constructor
  407. /// </summary>
  408. /// <param name="message"></param>
  409. public PrincipalNotFoundException(String message) : base(message) { }
  410. /// <summary>
  411. /// Compliance Constructor
  412. /// </summary>
  413. /// <param name="message"></param>
  414. /// <param name="ex"></param>
  415. public PrincipalNotFoundException(String message, Exception ex) : base(message, ex) { }
  416. /// <summary>
  417. /// Compliance Constructor
  418. /// </summary>
  419. /// <param name="info"></param>
  420. /// <param name="ctx"></param>
  421. protected PrincipalNotFoundException(SerializationInfo info, StreamingContext ctx) : base(info, ctx) { }
  422. }
  423. /// <summary>
  424. /// Exception indicating that a group was not found.
  425. /// </summary>
  426. public class GroupNotFoundException : NotFoundException
  427. {
  428. internal GroupNotFoundException(string group, object target)
  429. : base(StringUtil.Format(Strings.GroupNotFound, group), target)
  430. {
  431. }
  432. /// <summary>
  433. /// Compliance Constructor
  434. /// </summary>
  435. public GroupNotFoundException() : base() { }
  436. /// <summary>
  437. /// Compliance Constructor
  438. /// </summary>
  439. /// <param name="message"></param>
  440. public GroupNotFoundException(String message) : base(message) { }
  441. /// <summary>
  442. /// Compliance Constructor
  443. /// </summary>
  444. /// <param name="message"></param>
  445. /// <param name="ex"></param>
  446. public GroupNotFoundException(String message, Exception ex) : base(message, ex) { }
  447. /// <summary>
  448. /// Compliance Constructor
  449. /// </summary>
  450. /// <param name="info"></param>
  451. /// <param name="ctx"></param>
  452. protected GroupNotFoundException(SerializationInfo info, StreamingContext ctx) : base(info, ctx) { }
  453. }
  454. /// <summary>
  455. /// Exception indicating that a user was not found.
  456. /// </summary>
  457. public class UserNotFoundException : NotFoundException
  458. {
  459. internal UserNotFoundException(string user, object target)
  460. : base(StringUtil.Format(Strings.UserNotFound, user), target)
  461. {
  462. }
  463. /// <summary>
  464. /// Compliance Constructor
  465. /// </summary>
  466. public UserNotFoundException() : base() { }
  467. /// <summary>
  468. /// Compliance Constructor
  469. /// </summary>
  470. /// <param name="message"></param>
  471. public UserNotFoundException(String message) : base(message) { }
  472. /// <summary>
  473. /// Compliance Constructor
  474. /// </summary>
  475. /// <param name="message"></param>
  476. /// <param name="ex"></param>
  477. public UserNotFoundException(String message, Exception ex) : base(message, ex) { }
  478. /// <summary>
  479. /// Compliance Constructor
  480. /// </summary>
  481. /// <param name="info"></param>
  482. /// <param name="ctx"></param>
  483. protected UserNotFoundException(SerializationInfo info, StreamingContext ctx) : base(info, ctx) { }
  484. }
  485. /// <summary>
  486. /// Exception indicating that a group member was not found.
  487. /// </summary>
  488. public class MemberNotFoundException : NotFoundException
  489. {
  490. internal MemberNotFoundException(string member, string group)
  491. : base(StringUtil.Format(Strings.MemberNotFound, member, group), member)
  492. {
  493. }
  494. /// <summary>
  495. /// Compliance Constructor
  496. /// </summary>
  497. public MemberNotFoundException() : base() { }
  498. /// <summary>
  499. /// Compliance Constructor
  500. /// </summary>
  501. /// <param name="message"></param>
  502. public MemberNotFoundException(String message) : base(message) { }
  503. /// <summary>
  504. /// Compliance Constructor
  505. /// </summary>
  506. /// <param name="message"></param>
  507. /// <param name="ex"></param>
  508. public MemberNotFoundException(String message, Exception ex) : base(message, ex) { }
  509. /// <summary>
  510. /// Compliance Constructor
  511. /// </summary>
  512. /// <param name="info"></param>
  513. /// <param name="ctx"></param>
  514. protected MemberNotFoundException(SerializationInfo info, StreamingContext ctx) : base(info, ctx) { }
  515. }
  516. /// <summary>
  517. /// Exception indicating that an entity of some kind already exists.
  518. /// Also serves as a base class for more specific object-exists errors.
  519. /// </summary>
  520. public class ObjectExistsException : LocalAccountsException
  521. {
  522. internal ObjectExistsException(string message, object target)
  523. : base(message, target, ErrorCategory.ResourceExists)
  524. {
  525. }
  526. /// <summary>
  527. /// Compliance Constructor
  528. /// </summary>
  529. public ObjectExistsException() : base() { }
  530. /// <summary>
  531. /// Compliance Constructor
  532. /// </summary>
  533. /// <param name="message"></param>
  534. public ObjectExistsException(String message) : base(message) { }
  535. /// <summary>
  536. /// Compliance Constructor
  537. /// </summary>
  538. /// <param name="message"></param>
  539. /// <param name="ex"></param>
  540. public ObjectExistsException(String message, Exception ex) : base(message, ex) { }
  541. /// <summary>
  542. /// Compliance Constructor
  543. /// </summary>
  544. /// <param name="info"></param>
  545. /// <param name="ctx"></param>
  546. protected ObjectExistsException(SerializationInfo info, StreamingContext ctx) : base(info, ctx) { }
  547. }
  548. /// <summary>
  549. /// Exception indicating that a group already exists.
  550. /// </summary>
  551. public class GroupExistsException : ObjectExistsException
  552. {
  553. internal GroupExistsException(string group, object target)
  554. : base(StringUtil.Format(Strings.GroupExists, group), target)
  555. {
  556. }
  557. /// <summary>
  558. /// Compliance Constructor
  559. /// </summary>
  560. public GroupExistsException() : base() { }
  561. /// <summary>
  562. /// Compliance Constructor
  563. /// </summary>
  564. /// <param name="message"></param>
  565. public GroupExistsException(String message) : base(message) { }
  566. /// <summary>
  567. /// Compliance Constructor
  568. /// </summary>
  569. /// <param name="message"></param>
  570. /// <param name="ex"></param>
  571. public GroupExistsException(String message, Exception ex) : base(message, ex) { }
  572. /// <summary>
  573. /// Compliance Constructor
  574. /// </summary>
  575. /// <param name="info"></param>
  576. /// <param name="ctx"></param>
  577. protected GroupExistsException(SerializationInfo info, StreamingContext ctx) : base(info, ctx) { }
  578. }
  579. /// <summary>
  580. /// Exception indicating that a group already exists.
  581. /// </summary>
  582. public class UserExistsException : ObjectExistsException
  583. {
  584. internal UserExistsException(string user, object target)
  585. : base(StringUtil.Format(Strings.UserExists, user), target)
  586. {
  587. }
  588. /// <summary>
  589. /// Compliance Constructor
  590. /// </summary>
  591. public UserExistsException() : base() { }
  592. /// <summary>
  593. /// Compliance Constructor
  594. /// </summary>
  595. /// <param name="message"></param>
  596. public UserExistsException(String message) : base(message) { }
  597. /// <summary>
  598. /// Compliance Constructor
  599. /// </summary>
  600. /// <param name="message"></param>
  601. /// <param name="ex"></param>
  602. public UserExistsException(String message, Exception ex) : base(message, ex) { }
  603. /// <summary>
  604. /// Compliance Constructor
  605. /// </summary>
  606. /// <param name="info"></param>
  607. /// <param name="ctx"></param>
  608. protected UserExistsException(SerializationInfo info, StreamingContext ctx) : base(info, ctx) { }
  609. }
  610. /// <summary>
  611. /// Exception indicating that an object already exists as a group member.
  612. /// </summary>
  613. public class MemberExistsException : ObjectExistsException
  614. {
  615. internal MemberExistsException(string member, string group, object target)
  616. : base(StringUtil.Format(Strings.MemberExists, member, group), target)
  617. {
  618. }
  619. /// <summary>
  620. /// Compliance Constructor
  621. /// </summary>
  622. public MemberExistsException() : base() { }
  623. /// <summary>
  624. /// Compliance Constructor
  625. /// </summary>
  626. /// <param name="message"></param>
  627. public MemberExistsException(String message) : base(message) { }
  628. /// <summary>
  629. /// Compliance Constructor
  630. /// </summary>
  631. /// <param name="message"></param>
  632. /// <param name="ex"></param>
  633. public MemberExistsException(String message, Exception ex) : base(message, ex) { }
  634. /// <summary>
  635. /// Compliance Constructor
  636. /// </summary>
  637. /// <param name="info"></param>
  638. /// <param name="ctx"></param>
  639. protected MemberExistsException(SerializationInfo info, StreamingContext ctx) : base(info, ctx) { }
  640. }
  641. }