Skip to content

Pass boolean instead of options to exception constructor #716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public virtual async Task<IActionResult> PostAsync([FromBody] T entity)
throw new ResourceIdInPostRequestNotAllowedException();

if (_jsonApiOptions.ValidateModelState && !ModelState.IsValid)
throw new InvalidModelStateException(ModelState, typeof(T), _jsonApiOptions);
throw new InvalidModelStateException(ModelState, typeof(T), _jsonApiOptions.IncludeExceptionStackTraceInErrors);

entity = await _create.CreateAsync(entity);

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

if (_jsonApiOptions.ValidateModelState && !ModelState.IsValid)
throw new InvalidModelStateException(ModelState, typeof(T), _jsonApiOptions);
throw new InvalidModelStateException(ModelState, typeof(T), _jsonApiOptions.IncludeExceptionStackTraceInErrors);

var updatedEntity = await _update.UpdateAsync(id, entity);
return Ok(updatedEntity);
Expand Down
30 changes: 17 additions & 13 deletions src/JsonApiDotNetCore/Exceptions/InvalidModelStateException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using System.Net;
using System.Reflection;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCore.Models.JsonApiDocuments;
using Microsoft.AspNetCore.Mvc.ModelBinding;
Expand All @@ -17,23 +16,25 @@ public class InvalidModelStateException : Exception
{
public IList<Error> Errors { get; }

public InvalidModelStateException(ModelStateDictionary modelState, Type resourceType, IJsonApiOptions options)
public InvalidModelStateException(ModelStateDictionary modelState, Type resourceType,
bool includeExceptionStackTraceInErrors)
{
Errors = FromModelState(modelState, resourceType, options);
Errors = FromModelState(modelState, resourceType, includeExceptionStackTraceInErrors);
}

private static List<Error> FromModelState(ModelStateDictionary modelState, Type resourceType,
IJsonApiOptions options)
bool includeExceptionStackTraceInErrors)
{
List<Error> errors = new List<Error>();

foreach (var pair in modelState.Where(x => x.Value.Errors.Any()))
{
var propertyName = pair.Key;
PropertyInfo property = resourceType.GetProperty(propertyName);

// TODO: Need access to ResourceContext here, in order to determine attribute name when not explicitly set.
string attributeName = property?.GetCustomAttribute<AttrAttribute>().PublicAttributeName ?? property?.Name;
string attributeName =
property?.GetCustomAttribute<AttrAttribute>().PublicAttributeName ?? property?.Name;

foreach (var modelError in pair.Value.Errors)
{
Expand All @@ -43,27 +44,30 @@ private static List<Error> FromModelState(ModelStateDictionary modelState, Type
}
else
{
errors.Add(FromModelError(modelError, attributeName, options));
errors.Add(FromModelError(modelError, attributeName, includeExceptionStackTraceInErrors));
}
}
}

return errors;
}

private static Error FromModelError(ModelError modelError, string attributeName, IJsonApiOptions options)
private static Error FromModelError(ModelError modelError, string attributeName,
bool includeExceptionStackTraceInErrors)
{
var error = new Error(HttpStatusCode.UnprocessableEntity)
{
Title = "Input validation failed.",
Detail = modelError.ErrorMessage,
Source = attributeName == null ? null : new ErrorSource
{
Pointer = $"/data/attributes/{attributeName}"
}
Source = attributeName == null
? null
: new ErrorSource
{
Pointer = $"/data/attributes/{attributeName}"
}
};

if (options.IncludeExceptionStackTraceInErrors && modelError.Exception != null)
if (includeExceptionStackTraceInErrors && modelError.Exception != null)
{
error.Meta.IncludeExceptionStackTrace(modelError.Exception);
}
Expand Down