Skip to content

Commit 23bedc3

Browse files
author
Bart Koelman
committed
fixup-stacktrace
1 parent e9f43fd commit 23bedc3

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

src/JsonApiDotNetCore/Errors/InvalidModelStateException.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ private static ErrorObject FromModelError(ModelError modelError, string attribut
117117
if (includeExceptionStackTraceInErrors && modelError.Exception != null)
118118
{
119119
Exception exception = modelError.Exception.Demystify();
120-
string[] stackTraceLines = exception.StackTrace?.Split(Environment.NewLine);
120+
string[] stackTraceLines = exception.ToString().Split(Environment.NewLine);
121121

122-
if (!stackTraceLines.IsNullOrEmpty())
122+
if (stackTraceLines.Any())
123123
{
124124
error.Meta ??= new Dictionary<string, object>();
125125
error.Meta["StackTrace"] = stackTraceLines;

src/JsonApiDotNetCore/Errors/JsonApiException.cs

+5-2
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

+3-3
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)
@@ -104,9 +104,9 @@ protected virtual Document CreateErrorDocument(Exception exception)
104104

105105
private void IncludeStackTraces(Exception exception, IList<ErrorObject> errors)
106106
{
107-
string[] stackTraceLines = exception.StackTrace?.Split(Environment.NewLine);
107+
string[] stackTraceLines = exception.ToString().Split(Environment.NewLine);
108108

109-
if (!stackTraceLines.IsNullOrEmpty())
109+
if (stackTraceLines.Any())
110110
{
111111
foreach (ErrorObject error in errors)
112112
{

0 commit comments

Comments
 (0)