Skip to content

Commit ac7df01

Browse files
committed
#347 Refactor IConsumerErrorHandler to return a 'retry' response instead of supplying a 'retry' delegate
Signed-off-by: Richard Pringle <[email protected]>
1 parent 78afac0 commit ac7df01

File tree

31 files changed

+710
-590
lines changed

31 files changed

+710
-590
lines changed

docs/intro.md

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,28 +1059,39 @@ public class LoggingConsumerInterceptor<TMessage> : IConsumerInterceptor<TMessag
10591059

10601060
## Error Handling
10611061

1062-
Message processing by consumers or handlers may result in exceptions.
1063-
Starting with version 2.3.0, SMB introduces a standard way to integrate custom error handling logic across different transports.
1064-
1065-
The interface [IConsumerErrorHandler<T>](../src/SlimMessageBus.Host/Consumer/ErrorHandling/IConsumerErrorHandler.cs) enables the definition of custom error handling for specific message types:
1062+
Message processing by consumers or handlers may result in exceptions. The [IConsumerErrorHandler<T>](../src/SlimMessageBus.Host/Consumer/ErrorHandling/IConsumerErrorHandler.cs) provides a standard way to integrate custom error handling logic across different transports.
10661063

10671064
```cs
10681065
public interface IConsumerErrorHandler<in T>
10691066
{
10701067
/// <summary>
1071-
/// Executed when the message consumer (or handler) errors out. This interface allows to intercept and handle the exception.
1072-
/// Use the consumer context to get ahold of transport specific options to proceed (acknowledge/reject message).
1068+
/// <para>
1069+
/// Executed when the message consumer (or handler) errors out. The interface allows for interception of
1070+
/// exceptions to manipulate the processing pipeline (success/fail/retry).
1071+
/// </para>
1072+
/// <para>
1073+
/// The consumer context is available to apply transport specific operations (acknowledge/reject/dead letter/etc).
1074+
/// </para>
1075+
/// <para>
1076+
/// If message execution is to be re-attempted, any delays/jitter should be applied before the method returns.
1077+
/// </para>
10731078
/// </summary>
10741079
/// <param name="message">The message that failed to process.</param>
1075-
/// <param name="retry">Performs another message processing try. The return value is relevant if the consumer was a request handler (it will be its response value). Ensure to pass the return value to the result of the error handler.</param>
10761080
/// <param name="consumerContext">The consumer context for the message processing pipeline.</param>
10771081
/// <param name="exception">Exception that occurred during message processing.</param>
1082+
/// <param name="attempts">The number of times the message has been attempted to be processed.</param>
10781083
/// <returns>The error handling result.</returns>
1079-
Task<ConsumerErrorHandlerResult> OnHandleError(T message, Func<Task<object>> retry, IConsumerContext consumerContext, Exception exception);
1084+
Task<ConsumerErrorHandlerResult> OnHandleError(T message, IConsumerContext consumerContext, Exception exception, int attempts);
10801085
}
10811086
```
10821087

1083-
> The `retry()` parameter allows the message processing pipeline, including consumer interceptors, to retry processing when transient errors occur and retries are desired.
1088+
The returned `ConsumerErrorHandlerResult` object is used to override the execution for the remainder of the execution pipeline.
1089+
| Result | Description |
1090+
|---------|-------------|
1091+
| Failure | The message failed to be processed and should be returned to the queue |
1092+
| Success | The pipeline must treat the message as having been processed successfully |
1093+
| SuccessWithResponse | The pipeline to treat the messagage as having been processed successfully, returning the response to the request/response invocation ([IRequestResponseBus<T>](../src/SlimMessageBus/RequestResponse/IRequestResponseBus.cs)) |
1094+
| Retry | Execute the pipeline again (any delay/jitter should be applied before returning from method) |
10841095

10851096
To enable SMB to recognize the error handler, it must be registered within the Microsoft Dependency Injection (MSDI) framework:
10861097

docs/intro.t.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,14 +1059,17 @@ public class LoggingConsumerInterceptor<TMessage> : IConsumerInterceptor<TMessag
10591059

10601060
## Error Handling
10611061

1062-
Message processing by consumers or handlers may result in exceptions.
1063-
Starting with version 2.3.0, SMB introduces a standard way to integrate custom error handling logic across different transports.
1064-
1065-
The interface [IConsumerErrorHandler<T>](../src/SlimMessageBus.Host/Consumer/ErrorHandling/IConsumerErrorHandler.cs) enables the definition of custom error handling for specific message types:
1062+
Message processing by consumers or handlers may result in exceptions. The [IConsumerErrorHandler<T>](../src/SlimMessageBus.Host/Consumer/ErrorHandling/IConsumerErrorHandler.cs) provides a standard way to integrate custom error handling logic across different transports.
10661063

10671064
@[:cs](../src/SlimMessageBus.Host/Consumer/ErrorHandling/IConsumerErrorHandler.cs,Interface)
10681065

1069-
> The `retry()` parameter allows the message processing pipeline, including consumer interceptors, to retry processing when transient errors occur and retries are desired.
1066+
The returned `ConsumerErrorHandlerResult` object is used to override the execution for the remainder of the execution pipeline.
1067+
| Result | Description |
1068+
|---------|-------------|
1069+
| Failure | The message failed to be processed and should be returned to the queue |
1070+
| Success | The pipeline must treat the message as having been processed successfully |
1071+
| SuccessWithResponse | The pipeline to treat the messagage as having been processed successfully, returning the response to the request/response invocation ([IRequestResponseBus<T>](../src/SlimMessageBus/RequestResponse/IRequestResponseBus.cs)) |
1072+
| Retry | Execute the pipeline again (any delay/jitter should be applied before returning from method) |
10701073

10711074
To enable SMB to recognize the error handler, it must be registered within the Microsoft Dependency Injection (MSDI) framework:
10721075

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
namespace SlimMessageBus.Host.AmazonSQS;
22

3-
public interface ISqsConsumerErrorHandler<in T> : IConsumerErrorHandler<T>
4-
{
5-
}
3+
public interface ISqsConsumerErrorHandler<in T> : IConsumerErrorHandler<T>;
4+
5+
public abstract class SqsConsumerErrorHandler<T> : ConsumerErrorHandler<T>;

src/SlimMessageBus.Host.AmazonSQS/GlobalUsings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
global using Microsoft.Extensions.DependencyInjection.Extensions;
88
global using Microsoft.Extensions.Logging;
99

10-
global using SlimMessageBus.Host.Serialization;
10+
global using SlimMessageBus.Host.Consumer.ErrorHandling;
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
namespace SlimMessageBus.Host.AzureEventHub;
22

3-
public interface IEventHubConsumerErrorHandler<in T> : IConsumerErrorHandler<T>
4-
{
5-
}
3+
public interface IEventHubConsumerErrorHandler<in T> : IConsumerErrorHandler<T>;
4+
5+
public abstract class EventHubConsumerErrorHandler<T> : ConsumerErrorHandler<T>;
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
global using Microsoft.Extensions.Logging;
1+
global using Azure.Messaging.EventHubs;
2+
global using Azure.Messaging.EventHubs.Producer;
3+
global using Azure.Storage.Blobs;
4+
5+
global using Microsoft.Extensions.Logging;
26

3-
global using SlimMessageBus.Host;
47
global using SlimMessageBus.Host.Collections;
8+
global using SlimMessageBus.Host.Consumer.ErrorHandling;
59
global using SlimMessageBus.Host.Services;
6-
7-
global using Azure.Messaging.EventHubs;
8-
global using Azure.Messaging.EventHubs.Producer;
9-
global using Azure.Storage.Blobs;
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
namespace SlimMessageBus.Host.AzureServiceBus;
22

3-
public interface IServiceBusConsumerErrorHandler<in T> : IConsumerErrorHandler<T>
4-
{
5-
}
3+
public interface IServiceBusConsumerErrorHandler<in T> : IConsumerErrorHandler<T>;
4+
5+
public abstract class ServiceBusConsumerErrorHandler<T> : ConsumerErrorHandler<T>;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
global using Microsoft.Extensions.Logging;
1+
global using Azure.Messaging.ServiceBus;
2+
global using Azure.Messaging.ServiceBus.Administration;
3+
4+
global using Microsoft.Extensions.Logging;
25

3-
global using SlimMessageBus.Host;
46
global using SlimMessageBus.Host.Collections;
7+
global using SlimMessageBus.Host.Consumer.ErrorHandling;
58
global using SlimMessageBus.Host.Services;
6-
7-
global using Azure.Messaging.ServiceBus;
8-
global using Azure.Messaging.ServiceBus.Administration;
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
namespace SlimMessageBus.Host.Kafka;
22

3-
public interface IKafkaConsumerErrorHandler<in T> : IConsumerErrorHandler<T>
4-
{
5-
}
3+
public interface IKafkaConsumerErrorHandler<in T> : IConsumerErrorHandler<T>;
4+
5+
public abstract class KafkaConsumerErrorHandler<T> : ConsumerErrorHandler<T>;

src/SlimMessageBus.Host.Kafka/GlobalUsings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
global using Microsoft.Extensions.Logging;
44

5-
global using SlimMessageBus.Host;
6-
global using SlimMessageBus.Host.Serialization;
75
global using SlimMessageBus.Host.Collections;
6+
global using SlimMessageBus.Host.Consumer.ErrorHandling;
7+
global using SlimMessageBus.Host.Serialization;
88
global using SlimMessageBus.Host.Services;

0 commit comments

Comments
 (0)