Skip to content

Commit

Permalink
Move code to avoid throw from nested catch
Browse files Browse the repository at this point in the history
  • Loading branch information
bording committed Mar 5, 2024
1 parent efa959e commit 8933c0c
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions src/NServiceBus.AwsLambda.SQS/AwsLambdaEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,27 +404,18 @@ async Task ProcessMessageWithInMemoryRetries(Dictionary<string, string> headers,
catch (Exception ex) when (!ex.IsCausedBy(cancellationToken))
{
immediateProcessingAttempts++;
ErrorHandleResult errorHandlerResult;

try
{
var errorContext = new ErrorContext(
ex,
new Dictionary<string, string>(headers),
nativeMessageId,
body,
transportTransaction,
immediateProcessingAttempts,
receiveQueueAddress,
context);

errorHandlerResult = await ProcessFailedMessage(errorContext, lambdaContext, cancellationToken).ConfigureAwait(false);
}
catch (Exception onErrorEx) when (!onErrorEx.IsCausedBy(cancellationToken))
{
Logger.Warn($"Failed to execute recoverability policy for message with native ID: `{nativeMessageId}`", onErrorEx);
throw;
}
var errorContext = new ErrorContext(
ex,
new Dictionary<string, string>(headers),
nativeMessageId,
body,
transportTransaction,
immediateProcessingAttempts,
receiveQueueAddress,
context);

var errorHandlerResult = await ProcessFailedMessage(errorContext, lambdaContext, nativeMessageId, cancellationToken).ConfigureAwait(false);

errorHandled = errorHandlerResult == ErrorHandleResult.Handled;
}
Expand All @@ -435,17 +426,26 @@ async Task Process(MessageContext messageContext, ILambdaContext executionContex
{
await InitializeEndpointIfNecessary(executionContext, cancellationToken)
.ConfigureAwait(false);

await pipeline.PushMessage(messageContext, cancellationToken)
.ConfigureAwait(false);
}

async Task<ErrorHandleResult> ProcessFailedMessage(ErrorContext errorContext, ILambdaContext executionContext, CancellationToken cancellationToken)
async Task<ErrorHandleResult> ProcessFailedMessage(ErrorContext errorContext, ILambdaContext executionContext, string nativeMessageId, CancellationToken cancellationToken)
{
await InitializeEndpointIfNecessary(executionContext, cancellationToken)
.ConfigureAwait(false);
try
{
await InitializeEndpointIfNecessary(executionContext, cancellationToken)
.ConfigureAwait(false);

return await pipeline.PushFailedMessage(errorContext, cancellationToken)
.ConfigureAwait(false);
return await pipeline.PushFailedMessage(errorContext, cancellationToken)
.ConfigureAwait(false);
}
catch (Exception onErrorEx) when (!onErrorEx.IsCausedBy(cancellationToken))
{
Logger.Warn($"Failed to execute recoverability policy for message with native ID: `{nativeMessageId}`", onErrorEx);
throw;
}
}

async Task DeleteMessageAndBodyIfRequired(Message message, string messageS3BodyKey, CancellationToken cancellationToken)
Expand Down

1 comment on commit 8933c0c

@andreasohlund
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Please sign in to comment.