| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Globalization; |
| | 6 | |
|
| | 7 | | namespace Azure.Messaging.ServiceBus |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Serves as a basis for exceptions produced within the Service Bus |
| | 11 | | /// context. |
| | 12 | | /// </summary> |
| | 13 | | /// |
| | 14 | | /// <seealso cref="System.Exception" /> |
| | 15 | | /// |
| | 16 | | public class ServiceBusException : Exception |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// Indicates whether an exception should be considered transient or final. |
| | 20 | | /// </summary> |
| | 21 | | /// |
| | 22 | | /// <value><c>true</c> if the exception is likely transient; otherwise, <c>false</c>.</value> |
| | 23 | | /// |
| 30 | 24 | | public bool IsTransient { get; } |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// The reason for the failure of an Service Bus operation that resulted |
| | 28 | | /// in the exception. |
| | 29 | | /// </summary> |
| | 30 | | /// |
| 204 | 31 | | public ServiceBusFailureReason Reason { get; } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// The name of the Service Bus to which the exception is associated. |
| | 35 | | /// </summary> |
| | 36 | | /// |
| | 37 | | /// <value>The name of the Service Bus entity, if available; otherwise, <c>null</c>.</value> |
| | 38 | | /// |
| 88 | 39 | | public string EntityPath { get; } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Can be used to hold the processor error source when we rethrow exceptions. |
| | 43 | | /// </summary> |
| 0 | 44 | | internal ServiceBusErrorSource? ProcessorErrorSource { get; set; } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Gets a message that describes the current exception. |
| | 48 | | /// </summary> |
| | 49 | | /// |
| | 50 | | public override string Message |
| | 51 | | { |
| | 52 | | get |
| | 53 | | { |
| 88 | 54 | | if (string.IsNullOrEmpty(EntityPath)) |
| | 55 | | { |
| 88 | 56 | | return string.Format( |
| 88 | 57 | | CultureInfo.InvariantCulture, |
| 88 | 58 | | "{0} ({1})", |
| 88 | 59 | | base.Message, |
| 88 | 60 | | Reason); |
| | 61 | | } |
| 0 | 62 | | return string.Format( |
| 0 | 63 | | CultureInfo.InvariantCulture, |
| 0 | 64 | | "{0} ({1} - {2})", |
| 0 | 65 | | base.Message, |
| 0 | 66 | | EntityPath, |
| 0 | 67 | | Reason); |
| | 68 | | } |
| | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Initializes a new instance of the <see cref="ServiceBusException"/> class, using the <paramref name="reaso |
| | 73 | | /// to detect whether or not it should be transient. |
| | 74 | | /// </summary> |
| | 75 | | /// |
| | 76 | | /// <param name="message">The error message that explains the reason for the exception.</param> |
| | 77 | | /// <param name="reason">The reason for the failure that resulted in the exception.</param> |
| | 78 | | /// <param name="entityPath">The name of the Service Bus entity to which the exception is associated.</param> |
| | 79 | | /// <param name="innerException"></param> |
| | 80 | | /// |
| | 81 | | public ServiceBusException( |
| | 82 | | string message, |
| | 83 | | ServiceBusFailureReason reason, |
| | 84 | | string entityPath = default, |
| | 85 | | Exception innerException = default) : |
| 112 | 86 | | this(default, message, entityPath, reason, innerException) |
| | 87 | | { |
| | 88 | | switch (reason) |
| | 89 | | { |
| | 90 | | case ServiceBusFailureReason.ServiceCommunicationProblem: |
| | 91 | | case ServiceBusFailureReason.ServiceTimeout: |
| | 92 | | case ServiceBusFailureReason.ServiceBusy: |
| 16 | 93 | | IsTransient = true; |
| 16 | 94 | | break; |
| | 95 | |
|
| | 96 | | default: |
| 96 | 97 | | IsTransient = false; |
| | 98 | | break; |
| | 99 | | } |
| 96 | 100 | | } |
| | 101 | |
|
| | 102 | | /// <summary> |
| | 103 | | /// Initializes a new instance of the <see cref="ServiceBusException"/> class. |
| | 104 | | /// </summary> |
| | 105 | | /// |
| | 106 | | /// <param name="isTransient"><c>true</c> if the exception should be considered transient; otherwise, <c>false</ |
| | 107 | | /// <param name="message">The error message that explains the reason for the exception.</param> |
| | 108 | | /// <param name="entityName">The name of the Service Bus entity to which the exception is associated.</param> |
| | 109 | | /// <param name="reason">The reason for the failure that resulted in the exception.</param> |
| | 110 | | /// <param name="innerException">The exception that is the cause of the current exception, or a null reference i |
| | 111 | | /// |
| | 112 | | public ServiceBusException(bool isTransient, |
| | 113 | | string message, |
| | 114 | | string entityName = default, |
| | 115 | | ServiceBusFailureReason reason = ServiceBusFailureReason.GeneralError, |
| 122 | 116 | | Exception innerException = default) : base(message, innerException) |
| | 117 | | { |
| 122 | 118 | | IsTransient = isTransient; |
| 122 | 119 | | EntityPath = entityName; |
| 122 | 120 | | Reason = reason; |
| 122 | 121 | | } |
| | 122 | |
|
| | 123 | | /// <summary> |
| | 124 | | /// |
| | 125 | | /// </summary> |
| 0 | 126 | | public ServiceBusException() { } |
| | 127 | | } |
| | 128 | | } |