Skip to content

Commit 0aa077b

Browse files
author
Bart Koelman
authored
Fixed: hide duplicate stacktrace for ModelState errors (#905)
* Fixed: hide stacktrace for ModelState errors * Avoid to Demystify the same exception multiple times
1 parent 63ed35e commit 0aa077b

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

src/JsonApiDotNetCore/Errors/InvalidModelStateException.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
using System.Net;
56
using System.Reflection;
@@ -80,7 +81,7 @@ private static Error FromModelError(ModelError modelError, string attributeName,
8081

8182
if (includeExceptionStackTraceInErrors && modelError.Exception != null)
8283
{
83-
error.Meta.IncludeExceptionStackTrace(modelError.Exception);
84+
error.Meta.IncludeExceptionStackTrace(modelError.Exception.Demystify());
8485
}
8586

8687
return error;

src/JsonApiDotNetCore/Middleware/ExceptionHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ protected virtual ErrorDocument CreateErrorDocument(Exception exception)
9999

100100
private void ApplyOptions(Error error, Exception exception)
101101
{
102-
error.Meta.IncludeExceptionStackTrace(_options.IncludeExceptionStackTraceInErrors ? exception : null);
102+
Exception resultException = exception is InvalidModelStateException ? null : exception;
103+
104+
error.Meta.IncludeExceptionStackTrace(_options.IncludeExceptionStackTraceInErrors ? resultException : null);
103105
}
104106
}
105107
}

src/JsonApiDotNetCore/Serialization/Objects/ErrorMeta.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics;
43
using Newtonsoft.Json;
54

65
namespace JsonApiDotNetCore.Serialization.Objects
@@ -21,7 +20,7 @@ public void IncludeExceptionStackTrace(Exception exception)
2120
}
2221
else
2322
{
24-
Data["StackTrace"] = exception.Demystify().ToString()
23+
Data["StackTrace"] = exception.ToString()
2524
.Split("\n", int.MaxValue, StringSplitOptions.RemoveEmptyEntries);
2625
}
2726
}

0 commit comments

Comments
 (0)