Skip to content

Commit 094dacd

Browse files
author
Bart Koelman
committed
Fixed: write stack trace in meta, instead of the full exception
1 parent 8534c41 commit 094dacd

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/JsonApiDotNetCore/Errors/InvalidModelStateException.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,14 @@ private static ErrorObject FromModelError(ModelError modelError, string attribut
116116

117117
if (includeExceptionStackTraceInErrors && modelError.Exception != null)
118118
{
119-
string[] stackTraceLines = modelError.Exception.Demystify().ToString().Split(Environment.NewLine);
119+
Exception exception = modelError.Exception.Demystify();
120+
string[] stackTraceLines = exception.StackTrace?.Split(Environment.NewLine);
120121

121-
error.Meta ??= new Dictionary<string, object>();
122-
error.Meta["StackTrace"] = stackTraceLines;
122+
if (!stackTraceLines.IsNullOrEmpty())
123+
{
124+
error.Meta ??= new Dictionary<string, object>();
125+
error.Meta["StackTrace"] = stackTraceLines;
126+
}
123127
}
124128

125129
return error;

src/JsonApiDotNetCore/Middleware/ExceptionHandler.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,13 @@ private void ApplyOptions(ErrorObject error, Exception exception)
101101

102102
if (resultException != null && _options.IncludeExceptionStackTraceInErrors)
103103
{
104-
string[] stackTraceLines = resultException.ToString().Split(Environment.NewLine);
104+
string[] stackTraceLines = exception.StackTrace?.Split(Environment.NewLine);
105105

106-
error.Meta ??= new Dictionary<string, object>();
107-
error.Meta["StackTrace"] = stackTraceLines;
106+
if (!stackTraceLines.IsNullOrEmpty())
107+
{
108+
error.Meta ??= new Dictionary<string, object>();
109+
error.Meta["StackTrace"] = stackTraceLines;
110+
}
108111
}
109112
}
110113
}

test/JsonApiDotNetCoreTests/IntegrationTests/ExceptionHandling/ExceptionHandlerTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
116116
error.Detail.Should().Be("Exception has been thrown by the target of an invocation.");
117117

118118
IEnumerable<string> stackTraceLines = ((JsonElement)error.Meta["stackTrace"]).EnumerateArray().Select(token => token.GetString());
119-
stackTraceLines.Should().ContainMatch("* System.InvalidOperationException: Article status could not be determined.*");
119+
stackTraceLines.Should().ContainMatch("*at object System.Reflection.*");
120120

121121
loggerFactory.Logger.Messages.Should().HaveCount(1);
122122
loggerFactory.Logger.Messages.Single().LogLevel.Should().Be(LogLevel.Error);

0 commit comments

Comments
 (0)