I am presently encountering an issue with catching errors in ASP.NET's JSON serialization middleware. Specifically, with the following code.
var apiEntity = ModelSerializer.ToApi(entity);
try
{
var ok = Ok(apiEntity);
return ok;
}
catch
{
return BadRequest(new ApiError { Message = "Error" });
}
All runs fine. However, the 'catch' block is never activated, and the wrong error propagates.
I am getting this error:
System.ArgumentException: The JSON value of length 733746981 is too large and not supported.
I want to catch this error so that the correct error is sent. How can I catch this error in the middleware?
ToApian async method? Irrespective to that, are you sure putting 700 MB of data in a json is the right thing to do?