33using System . Linq ;
44using System . Net ;
55using System . Reflection ;
6- using JsonApiDotNetCore . Configuration ;
76using JsonApiDotNetCore . Models ;
87using JsonApiDotNetCore . Models . JsonApiDocuments ;
98using 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