3
3
using System . Linq ;
4
4
using System . Net ;
5
5
using System . Reflection ;
6
- using JsonApiDotNetCore . Configuration ;
7
6
using JsonApiDotNetCore . Models ;
8
7
using JsonApiDotNetCore . Models . JsonApiDocuments ;
9
8
using Microsoft . AspNetCore . Mvc . ModelBinding ;
@@ -17,23 +16,25 @@ public class InvalidModelStateException : Exception
17
16
{
18
17
public IList < Error > Errors { get ; }
19
18
20
- public InvalidModelStateException ( ModelStateDictionary modelState , Type resourceType , IJsonApiOptions options )
19
+ public InvalidModelStateException ( ModelStateDictionary modelState , Type resourceType ,
20
+ bool includeExceptionStackTraceInErrors )
21
21
{
22
- Errors = FromModelState ( modelState , resourceType , options ) ;
22
+ Errors = FromModelState ( modelState , resourceType , includeExceptionStackTraceInErrors ) ;
23
23
}
24
24
25
25
private static List < Error > FromModelState ( ModelStateDictionary modelState , Type resourceType ,
26
- IJsonApiOptions options )
26
+ bool includeExceptionStackTraceInErrors )
27
27
{
28
28
List < Error > errors = new List < Error > ( ) ;
29
29
30
30
foreach ( var pair in modelState . Where ( x => x . Value . Errors . Any ( ) ) )
31
31
{
32
32
var propertyName = pair . Key ;
33
33
PropertyInfo property = resourceType . GetProperty ( propertyName ) ;
34
-
34
+
35
35
// 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 ;
37
38
38
39
foreach ( var modelError in pair . Value . Errors )
39
40
{
@@ -43,27 +44,30 @@ private static List<Error> FromModelState(ModelStateDictionary modelState, Type
43
44
}
44
45
else
45
46
{
46
- errors . Add ( FromModelError ( modelError , attributeName , options ) ) ;
47
+ errors . Add ( FromModelError ( modelError , attributeName , includeExceptionStackTraceInErrors ) ) ;
47
48
}
48
49
}
49
50
}
50
51
51
52
return errors ;
52
53
}
53
54
54
- private static Error FromModelError ( ModelError modelError , string attributeName , IJsonApiOptions options )
55
+ private static Error FromModelError ( ModelError modelError , string attributeName ,
56
+ bool includeExceptionStackTraceInErrors )
55
57
{
56
58
var error = new Error ( HttpStatusCode . UnprocessableEntity )
57
59
{
58
60
Title = "Input validation failed." ,
59
61
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
+ }
64
68
} ;
65
69
66
- if ( options . IncludeExceptionStackTraceInErrors && modelError . Exception != null )
70
+ if ( includeExceptionStackTraceInErrors && modelError . Exception != null )
67
71
{
68
72
error . Meta . IncludeExceptionStackTrace ( modelError . Exception ) ;
69
73
}
0 commit comments