| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Globalization; |
| | 6 | | using Azure.Messaging.EventHubs.Consumer; |
| | 7 | |
|
| | 8 | | namespace Azure.Messaging.EventHubs |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Serves as a basis for exceptions produced within the Event Hubs |
| | 12 | | /// context. |
| | 13 | | /// </summary> |
| | 14 | | /// |
| | 15 | | /// <seealso cref="System.Exception" /> |
| | 16 | | /// |
| | 17 | | public class EventHubsException : Exception |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// Indicates whether an exception should be considered transient or final. |
| | 21 | | /// </summary> |
| | 22 | | /// |
| | 23 | | /// <value><c>true</c> if the exception is likely transient; otherwise, <c>false</c>.</value> |
| | 24 | | /// |
| 184 | 25 | | public bool IsTransient { get; } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// The reason for the failure of an Event Hubs operation that resulted |
| | 29 | | /// in the exception. |
| | 30 | | /// </summary> |
| | 31 | | /// |
| 358 | 32 | | public FailureReason Reason { get; } |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// The name of the Event Hubs to which the exception is associated. |
| | 36 | | /// </summary> |
| | 37 | | /// |
| | 38 | | /// <value>The name of the Event Hub, if available; otherwise, <c>null</c>.</value> |
| | 39 | | /// |
| 492 | 40 | | public string EventHubName { get; } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Gets a message that describes the current exception. |
| | 44 | | /// </summary> |
| | 45 | | /// |
| | 46 | | public override string Message |
| | 47 | | { |
| | 48 | | get |
| | 49 | | { |
| 234 | 50 | | if (string.IsNullOrEmpty(EventHubName)) |
| | 51 | | { |
| 26 | 52 | | return base.Message; |
| | 53 | | } |
| | 54 | |
|
| 208 | 55 | | return string.Format(CultureInfo.InvariantCulture, "{0} ({1})", base.Message, EventHubName); |
| | 56 | | } |
| | 57 | | } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Initializes a new instance of the <see cref="EventHubsException"/> class. |
| | 61 | | /// </summary> |
| | 62 | | /// |
| | 63 | | /// <param name="isTransient"><c>true</c> if the exception should be considered transient; otherwise, <c>false</ |
| | 64 | | /// <param name="eventHubName">The name of the Event Hub to which the exception is associated.</param> |
| | 65 | | /// |
| | 66 | | public EventHubsException(bool isTransient, |
| 62 | 67 | | string eventHubName) : this(isTransient, eventHubName, null, FailureReason.GeneralErro |
| | 68 | | { |
| 62 | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Initializes a new instance of the <see cref="EventHubsException"/> class. |
| | 73 | | /// </summary> |
| | 74 | | /// |
| | 75 | | /// <param name="isTransient"><c>true</c> if the exception should be considered transient; otherwise, <c>false</ |
| | 76 | | /// <param name="eventHubName">The name of the Event Hub to which the exception is associated.</param> |
| | 77 | | /// <param name="reason">The reason for the failure that resulted in the exception.</param> |
| | 78 | | /// |
| | 79 | | public EventHubsException(bool isTransient, |
| | 80 | | string eventHubName, |
| 22 | 81 | | FailureReason reason) : this(isTransient, eventHubName, null, reason, null) |
| | 82 | | { |
| 22 | 83 | | } |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Initializes a new instance of the <see cref="EventHubsException"/> class. |
| | 87 | | /// </summary> |
| | 88 | | /// |
| | 89 | | /// <param name="isTransient"><c>true</c> if the exception should be considered transient; otherwise, <c>false</ |
| | 90 | | /// <param name="eventHubName">The name of the Event Hub to which the exception is associated.</param> |
| | 91 | | /// <param name="message">The error message that explains the reason for the exception.</param> |
| | 92 | | /// |
| | 93 | | public EventHubsException(bool isTransient, |
| | 94 | | string eventHubName, |
| 14 | 95 | | string message) : this(isTransient, eventHubName, message, FailureReason.GeneralError, |
| | 96 | | { |
| 14 | 97 | | } |
| | 98 | |
|
| | 99 | | /// <summary> |
| | 100 | | /// Initializes a new instance of the <see cref="EventHubsException"/> class. |
| | 101 | | /// </summary> |
| | 102 | | /// |
| | 103 | | /// <param name="isTransient"><c>true</c> if the exception should be considered transient; otherwise, <c>false</ |
| | 104 | | /// <param name="eventHubName">The name of the Event Hub to which the exception is associated.</param> |
| | 105 | | /// <param name="message">The error message that explains the reason for the exception.</param> |
| | 106 | | /// <param name="reason">The reason for the failure that resulted in the exception.</param> |
| | 107 | | /// |
| | 108 | | public EventHubsException(bool isTransient, |
| | 109 | | string eventHubName, |
| | 110 | | string message, |
| 10 | 111 | | FailureReason reason) : this(isTransient, eventHubName, message, reason, null) |
| | 112 | | { |
| 10 | 113 | | } |
| | 114 | |
|
| | 115 | | /// <summary> |
| | 116 | | /// Initializes a new instance of the <see cref="EventHubsException"/> class, using the <paramref name="reason |
| | 117 | | /// to detect whether or not it should be transient. |
| | 118 | | /// </summary> |
| | 119 | | /// |
| | 120 | | /// <param name="eventHubName">The name of the Event Hub to which the exception is associated.</param> |
| | 121 | | /// <param name="message">The error message that explains the reason for the exception.</param> |
| | 122 | | /// <param name="reason">The reason for the failure that resulted in the exception.</param> |
| | 123 | | /// |
| | 124 | | public EventHubsException(string eventHubName, |
| | 125 | | string message, |
| 212 | 126 | | FailureReason reason) : this(default, eventHubName, message, reason, null) |
| | 127 | | { |
| | 128 | | switch (reason) |
| | 129 | | { |
| | 130 | | case FailureReason.ServiceCommunicationProblem: |
| | 131 | | case FailureReason.ServiceTimeout: |
| | 132 | | case FailureReason.ServiceBusy: |
| 134 | 133 | | IsTransient = true; |
| 134 | 134 | | break; |
| | 135 | |
|
| | 136 | | default: |
| 78 | 137 | | IsTransient = false; |
| | 138 | | break; |
| | 139 | | } |
| 78 | 140 | | } |
| | 141 | |
|
| | 142 | | /// <summary> |
| | 143 | | /// Initializes a new instance of the <see cref="EventHubsException"/> class. |
| | 144 | | /// </summary> |
| | 145 | | /// |
| | 146 | | /// <param name="isTransient"><c>true</c> if the exception should be considered transient; otherwise, <c>false</ |
| | 147 | | /// <param name="eventHubName">The name of the Event Hub to which the exception is associated.</param> |
| | 148 | | /// <param name="message">The error message that explains the reason for the exception.</param> |
| | 149 | | /// <param name="innerException">The exception that is the cause of the current exception, or a null reference i |
| | 150 | | /// |
| | 151 | | public EventHubsException(bool isTransient, |
| | 152 | | string eventHubName, |
| | 153 | | string message, |
| 2 | 154 | | Exception innerException) : this(isTransient, eventHubName, message, FailureReason.Gen |
| | 155 | | { |
| 2 | 156 | | } |
| | 157 | |
|
| | 158 | | /// <summary> |
| | 159 | | /// Initializes a new instance of the <see cref="EventHubsException"/> class. |
| | 160 | | /// </summary> |
| | 161 | | /// |
| | 162 | | /// <param name="isTransient"><c>true</c> if the exception should be considered transient; otherwise, <c>false</ |
| | 163 | | /// <param name="eventHubName">The name of the Event Hub to which the exception is associated.</param> |
| | 164 | | /// <param name="message">The error message that explains the reason for the exception.</param> |
| | 165 | | /// <param name="reason">The reason for the failure that resulted in the exception.</param> |
| | 166 | | /// <param name="innerException">The exception that is the cause of the current exception, or a null reference i |
| | 167 | | /// |
| | 168 | | public EventHubsException(bool isTransient, |
| | 169 | | string eventHubName, |
| | 170 | | string message, |
| | 171 | | FailureReason reason, |
| 334 | 172 | | Exception innerException) : base(message, innerException) |
| | 173 | | { |
| 334 | 174 | | IsTransient = isTransient; |
| 334 | 175 | | EventHubName = eventHubName; |
| 334 | 176 | | Reason = reason; |
| 334 | 177 | | } |
| | 178 | |
|
| | 179 | | /// <summary> |
| | 180 | | /// Converts the instance to string representation. |
| | 181 | | /// </summary> |
| | 182 | | /// |
| | 183 | | /// <returns>A <see cref="System.String" /> that represents this instance.</returns> |
| | 184 | | /// |
| 18 | 185 | | public override string ToString() => $"{ typeof(EventHubsException).Name }({ Reason })"; |
| | 186 | |
|
| | 187 | | /// <summary> |
| | 188 | | /// The set of well-known reasons for an Event Hubs operation failure that |
| | 189 | | /// was the cause of an exception. |
| | 190 | | /// </summary> |
| | 191 | | /// |
| | 192 | | public enum FailureReason |
| | 193 | | { |
| | 194 | | /// <summary>The exception was the result of a general error within the client library.</summary> |
| | 195 | | GeneralError, |
| | 196 | |
|
| | 197 | | /// <summary>An operation has been attempted using an Event Hubs client instance which has already been clos |
| | 198 | | ClientClosed, |
| | 199 | |
|
| | 200 | | /// <summary>A client was forcefully disconnected from an Event Hub instance. This typically occurs when an |
| | 201 | | ConsumerDisconnected, |
| | 202 | |
|
| | 203 | | /// <summary>An Event Hubs resource, such as an Event Hub, consumer group, or partition cannot be found by t |
| | 204 | | ResourceNotFound, |
| | 205 | |
|
| | 206 | | /// <summary>A message is larger than the maximum size allowed for its transport.</summary> |
| | 207 | | MessageSizeExceeded, |
| | 208 | |
|
| | 209 | | /// <summary>The quota applied to an Event Hubs resource has been exceeded while interacting with the Azure |
| | 210 | | QuotaExceeded, |
| | 211 | |
|
| | 212 | | /// <summary>The Azure Event Hubs service reports that it is busy in response to a client request to perform |
| | 213 | | ServiceBusy, |
| | 214 | |
|
| | 215 | | /// <summary>An operation or other request timed out while interacting with the Azure Event Hubs service.</s |
| | 216 | | ServiceTimeout, |
| | 217 | |
|
| | 218 | | /// <summary>There was a general communications error encountered when interacting with the Azure Event Hubs |
| | 219 | | ServiceCommunicationProblem |
| | 220 | | } |
| | 221 | | } |
| | 222 | | } |