Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce fault header keys #6876

Merged
merged 6 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,14 @@ namespace NServiceBus.Faults
}
public static class FaultsHeaderKeys
{
public const string ExceptionType = "NServiceBus.ExceptionInfo.ExceptionType";
public const string FailedQ = "NServiceBus.FailedQ";
public const string HelpLink = "NServiceBus.ExceptionInfo.HelpLink";
public const string InnerExceptionType = "NServiceBus.ExceptionInfo.InnerExceptionType";
public const string Message = "NServiceBus.ExceptionInfo.Message";
public const string Source = "NServiceBus.ExceptionInfo.Source";
public const string StackTrace = "NServiceBus.ExceptionInfo.StackTrace";
public const string TimeOfFailure = "NServiceBus.TimeOfFailure";
}
public class ImmediateRetryMessage
{
Expand Down
71 changes: 0 additions & 71 deletions src/NServiceBus.Core/Recoverability/FaultMetadataExtractor.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace NServiceBus
{
using System;
using System.Collections;
using System.Collections.Generic;
using Faults;
using Transport;

class FaultMetadataExtractor(Dictionary<string, string> staticFaultMetadata, Action<Dictionary<string, string>> headerCustomizations)
{
public Dictionary<string, string> Extract(ErrorContext errorContext)
{
var metadata = new Dictionary<string, string>(staticFaultMetadata)
{
[FaultsHeaderKeys.FailedQ] = errorContext.ReceiveAddress
};

SetExceptionMetadata(metadata, errorContext.Exception);

headerCustomizations(metadata);

return metadata;
}

static void SetExceptionMetadata(Dictionary<string, string> headers, Exception e)
{
headers[FaultsHeaderKeys.ExceptionType] = e.GetType().FullName;

if (e.InnerException != null)
{
headers[FaultsHeaderKeys.InnerExceptionType] = e.InnerException.GetType().FullName;
}

headers[FaultsHeaderKeys.HelpLink] = e.HelpLink;
headers[FaultsHeaderKeys.Message] = Truncate(e.GetMessage(), 16384);
headers[FaultsHeaderKeys.Source] = e.Source;
headers[FaultsHeaderKeys.StackTrace] = e.ToString();
headers[FaultsHeaderKeys.TimeOfFailure] = DateTimeOffsetHelper.ToWireFormattedString(DateTimeOffset.UtcNow);

foreach (DictionaryEntry entry in e.Data)
{
if (entry.Value == null)
{
continue;
}
headers[$"{FaultsHeaderKeys.ExceptionInfoDataPrefix}{entry.Key}"] = entry.Value.ToString();
}
}

static string Truncate(string value, int maxLength) =>
string.IsNullOrEmpty(value)
? value
: value.Length <= maxLength
? value
: value[..maxLength];
}
}
40 changes: 40 additions & 0 deletions src/NServiceBus.Core/Recoverability/Faults/FaultsHeaderKeys.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace NServiceBus.Faults
{
using System;

/// <summary>
/// Class holding keys to message headers for faults.
/// </summary>
Expand All @@ -9,5 +11,43 @@ public static class FaultsHeaderKeys
/// Header key for setting/getting the queue at which the message processing failed.
/// </summary>
public const string FailedQ = "NServiceBus.FailedQ";

/// <summary>
/// Header key for setting/getting the exception type that caused the message processing to fail.
/// </summary>
public const string ExceptionType = $"{ExceptionInfoPrefix}ExceptionType";

/// <summary>
/// Header key for setting/getting the inner exception type that caused the message processing to fail.
/// </summary>
public const string InnerExceptionType = $"{ExceptionInfoPrefix}InnerExceptionType";

/// <summary>
/// Header key for setting/getting the <see cref="Exception.HelpLink"/> of the exception that caused the message processing to fail.
/// </summary>
public const string HelpLink = $"{ExceptionInfoPrefix}HelpLink";

/// <summary>
/// Header key for setting/getting the <see cref="Exception.Message"/> of the exception that caused the message processing to fail.
/// </summary>
public const string Message = $"{ExceptionInfoPrefix}Message";

/// <summary>
/// Header key for setting/getting the <see cref="Exception.Source"/> of the exception that caused the message processing to fail.
/// </summary>
public const string Source = $"{ExceptionInfoPrefix}Source";

/// <summary>
/// Header key for setting/getting the <see cref="Exception.StackTrace"/> of the exception that caused the message processing to fail.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to stick to the same conventions of XML documentation, even though the Headers file uses a different one.

/// </summary>
public const string StackTrace = $"{ExceptionInfoPrefix}StackTrace";

/// <summary>
/// Header key for setting/getting the normalized time of the failure.
/// </summary>
public const string TimeOfFailure = "NServiceBus.TimeOfFailure";

const string ExceptionInfoPrefix = "NServiceBus.ExceptionInfo.";
internal const string ExceptionInfoDataPrefix = $"{ExceptionInfoPrefix}Data.";
}
}