Having the following code:
return Request.CreateErrorResponse(
HttpStatusCode.BadRequest,
new ArgumentNullException("searchCriteria"));
I expect to be able to read ExceptionType back:
SearchCriteria searchCriteria = null;
HttpResponseMessage message = controller.Do(searchCriteria);
ObjectContent<HttpError> content = message.Content as ObjectContent<HttpError>;
HttpError error = (HttpError)content.Value;
string exceptionType = error.ExceptionType;
but the last line gives always `null`? Why is that?
Comments: In a test case, they key is that we look at the requestContext.IncludeErrorDetails Boolean property. Because of the way you are initializing the test, this property is not set correctly. In the runtime it's caching the value earlier, but in the test it's too late. So the fix to the test would be to grab the request context off the request, and set the includeerrordetails to true. ``` HttpRequestContext requestContext = (HttpRequestContext)(controller.Request.Properties[System.Web.Http.Hosting.HttpPropertyKeys.RequestContextKey]); requestContext.IncludeErrorDetail = true; ``` or ``` request.SetRequestContext(new HttpRequestContext() { IncludeErrorDetail = true }); ```
return Request.CreateErrorResponse(
HttpStatusCode.BadRequest,
new ArgumentNullException("searchCriteria"));
I expect to be able to read ExceptionType back:
SearchCriteria searchCriteria = null;
HttpResponseMessage message = controller.Do(searchCriteria);
ObjectContent<HttpError> content = message.Content as ObjectContent<HttpError>;
HttpError error = (HttpError)content.Value;
string exceptionType = error.ExceptionType;
but the last line gives always `null`? Why is that?
Comments: In a test case, they key is that we look at the requestContext.IncludeErrorDetails Boolean property. Because of the way you are initializing the test, this property is not set correctly. In the runtime it's caching the value earlier, but in the test it's too late. So the fix to the test would be to grab the request context off the request, and set the includeerrordetails to true. ``` HttpRequestContext requestContext = (HttpRequestContext)(controller.Request.Properties[System.Web.Http.Hosting.HttpPropertyKeys.RequestContextKey]); requestContext.IncludeErrorDetail = true; ``` or ``` request.SetRequestContext(new HttpRequestContext() { IncludeErrorDetail = true }); ```