Skip to content

Commit

Permalink
rebus-org#1154 Error handler not having access to the original except…
Browse files Browse the repository at this point in the history
…ions - DefaultRetryStep using ExceptionInfo factory to create ExceptionInfo instead of newing it up; when configuring Rebus with InMemExceptionInfoFactory [Configure.With(activator).Errors(e => e.UseInMemExceptionInfos())],

an error handler will get an instance of InMemExceptionInfo in HandlePoisonMessage method, with InMemExceptionInfo storing all exceptions within AggregateException; not tested - tests will be added in a subsequent commit when this approach is approved
  • Loading branch information
xhafan committed Mar 22, 2024
1 parent 942cdf5 commit 4be1c8e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Rebus/Retry/IExceptionInfoFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace Rebus.Retry;

Expand All @@ -13,4 +14,13 @@ public interface IExceptionInfoFactory
/// <param name="exception">Source exception.</param>
/// <returns>An <see cref="ExceptionInfo"/> containing information from the supplied exception.</returns>
ExceptionInfo CreateInfo(Exception exception);

/// <summary>
/// Create an <see cref="ExceptionInfo"/> from a collection of exception infos.
/// </summary>
/// <param name="exceptionInfos">Collection of exception infos</param>
/// <param name="message">Message</param>
/// <param name="details">Details</param>
/// <returns></returns>
ExceptionInfo CreateInfo(IEnumerable<ExceptionInfo> exceptionInfos, string message, string details);
}
17 changes: 17 additions & 0 deletions Rebus/Retry/Info/InMemExceptionInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Rebus.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Rebus.Retry.Info;

Expand All @@ -21,6 +23,21 @@ public InMemExceptionInfo(Exception exception) : base(
Exception = exception ?? throw new ArgumentNullException(nameof(exception));
}

/// <summary>
/// Constructs a new in-memory exception info from a collection of exception infos, with an option to override the message and details.
/// </summary>
/// <param name="exceptionInfos">Collection of exception infos</param>
/// <param name="Message">Message</param>
/// <param name="Details">Details</param>
public InMemExceptionInfo(IEnumerable<ExceptionInfo> exceptionInfos, string Message, string Details) : base(
typeof(AggregateException).GetSimpleAssemblyQualifiedName(),
Message,
Details,
DateTimeOffset.Now)
{
Exception = new AggregateException(exceptionInfos.OfType<InMemExceptionInfo>().Select(x => x.Exception));
}

/// <summary>
/// Gets or sets the original exception.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions Rebus/Retry/Info/InMemExceptionInfoFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace Rebus.Retry.Info;

Expand All @@ -17,4 +18,16 @@ public class InMemExceptionInfoFactory : IExceptionInfoFactory
/// <param name="exception">Source exception.</param>
/// <returns>An <see cref="ExceptionInfo"/> containing information from the supplied exception.</returns>
public ExceptionInfo CreateInfo(Exception exception) => new InMemExceptionInfo(exception);

/// <summary>
/// Create an <see cref="ExceptionInfo"/> from a collection of exception infos and store the original exceptions.
/// </summary>
/// <param name="exceptionInfos">Collection of exception infos</param>
/// <param name="message">Message</param>
/// <param name="details">Details</param>
/// <returns></returns>
public ExceptionInfo CreateInfo(IEnumerable<ExceptionInfo> exceptionInfos, string message, string details)
{
return new InMemExceptionInfo(exceptionInfos, message, details);
}
}
19 changes: 19 additions & 0 deletions Rebus/Retry/Info/ToStringExceptionInfoFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using Rebus.Extensions;

namespace Rebus.Retry.Info;

Expand All @@ -13,4 +15,21 @@ public class ToStringExceptionInfoFactory : IExceptionInfoFactory
/// <param name="exception">Source exception.</param>
/// <returns>An <see cref="ExceptionInfo"/> containing information from the supplied exception.</returns>
public ExceptionInfo CreateInfo(Exception exception) => ExceptionInfo.FromException(exception);

/// <summary>
/// Create an <see cref="ExceptionInfo"/> from a collection of exception infos.
/// </summary>
/// <param name="exceptionInfos">Collection of exception infos</param>
/// <param name="message">Message</param>
/// <param name="details">Details</param>
/// <returns></returns>
public ExceptionInfo CreateInfo(IEnumerable<ExceptionInfo> exceptionInfos, string message, string details)
{
return new ExceptionInfo(
Type: typeof(AggregateException).GetSimpleAssemblyQualifiedName(),
Message: message,
Details: details,
Time: DateTimeOffset.Now
);
}
}
7 changes: 3 additions & 4 deletions Rebus/Retry/Simple/DefaultRetryStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,13 @@ async Task PassToErrorHandler(StepContext context, ExceptionInfo exception)
await scope.CompleteAsync();
}

static ExceptionInfo GetAggregateException(IEnumerable<ExceptionInfo> exceptions)
ExceptionInfo GetAggregateException(IEnumerable<ExceptionInfo> exceptions)
{
var list = exceptions.ToList();

return new(typeof(AggregateException).GetSimpleAssemblyQualifiedName(),
return _exceptionInfoFactory.CreateInfo(list,
$"{list.Count} unhandled exceptions",
string.Join(Environment.NewLine + Environment.NewLine, list.Select(e => e.GetFullErrorDescription())),
DateTimeOffset.Now
string.Join(Environment.NewLine + Environment.NewLine, list.Select(e => e.GetFullErrorDescription()))
);
}
}

0 comments on commit 4be1c8e

Please sign in to comment.