Skip to content

Commit 2eb0aea

Browse files
author
Bart Koelman
authored
Pass boolean instead of options to exception constructor (#716)
1 parent 08ad28a commit 2eb0aea

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public virtual async Task<IActionResult> PostAsync([FromBody] T entity)
114114
throw new ResourceIdInPostRequestNotAllowedException();
115115

116116
if (_jsonApiOptions.ValidateModelState && !ModelState.IsValid)
117-
throw new InvalidModelStateException(ModelState, typeof(T), _jsonApiOptions);
117+
throw new InvalidModelStateException(ModelState, typeof(T), _jsonApiOptions.IncludeExceptionStackTraceInErrors);
118118

119119
entity = await _create.CreateAsync(entity);
120120

@@ -130,7 +130,7 @@ public virtual async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
130130
throw new InvalidRequestBodyException(null, null, null);
131131

132132
if (_jsonApiOptions.ValidateModelState && !ModelState.IsValid)
133-
throw new InvalidModelStateException(ModelState, typeof(T), _jsonApiOptions);
133+
throw new InvalidModelStateException(ModelState, typeof(T), _jsonApiOptions.IncludeExceptionStackTraceInErrors);
134134

135135
var updatedEntity = await _update.UpdateAsync(id, entity);
136136
return Ok(updatedEntity);

src/JsonApiDotNetCore/Exceptions/InvalidModelStateException.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Linq;
44
using System.Net;
55
using System.Reflection;
6-
using JsonApiDotNetCore.Configuration;
76
using JsonApiDotNetCore.Models;
87
using JsonApiDotNetCore.Models.JsonApiDocuments;
98
using Microsoft.AspNetCore.Mvc.ModelBinding;
@@ -17,23 +16,25 @@ public class InvalidModelStateException : Exception
1716
{
1817
public IList<Error> Errors { get; }
1918

20-
public InvalidModelStateException(ModelStateDictionary modelState, Type resourceType, IJsonApiOptions options)
19+
public InvalidModelStateException(ModelStateDictionary modelState, Type resourceType,
20+
bool includeExceptionStackTraceInErrors)
2121
{
22-
Errors = FromModelState(modelState, resourceType, options);
22+
Errors = FromModelState(modelState, resourceType, includeExceptionStackTraceInErrors);
2323
}
2424

2525
private static List<Error> FromModelState(ModelStateDictionary modelState, Type resourceType,
26-
IJsonApiOptions options)
26+
bool includeExceptionStackTraceInErrors)
2727
{
2828
List<Error> errors = new List<Error>();
2929

3030
foreach (var pair in modelState.Where(x => x.Value.Errors.Any()))
3131
{
3232
var propertyName = pair.Key;
3333
PropertyInfo property = resourceType.GetProperty(propertyName);
34-
34+
3535
// TODO: Need access to ResourceContext here, in order to determine attribute name when not explicitly set.
36-
string attributeName = property?.GetCustomAttribute<AttrAttribute>().PublicAttributeName ?? property?.Name;
36+
string attributeName =
37+
property?.GetCustomAttribute<AttrAttribute>().PublicAttributeName ?? property?.Name;
3738

3839
foreach (var modelError in pair.Value.Errors)
3940
{
@@ -43,27 +44,30 @@ private static List<Error> FromModelState(ModelStateDictionary modelState, Type
4344
}
4445
else
4546
{
46-
errors.Add(FromModelError(modelError, attributeName, options));
47+
errors.Add(FromModelError(modelError, attributeName, includeExceptionStackTraceInErrors));
4748
}
4849
}
4950
}
5051

5152
return errors;
5253
}
5354

54-
private static Error FromModelError(ModelError modelError, string attributeName, IJsonApiOptions options)
55+
private static Error FromModelError(ModelError modelError, string attributeName,
56+
bool includeExceptionStackTraceInErrors)
5557
{
5658
var error = new Error(HttpStatusCode.UnprocessableEntity)
5759
{
5860
Title = "Input validation failed.",
5961
Detail = modelError.ErrorMessage,
60-
Source = attributeName == null ? null : new ErrorSource
61-
{
62-
Pointer = $"/data/attributes/{attributeName}"
63-
}
62+
Source = attributeName == null
63+
? null
64+
: new ErrorSource
65+
{
66+
Pointer = $"/data/attributes/{attributeName}"
67+
}
6468
};
6569

66-
if (options.IncludeExceptionStackTraceInErrors && modelError.Exception != null)
70+
if (includeExceptionStackTraceInErrors && modelError.Exception != null)
6771
{
6872
error.Meta.IncludeExceptionStackTrace(modelError.Exception);
6973
}

0 commit comments

Comments
 (0)