@@ -23,6 +23,8 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
23
23
/// </summary>
24
24
public class NewtonsoftJsonInputFormatter : TextInputFormatter , IInputFormatterExceptionPolicy
25
25
{
26
+ internal const string EnableSkipHandledError = "Microsoft.AspNetCore.Mvc.NewtonsoftJson.EnableSkipHandledError" ;
27
+
26
28
private readonly IArrayPool < char > _charPool ;
27
29
private readonly ILogger _logger ;
28
30
private readonly ObjectPoolProvider _objectPoolProvider ;
@@ -79,6 +81,8 @@ public NewtonsoftJsonInputFormatter(
79
81
_options = options ;
80
82
_jsonOptions = jsonOptions ;
81
83
84
+ SkipHandledErrorEnabled = AppContext . TryGetSwitch ( EnableSkipHandledError , out var enabled ) && enabled ;
85
+
82
86
SupportedEncodings . Add ( UTF8EncodingWithoutBOM ) ;
83
87
SupportedEncodings . Add ( UTF16EncodingLittleEndian ) ;
84
88
@@ -109,6 +113,9 @@ public virtual InputFormatterExceptionPolicy ExceptionPolicy
109
113
/// </remarks>
110
114
protected JsonSerializerSettings SerializerSettings { get ; }
111
115
116
+ // internal for testing
117
+ internal bool SkipHandledErrorEnabled { get ; set ; }
118
+
112
119
/// <inheritdoc />
113
120
public override async Task < InputFormatterResult > ReadRequestBodyAsync (
114
121
InputFormatterContext context ,
@@ -232,6 +239,13 @@ public override async Task<InputFormatterResult> ReadRequestBodyAsync(
232
239
233
240
void ErrorHandler ( object ? sender , Newtonsoft . Json . Serialization . ErrorEventArgs eventArgs )
234
241
{
242
+ // Skipping error, if it's already marked as handled
243
+ // This allows user code to implement its own error handling
244
+ if ( eventArgs . ErrorContext . Handled && SkipHandledErrorEnabled )
245
+ {
246
+ return ;
247
+ }
248
+
235
249
successful = false ;
236
250
237
251
// When ErrorContext.Path does not include ErrorContext.Member, add Member to form full path.
0 commit comments