I'm working on a web api using dotnet core 2.2 and we want to catch serialization exception and return a 400 badRequest to distinguish from the validation errors 422UnprocessableEntity. We tried to create an exception handler
public void JsonSerializerExceptionHandler(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
{
args.ErrorContext.Handled = true;
var errorContext = args.ErrorContext;
if (errorContext == null)
{
return;
}
var error = errorContext.Error;
throw new SerializationException(error.Message, error.InnerException);
}
but when it throw it throw an other Exception of type InvalidOperationException with message
Current error context error is different to requested error.
We tried different approach but can't find a solution. Can someone help?