Skip to content

Commit

Permalink
Ensure the deadLetterErrorDescription is within the allowed size
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad committed Nov 7, 2023
1 parent a66c850 commit e6b9ca5
Showing 1 changed file with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace TeacherIdentity.AuthServer.Notifications.WebHooks;

public sealed class ServiceBusWebHookNotificationPublisher : WebHookNotificationPublisher, IAsyncDisposable, IHostedService
{
private const int MaxDeadLetterDescriptionLength = 4096;

private static readonly TimeSpan[] _retryIntervals = new[]
{
TimeSpan.FromSeconds(30),
Expand Down Expand Up @@ -110,7 +112,10 @@ private async Task ProcessMessage(ProcessMessageEventArgs arg)
{
_logger.LogError(ex, "Failed processing message after {RetryNumber} retries.", retryNumber);

await arg.DeadLetterMessageAsync(message, deadLetterReason: "Could not deliver web hook", deadLetterErrorDescription: ex.ToString());
await arg.DeadLetterMessageAsync(
message,
deadLetterReason: "Could not deliver web hook",
deadLetterErrorDescription: ex.ToString().Truncate(MaxDeadLetterDescriptionLength);

Check failure on line 118 in dotnet-authserver/src/TeacherIdentity.AuthServer/Notifications/WebHooks/ServiceBusWebHookNotificationPublisher.cs

View workflow job for this annotation

GitHub Actions / Build & test

) expected

Check failure on line 118 in dotnet-authserver/src/TeacherIdentity.AuthServer/Notifications/WebHooks/ServiceBusWebHookNotificationPublisher.cs

View workflow job for this annotation

GitHub Actions / Build & test

) expected
return;
}

Expand Down Expand Up @@ -155,3 +160,20 @@ private class ApplicationPropertiesKeys
public const string RetryNumber = "RetryNumber";
}
}

file static class StringExtensions
{
private const string TruncatedSuffix = "...";

public static string Truncate(this string str, int length)
{
if (length <= TruncatedSuffix.Length)
{
throw new ArgumentOutOfRangeException(nameof(length));
}

return str.Length > length ?
str[..(length - TruncatedSuffix.Length + 1)] + TruncatedSuffix :
str;
}
}

0 comments on commit e6b9ca5

Please sign in to comment.