An exception occurs in web service and ASP.NET Web API returns the exception in JSON response, like following:
HTTP/1.1 500 Internal Server Error
Content-Type: application/json; charset=utf-8
{ "Message":"An error has occurred.",
"ExceptionMessage":"Incorrect syntax near 'FooBar'.",
"ExceptionType":"System.Data.SqlClient.SqlException",
"StackTrace":"at System.Web.Http.ApiController.blah.blah.blah" }
I would like to recreate the exception at client side. I want to convert the response to a SqlException object (in the example below) and then throw the exception. Some blogs have mentioned using Activator.CreateInstance() and Type.GetType() for creating an object in run-time with the type name in string and some have mentioned the use of dynamic. However, I am unable to figure how to use it properly. I would appreciate if someone can educate me. Thanks!
public class ExceptionResponse
{
public string Message { get; set; }
public string ExceptionType { get; set; }
public string ExceptionMessage { get; set; }
public string StackTrace { get; set; }
}
ExceptionResponse response = httpContent.ReadAsAsync<ExceptionResponse>().Result;
Type exceptionType = Type.GetType(response.ExceptionType);
throw Activator.CreateInstance(exceptionType, new object[]);
// Visual Studio indicates error: The type caught or throw must be derived from System.Exception
As I understand, you try to handle an exception which reciving by http as a JSON-formatted message. So, you can try to serialize (parse) HTTP response and create a new instance of ExceptionResponse. For example:
Your exception class will be like this:
Invoking:
At last, I found an example from Github showing how to recreate a SQL exception using reflection.