Skip to content

Commit

Permalink
[Event Hubs] Include Inner Exception Details (Azure#33014)
Browse files Browse the repository at this point in the history
* [Event Hubs] Include Inner Exception Details

The focus of these changes is to include the details of any inner exception
as part of the `ToString` output of `EventHubsException`.
  • Loading branch information
jsquire authored Dec 13, 2022
1 parent fc62800 commit c4b93bf
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
2 changes: 2 additions & 0 deletions sdk/eventhub/Azure.Messaging.EventHubs.Processor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

### Other Changes

- Calling `ToString` on an `EventHubsException` now includes details of any inner exception.

## 5.7.5 (2022-11-22)

### Bugs Fixed
Expand Down
2 changes: 2 additions & 0 deletions sdk/eventhub/Azure.Messaging.EventHubs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

### Other Changes

- Calling `ToString` on an `EventHubsException` now includes details of any inner exception.

## 5.7.5 (2022-11-22)

### Acknowledgments
Expand Down
12 changes: 11 additions & 1 deletion sdk/eventhub/Azure.Messaging.EventHubs/src/EventHubsException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,17 @@ internal EventHubsException(string eventHubName,
/// <returns>A <see cref="System.String" /> that represents this instance.</returns>
///
public override string ToString() =>
$"{ typeof(EventHubsException).FullName }({ Reason }): { Message }{ Environment.NewLine }{ StackTrace }";
$"{ typeof(EventHubsException).FullName }({ Reason }): { Message }{ Environment.NewLine }{ StackTrace }{ FormatInnerException() }";

/// <summary>
/// Formats the <see cref="Exception.InnerException"/> for inclusion in the <see cref="ToString" />
/// details.
/// </summary>
///
/// <returns>The text to include for the inner exception, if any.</returns>
///
private string FormatInnerException() =>
(InnerException == null) ? string.Empty : $"{ Environment.NewLine }{ InnerException }";

/// <summary>
/// The set of well-known reasons for an Event Hubs operation failure that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
namespace Azure.Messaging.EventHubs.Processor
{
/// <summary>
/// Contains information about a partition whose processing surfaced an exception, as well as
/// the exception that occurred.
/// Contains information about an exception that occurred within the processor, whether for a specific partition
/// or as part of the processor's internal operations.
/// </summary>
///
/// <seealso href="https://www.nuget.org/packages/Azure.Messaging.EventHubs.Processor">Azure.Messaging.EventHubs.Processor (NuGet)</seealso>
///
public struct ProcessErrorEventArgs
{
/// <summary>
/// The identifier of the partition being processed when the exception occurred.
/// The identifier of the partition being processed when the exception occurred. If the exception did
/// not occur for a specific partition, this value will be <c>null</c>.
/// </summary>
///
public string PartitionId { get; }
Expand All @@ -29,7 +30,7 @@ public struct ProcessErrorEventArgs
public string Operation { get; }

/// <summary>
/// The exception that was occurred during partition processing.
/// The exception that was occurred during processing.
/// </summary>
///
public Exception Exception { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,32 @@ public void ToStringContainsExceptionDetails()
Assert.That(exceptionString, Contains.Substring(eventHubName), "The ToString value should contain the Event Hub name.");
Assert.That(exceptionString, Contains.Substring($"{ Environment.NewLine }{ instance.StackTrace }"), "The ToString value should contain the stack trace on a new line.");
}

/// <summary>
/// Verifies functionality of the <see cref="EventHubsException.ToString" />
/// method.
/// </summary>
///
[Test]
public void ToStringContainsInnerExceptionDetails()
{
var message = "Inner message!";
var innerException = new DivideByZeroException(message);

EventHubsException instance;

try
{
throw new EventHubsException(false, "hub", "Outer", EventHubsException.FailureReason.QuotaExceeded, innerException);
}
catch (EventHubsException ex)
{
instance = ex;
}

var exceptionString = instance.ToString();
Assert.That(exceptionString, Is.Not.Null.And.Not.Empty, "The ToString value should be populated.");
Assert.That(exceptionString, Contains.Substring(innerException.ToString()), "The ToString value should contain the full set of details for the inner exception.");
}
}
}

0 comments on commit c4b93bf

Please sign in to comment.