Skip to content

Commit f502628

Browse files
author
Bart Koelman
committed
Fixed: do not duplicate the list of JSON:API errors in meta stack trace, but do write them when logging
1 parent 8534c41 commit f502628

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

src/JsonApiDotNetCore/Errors/InvalidModelStateException.cs

Lines changed: 7 additions & 3 deletions
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.ToString().Split(Environment.NewLine);
120121

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

125129
return error;

src/JsonApiDotNetCore/Errors/JsonApiException.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ public class JsonApiException : Exception
2424

2525
public IReadOnlyList<ErrorObject> Errors { get; }
2626

27-
public override string Message => $"Errors = {JsonSerializer.Serialize(Errors, SerializerOptions)}";
28-
2927
public JsonApiException(ErrorObject error, Exception innerException = null)
3028
: base(null, innerException)
3129
{
@@ -42,5 +40,10 @@ public JsonApiException(IEnumerable<ErrorObject> errors, Exception innerExceptio
4240

4341
Errors = errorList;
4442
}
43+
44+
public string GetSummary()
45+
{
46+
return $"{nameof(JsonApiException)}: Errors = {JsonSerializer.Serialize(Errors, SerializerOptions)}";
47+
}
4548
}
4649
}

src/JsonApiDotNetCore/Middleware/ExceptionHandler.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected virtual string GetLogMessage(Exception exception)
6767
{
6868
ArgumentGuard.NotNull(exception, nameof(exception));
6969

70-
return exception.Message;
70+
return exception is JsonApiException jsonApiException ? jsonApiException.GetSummary() : exception.Message;
7171
}
7272

7373
protected virtual Document CreateErrorDocument(Exception exception)
@@ -103,8 +103,11 @@ private void ApplyOptions(ErrorObject error, Exception exception)
103103
{
104104
string[] stackTraceLines = resultException.ToString().Split(Environment.NewLine);
105105

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

test/JsonApiDotNetCoreTests/IntegrationTests/ExceptionHandling/ExceptionHandlerTests.cs

Lines changed: 1 addition & 1 deletion
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)