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

Populate required field Message with n/a if it is empty #2857

Merged
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 @@ -185,6 +185,19 @@ public void DoesNotTrimShortExceptionMessages()
Assert.AreEqual(5, expDetails.message.Length);
}

[TestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
public void ProvidesDefaultMessage(string message)
TimothyMothra marked this conversation as resolved.
Show resolved Hide resolved
NeilMountford marked this conversation as resolved.
Show resolved Hide resolved
{
var exp = new ExceptionWithMessageOverride(message);

ExceptionDetails expDetails = ExceptionConverter.ConvertToExceptionDetails(exp, null);

Assert.AreEqual("n/a", expDetails.message);
}

[MethodImplAttribute(MethodImplOptions.NoInlining)]
private Exception CreateException(int numberOfStackpoints)
{
Expand Down Expand Up @@ -212,5 +225,20 @@ private void FailedFunction(int numberOfStackpoints)

throw new AggregateException("exception message");
}

/// <summary>
/// Overrides the Message property of the base exception, so that null messages are
/// returned as null, rather than a default, e.g. "Exception of type 'System.Exception' was thrown".
/// </summary>
private class ExceptionWithMessageOverride : Exception
{
public ExceptionWithMessageOverride(string message)
: base(message)
{
Message = message;
}

public override string Message { get; }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Microsoft.ApplicationInsights.Extensibility.Implementation.External
{
using System;
using Microsoft.ApplicationInsights.DataContracts;

/// <summary>
/// Additional implementation for ExceptionDetails.
Expand All @@ -21,7 +22,7 @@ internal static ExceptionDetails CreateWithoutStackInfo(Exception exception, Exc
{
id = exception.GetHashCode(),
typeName = exception.GetType().FullName,
message = exception.Message,
message = Utils.PopulateRequiredNonWhitespaceStringValue(exception.Message, "message", typeof(ExceptionTelemetry).FullName),
};

if (parentExceptionDetails != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ public static string PopulateRequiredStringValue(string value, string parameterN
return value;
}

/// <summary>
/// Validates the string and if null or empty populates it with '$parameterName is a required field for $telemetryType' value.
/// </summary>
public static string PopulateRequiredNonWhitespaceStringValue(string value, string parameterName, string telemetryType)
{
if (value.IsNullOrWhiteSpace())
{
CoreEventSource.Log.PopulateRequiredStringWithValue(parameterName, telemetryType);
return "n/a";
}

return value;
}

/// <summary>
/// Returns default Timespan value if not a valid Timespan.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## VNext
- [Populate required field Message with "n/a" if it is empty](https://github.com/microsoft/ApplicationInsights-dotnet/issues/1066)

## Version 2.22.0
- no changes since beta.
Expand Down
Loading