1

In my ASP.NET Core WebAPI controller, when I pass an exception object to JsonResult, it doesn't seem to recognize any of the exception properties and populate them accordingly (it just returns the 500 with no text). However, when I new-up an anonymous object and pass that in, it returns the 500 with what you would expect. It seems as though it has a big problem mapping an exception object directly and in its entirety. I'm wondering if there is a configuration issue/option.

[HttpGet]
public IActionResult GetSomething()
{
    try
    {
        throw new Exception("hey");
    }
    catch (Exception e)
    {
        // the following does not work as expected
        return new JsonResult(e)
        {
            StatusCode = StatusCodes.Status500InternalServerError
        };

        // the following works as expected
        return new JsonResult(new { Message = e.Message })
        {
            StatusCode = StatusCodes.Status500InternalServerError
        };
    }
}
2
  • what version of ASP.Net core do you use? Commented Jan 5, 2020 at 15:12
  • @Piotr, ASP.NET Core 3.0 Commented Jan 5, 2020 at 15:15

2 Answers 2

1

When you pass whole Exception object, it tries to serialize it to Json, including all nested object. It is possible that serialization just exceeds maximum depth and throws an exception.
Anyway I would say this is not a good practice to return the whole Exception back to the client as you may expose sensitive information. It would be better to send only what client may need.

Sign up to request clarification or add additional context in comments.

4 Comments

@CINCHAPPS there is no difference in creating new class inheriting Exception, you still have all your properties that may exceed max depth, especially if you have cross-referenced properties (parent has child and child has parent, this is infinite reference that serializer can not handle)
I went ahead and dug a little further by creating another class (ExceptionMapper) that maps some of the Exception properties (passing the exception in the constructor of the new class, copying the properties from the Exception object to the new ExceptionMapper object). The process breaks down when it is tries to duplicate the TargetSite property, so I would guess something in the JSON serializer can't handle dealing with a nullable MethodBase which isn't so surprising
I made an error in my comment. I didn't mean to say I inherited Exception. It does not inherit from anything. It handles Message, StackTrace, Source, InnerException, Data, HResult, and HelpLink without issue.
@CINCHAPPS got it, if you are using property MethodBase TargetSite in your new class - it might be the case I described above in comment related to cross-referensing
0

The following seems to be a reasonable solution. Build your own class that accepts an Exception object in the constructor and avoid trying to copy the TargetSite property.

return new JsonResult(new { Exception = new ExceptionMapper(e) })
{
    StatusCode = StatusCodes.Status500InternalServerError
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.