NetMQException.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. using System;
  2. using System.Net.Sockets;
  3. using System.Runtime.Serialization;
  4. using System.Security.Permissions;
  5. using NetMQ.Core;
  6. namespace NetMQ
  7. {
  8. /// <summary>
  9. /// Base class for custom exceptions within the NetMQ library.
  10. /// </summary>
  11. [Serializable]
  12. public class NetMQException : Exception
  13. {
  14. /// <summary>
  15. /// Exception error code
  16. /// </summary>
  17. public ErrorCode ErrorCode { get; }
  18. #region Exception contract & serialisation
  19. // For discussion of this contract, see https://msdn.microsoft.com/en-us/library/ms182151.aspx
  20. /// <summary>
  21. /// Create NetMQ Exception
  22. /// </summary>
  23. public NetMQException()
  24. {}
  25. /// <summary>
  26. /// Create a new NetMQ exception
  27. /// </summary>
  28. /// <param name="message"></param>
  29. public NetMQException(string message)
  30. : base(message)
  31. {}
  32. /// <summary>
  33. /// Create a new NetMQ exception
  34. /// </summary>
  35. /// <param name="message"></param>
  36. /// <param name="innerException"></param>
  37. public NetMQException(string message, Exception innerException)
  38. : base(message, innerException)
  39. {}
  40. /// <summary>Constructor for serialisation.</summary>
  41. protected NetMQException(SerializationInfo info, StreamingContext context)
  42. : base(info, context)
  43. {
  44. ErrorCode = (ErrorCode)info.GetInt32("ErrorCode");
  45. }
  46. /// <inheritdoc />
  47. [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
  48. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  49. {
  50. info.AddValue("ErrorCode", ErrorCode);
  51. base.GetObjectData(info, context);
  52. }
  53. #endregion
  54. /// <summary>
  55. /// Create a new NetMQException containing the given Exception, Message and ErrorCode.
  56. /// </summary>
  57. /// <param name="innerException">an Exception that this exception will expose via it's InnerException property</param>
  58. /// <param name="message">the textual description of what gave rise to this exception, to expose via the Message property</param>
  59. /// <param name="errorCode">an ErrorCode that this exception will expose via its ErrorCode property</param>
  60. protected NetMQException(Exception? innerException, string? message, ErrorCode errorCode)
  61. : base(message, innerException)
  62. {
  63. ErrorCode = errorCode;
  64. }
  65. /// <summary>
  66. /// Create and return a new NetMQException with no Message containing only the given SocketException.
  67. /// </summary>
  68. /// <param name="innerException">a SocketException that this exception will expose via its InnerException property</param>
  69. /// <returns>a new NetMQException</returns>
  70. public static NetMQException Create(SocketException innerException)
  71. {
  72. return Create(innerException.SocketErrorCode, innerException);
  73. }
  74. /// <summary>
  75. /// Create and return a new NetMQException with no Message containing the given SocketError and Exception.
  76. /// </summary>
  77. /// <param name="error">a SocketError that this exception will carry and expose via its ErrorCode property</param>
  78. /// <param name="innerException">an Exception that this exception will expose via its InnerException property</param>
  79. /// <returns>a new NetMQException</returns>
  80. public static NetMQException Create(SocketError error, Exception? innerException = null)
  81. {
  82. var errorCode = error.ToErrorCode();
  83. #if DEBUG
  84. if (errorCode == 0)
  85. {
  86. var s = $"(And within NetMQException.Create: Unanticipated error-code: {error})";
  87. return Create(errorCode: errorCode, message: s, innerException: innerException);
  88. }
  89. #endif
  90. return Create(errorCode, innerException);
  91. }
  92. /// <summary>
  93. /// Create and return a new NetMQException with no Message containing the given ErrorCode and Exception.
  94. /// </summary>
  95. /// <param name="errorCode">an ErrorCode for this exception to contain and expose via its ErrorCode property</param>
  96. /// <param name="innerException">an Exception for this exception to contain and expose via its InnerException property</param>
  97. /// <returns>a new NetMQException</returns>
  98. public static NetMQException Create(ErrorCode errorCode, Exception? innerException)
  99. {
  100. return Create(errorCode, null, innerException);
  101. }
  102. /// <summary>
  103. /// Create and return a new NetMQException with no Message containing only the given ErrorCode.
  104. /// </summary>
  105. /// <param name="errorCode">an ErrorCode that this exception will carry and expose via its ErrorCode property</param>
  106. /// <returns>a new NetMQException</returns>
  107. public static NetMQException Create(ErrorCode errorCode)
  108. {
  109. return Create(null, errorCode);
  110. }
  111. /// <summary>
  112. /// Create and return a new NetMQException with the given Message and ErrorCode.
  113. /// </summary>
  114. /// <param name="message">the textual description of what gave rise to this exception, to expose via the Message property</param>
  115. /// <param name="errorCode">an ErrorCode that this exception will carry and expose via its ErrorCode property</param>
  116. /// <returns>a new NetMQException</returns>
  117. public static NetMQException Create(string? message, ErrorCode errorCode)
  118. {
  119. return Create(errorCode, message, null);
  120. }
  121. /// <summary>
  122. /// Create and return a new NetMQException with the given ErrorCode, Message, and Exception.
  123. /// </summary>
  124. /// <param name="errorCode">an ErrorCode that this exception will contain and expose via its ErrorCode property</param>
  125. /// <param name="message">the textual description of what gave rise to this exception, to expose via the Message property</param>
  126. /// <param name="innerException">an Exception that this exception will expose via its InnerException property</param>
  127. /// <returns>a new NetMQException, or subclass of NetMQException that corresponds to the given ErrorCode</returns>
  128. private static NetMQException Create(ErrorCode errorCode, string? message, Exception? innerException)
  129. {
  130. switch (errorCode)
  131. {
  132. case ErrorCode.ContextTerminated:
  133. return new TerminatingException(innerException, message);
  134. case ErrorCode.Invalid:
  135. return new InvalidException(innerException, message);
  136. case ErrorCode.EndpointNotFound:
  137. return new EndpointNotFoundException(innerException, message);
  138. case ErrorCode.AddressAlreadyInUse:
  139. return new AddressAlreadyInUseException(innerException, message);
  140. case ErrorCode.ProtocolNotSupported:
  141. return new ProtocolNotSupportedException(innerException, message);
  142. case ErrorCode.HostUnreachable:
  143. return new HostUnreachableException(innerException, message);
  144. case ErrorCode.FiniteStateMachine:
  145. return new FiniteStateMachineException(innerException, message);
  146. case ErrorCode.Fault:
  147. return new FaultException(innerException, message);
  148. default:
  149. return new NetMQException(innerException, message, errorCode);
  150. }
  151. }
  152. }
  153. /// <summary>
  154. /// AddressAlreadyInUseException is a NetMQException that is used within SocketBase.Bind to signal an address-conflict.
  155. /// </summary>
  156. [Serializable]
  157. public class AddressAlreadyInUseException : NetMQException
  158. {
  159. /// <summary>
  160. /// Create a new AddressAlreadyInUseException with a given inner-exception and message.
  161. /// </summary>
  162. /// <param name="innerException">an Exception for this new exception to contain and expose via its InnerException property</param>
  163. /// <param name="message">the textual description of what gave rise to this exception, to expose via the Message property</param>
  164. public AddressAlreadyInUseException(Exception? innerException, string? message)
  165. : base(innerException, message, ErrorCode.AddressAlreadyInUse)
  166. {
  167. }
  168. /// <summary>
  169. /// Create a new AddressAlreadyInUseException with a given message.
  170. /// </summary>
  171. /// <param name="message">the textual description of what gave rise to this exception, to expose via the Message property</param>
  172. public AddressAlreadyInUseException(string? message)
  173. : this(null, message)
  174. {
  175. }
  176. /// <summary>Constructor for serialisation.</summary>
  177. protected AddressAlreadyInUseException(SerializationInfo info, StreamingContext context)
  178. : base(info, context)
  179. {
  180. }
  181. }
  182. /// <summary>
  183. /// EndpointNotFoundException is a NetMQException that is used within Ctx.FindEndpoint to signal a failure to find a specified address.
  184. /// </summary>
  185. [Serializable]
  186. public class EndpointNotFoundException : NetMQException
  187. {
  188. /// <summary>
  189. /// Create a new EndpointNotFoundException with a given inner-exception and message.
  190. /// </summary>
  191. /// <param name="innerException">an Exception for this new exception to contain and expose via its InnerException property</param>
  192. /// <param name="message">the textual description of what gave rise to this exception, to expose via the Message property</param>
  193. public EndpointNotFoundException(Exception? innerException, string? message)
  194. : base(innerException, message, ErrorCode.EndpointNotFound)
  195. {
  196. }
  197. /// <summary>
  198. /// Create a new EndpointNotFoundException with a given message.
  199. /// </summary>
  200. /// <param name="message">the textual description of what gave rise to this exception, to expose via the Message property</param>
  201. public EndpointNotFoundException(string? message)
  202. : this(null, message)
  203. {
  204. }
  205. /// <summary>
  206. /// Create a new EndpointNotFoundException with no message nor inner-exception.
  207. /// </summary>
  208. public EndpointNotFoundException()
  209. : this(null)
  210. {
  211. }
  212. /// <summary>Constructor for serialisation.</summary>
  213. protected EndpointNotFoundException(SerializationInfo info, StreamingContext context)
  214. : base(info, context)
  215. {
  216. }
  217. }
  218. /// <summary>
  219. /// TerminatingException is a NetMQException that is used within SocketBase and Ctx to signal
  220. /// that you're making the mistake of trying to do further work after terminating the message-queueing system.
  221. /// </summary>
  222. [Serializable]
  223. public class TerminatingException : NetMQException
  224. {
  225. /// <summary>
  226. /// Create a new TerminatingException with a given inner-exception and message.
  227. /// </summary>
  228. /// <param name="innerException">an Exception for this new exception to contain and expose via its InnerException property</param>
  229. /// <param name="message">the textual description of what gave rise to this exception, to expose via the Message property</param>
  230. internal TerminatingException(Exception? innerException, string? message)
  231. : base(innerException, message, ErrorCode.ContextTerminated)
  232. {
  233. }
  234. /// <summary>
  235. /// Create new TerminatingException
  236. /// </summary>
  237. /// <param name="message"></param>
  238. public TerminatingException(string? message)
  239. : this(null, message)
  240. {
  241. }
  242. /// <summary>
  243. /// Create a new TerminatingException with no message nor inner-exception.
  244. /// </summary>
  245. internal TerminatingException()
  246. : this(null, null)
  247. {
  248. }
  249. /// <summary>Constructor for serialisation.</summary>
  250. protected TerminatingException(SerializationInfo info, StreamingContext context)
  251. : base(info, context)
  252. {
  253. }
  254. }
  255. /// <summary>
  256. /// InvalidException is a NetMQException that is used within the message-queueing system to signal invalid value errors.
  257. /// </summary>
  258. [Serializable]
  259. public class InvalidException : NetMQException
  260. {
  261. /// <summary>
  262. /// Create a new InvalidException with a given inner-exception and message.
  263. /// </summary>
  264. /// <param name="innerException">an Exception for this new exception to contain and expose via its InnerException property</param>
  265. /// <param name="message">the textual description of what gave rise to this exception, to expose via the Message property</param>
  266. public InvalidException(Exception? innerException, string? message)
  267. : base(innerException, message, ErrorCode.Invalid)
  268. {
  269. }
  270. /// <summary>
  271. /// Create a new InvalidException with the given message.
  272. /// </summary>
  273. /// <param name="message">the textual description of what gave rise to this exception, to expose via the Message property</param>
  274. public InvalidException(string? message)
  275. : this(null, message)
  276. {
  277. }
  278. /// <summary>
  279. /// Create a new InvalidException with no message nor inner-exception.
  280. /// </summary>
  281. public InvalidException()
  282. : this(null, null)
  283. {
  284. }
  285. /// <summary>Constructor for serialisation.</summary>
  286. protected InvalidException(SerializationInfo info, StreamingContext context)
  287. : base(info, context)
  288. {
  289. }
  290. }
  291. /// <summary>
  292. /// FaultException is a NetMQException that is used within the message-queueing system to signal general fault conditions.
  293. /// </summary>
  294. [Serializable]
  295. public class FaultException : NetMQException
  296. {
  297. /// <summary>
  298. /// Create a new FaultException with a given inner-exception and message.
  299. /// </summary>
  300. /// <param name="innerException">an Exception for this new exception to contain and expose via its InnerException property</param>
  301. /// <param name="message">the textual description of what gave rise to this exception, to expose via the Message property</param>
  302. internal FaultException(Exception? innerException, string? message)
  303. : base(innerException, message, ErrorCode.Fault)
  304. {
  305. }
  306. /// <summary>
  307. /// Create a new FaultException with the given message.
  308. /// </summary>
  309. /// <param name="message">the textual description of what gave rise to this exception, to expose via the Message property</param>
  310. internal FaultException(string? message)
  311. : this(null, message)
  312. {
  313. }
  314. /// <summary>
  315. /// Create a new FaultException with no message nor inner-exception.
  316. /// </summary>
  317. public FaultException()
  318. : this(null, null)
  319. {
  320. }
  321. /// <summary>Constructor for serialisation.</summary>
  322. protected FaultException(SerializationInfo info, StreamingContext context)
  323. : base(info, context)
  324. {
  325. }
  326. }
  327. /// <summary>
  328. /// ProtocolNotSupportedException is a NetMQException that is used within the message-queueing system to signal
  329. /// mistakes in properly utilizing the communications protocols.
  330. /// </summary>
  331. [Serializable]
  332. public class ProtocolNotSupportedException : NetMQException
  333. {
  334. /// <summary>
  335. /// Create a new ProtocolNotSupportedException with a given inner-exception and message.
  336. /// </summary>
  337. /// <param name="innerException">an Exception for this new exception to contain and expose via its InnerException property</param>
  338. /// <param name="message">the textual description of what gave rise to this exception, to expose via the Message property</param>
  339. internal ProtocolNotSupportedException(Exception? innerException, string? message)
  340. : base(innerException, message, ErrorCode.ProtocolNotSupported)
  341. {
  342. }
  343. /// <summary>
  344. /// Create a new ProtocolNotSupportedException with the given message.
  345. /// </summary>
  346. /// <param name="message">the textual description of what gave rise to this exception, to expose via the Message property</param>
  347. internal ProtocolNotSupportedException(string? message)
  348. : this(null, message)
  349. {
  350. }
  351. /// <summary>
  352. /// Create a new ProtocolNotSupportedException with no message nor inner-exception.
  353. /// </summary>
  354. public ProtocolNotSupportedException()
  355. : this(null, null)
  356. {
  357. }
  358. /// <summary>Constructor for serialisation.</summary>
  359. protected ProtocolNotSupportedException(SerializationInfo info, StreamingContext context)
  360. : base(info, context)
  361. {
  362. }
  363. }
  364. /// <summary>
  365. /// HostUnreachableException is an Exception that is used within the message-queueing system
  366. /// to signal failures to communicate with a host.
  367. /// </summary>
  368. [Serializable]
  369. public class HostUnreachableException : NetMQException
  370. {
  371. /// <summary>
  372. /// Create a new HostUnreachableException with a given inner-exception and message.
  373. /// </summary>
  374. /// <param name="innerException">an Exception for this new exception to contain and expose via its InnerException property</param>
  375. /// <param name="message">the textual description of what gave rise to this exception, to expose via the Message property</param>
  376. internal HostUnreachableException(Exception? innerException, string? message)
  377. : base(innerException, message, ErrorCode.HostUnreachable)
  378. {
  379. }
  380. /// <summary>
  381. /// Create a new HostUnreachableException with the given message.
  382. /// </summary>
  383. /// <param name="message">the textual description of what gave rise to this exception, to expose via the Message property</param>
  384. internal HostUnreachableException(string? message)
  385. : this(null, message)
  386. {
  387. }
  388. /// <summary>
  389. /// Create a new HostUnreachableException with no message nor inner-exception.
  390. /// </summary>
  391. public HostUnreachableException()
  392. : this(null, null)
  393. {
  394. }
  395. /// <summary>Constructor for serialisation.</summary>
  396. protected HostUnreachableException(SerializationInfo info, StreamingContext context)
  397. : base(info, context)
  398. {
  399. }
  400. }
  401. /// <summary>
  402. /// FiniteStateMachineException is an Exception that is used within the message-queueing system
  403. /// to signal errors in the send/receive order with request/response sockets.
  404. /// </summary>
  405. [Serializable]
  406. public class FiniteStateMachineException : NetMQException
  407. {
  408. /// <summary>
  409. /// Create a new FiniteStateMachineException with a given inner-exception and message.
  410. /// </summary>
  411. /// <param name="innerException">an Exception for this new exception to contain and expose via its InnerException property</param>
  412. /// <param name="message">the textual description of what gave rise to this exception, to expose via the Message property</param>
  413. internal FiniteStateMachineException(Exception? innerException, string? message)
  414. : base(innerException, message, ErrorCode.FiniteStateMachine)
  415. {
  416. }
  417. /// <summary>
  418. /// Create a new FiniteStateMachineException with the given message.
  419. /// </summary>
  420. /// <param name="message">the textual description of what gave rise to this exception, to expose via the Message property</param>
  421. internal FiniteStateMachineException(string? message)
  422. : this(null, message)
  423. {
  424. }
  425. /// <summary>
  426. /// Create a new FiniteStateMachineException with no message nor inner-exception.
  427. /// </summary>
  428. public FiniteStateMachineException()
  429. : this(null, null)
  430. {
  431. }
  432. /// <summary>Constructor for serialisation.</summary>
  433. protected FiniteStateMachineException(SerializationInfo info, StreamingContext context)
  434. : base(info, context)
  435. {
  436. }
  437. }
  438. }