| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Azure.Core; |
| | | 7 | | using Microsoft.Azure.Amqp; |
| | | 8 | | |
| | | 9 | | namespace Azure.Messaging.EventHubs.Amqp |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// The set of extension methods for the <see cref="Exception" /> class. |
| | | 13 | | /// </summary> |
| | | 14 | | /// |
| | | 15 | | internal static class ExceptionExtensions |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Considers an exception surfaced during an AMQP-based service operation, unwrapping |
| | | 19 | | /// and translating it into the form that should be considered for error handling |
| | | 20 | | /// decisions. |
| | | 21 | | /// </summary> |
| | | 22 | | /// |
| | | 23 | | /// <param name="instance">The instance that this method was invoked on.</param> |
| | | 24 | | /// <param name="eventHubName">The name of the Event Hub that the service operation was targeting.</param> |
| | | 25 | | /// |
| | | 26 | | /// <returns>The <see cref="Exception" /> that corresponds to the <paramref name="instance" /> and which represe |
| | | 27 | | /// |
| | | 28 | | public static Exception TranslateServiceException(this Exception instance, |
| | | 29 | | string eventHubName) |
| | | 30 | | { |
| | 226 | 31 | | Argument.AssertNotNull(instance, nameof(instance)); |
| | | 32 | | |
| | 224 | 33 | | switch (instance) |
| | | 34 | | { |
| | | 35 | | case AmqpException amqpEx: |
| | 0 | 36 | | return AmqpError.CreateExceptionForError(amqpEx.Error, eventHubName); |
| | | 37 | | |
| | 94 | 38 | | case OperationCanceledException operationEx when (operationEx.InnerException is AmqpException): |
| | 12 | 39 | | return AmqpError.CreateExceptionForError(((AmqpException)operationEx.InnerException).Error, eventHub |
| | | 40 | | |
| | 82 | 41 | | case OperationCanceledException operationEx when (operationEx.InnerException != null): |
| | 16 | 42 | | return operationEx.InnerException; |
| | | 43 | | |
| | 66 | 44 | | case OperationCanceledException operationEx when (!(operationEx is TaskCanceledException)): |
| | 62 | 45 | | return new EventHubsException(eventHubName, operationEx.Message, EventHubsException.FailureReason.Se |
| | | 46 | | |
| | | 47 | | default: |
| | 134 | 48 | | return instance; |
| | | 49 | | } |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Considers an exception surfaced during the creation of an AMQP link, determining if the cause was a race c |
| | | 54 | | /// with the connection closing and translating it into the form that should be considered for error handling |
| | | 55 | | /// </summary> |
| | | 56 | | /// |
| | | 57 | | /// <param name="instance">The instance that this method was invoked on.</param> |
| | | 58 | | /// <param name="eventHubName">The name of the Event Hub that the service operation was targeting.</param> |
| | | 59 | | /// |
| | | 60 | | /// <returns>The <see cref="Exception" /> that corresponds to the <paramref name="instance" /> and which represe |
| | | 61 | | /// |
| | | 62 | | public static Exception TranslateConnectionCloseDuringLinkCreationException(this Exception instance, |
| | | 63 | | string eventHubName) |
| | | 64 | | { |
| | 52 | 65 | | Argument.AssertNotNull(instance, nameof(instance)); |
| | | 66 | | |
| | 50 | 67 | | switch (instance) |
| | | 68 | | { |
| | 14 | 69 | | case InvalidOperationException _ when (instance.Message.IndexOf("when the connection is closing", String |
| | | 70 | | case TaskCanceledException _: |
| | 4 | 71 | | return new EventHubsException(true, eventHubName, Resources.CouldNotCreateLink, EventHubsException.F |
| | | 72 | | |
| | | 73 | | case ObjectDisposedException _: |
| | 2 | 74 | | return new EventHubsException(false, eventHubName, Resources.CouldNotCreateLink, EventHubsException. |
| | | 75 | | |
| | | 76 | | default: |
| | 44 | 77 | | return instance; |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | } |