public override void Handle(ExceptionHandlerContext context) { var result = new HttpActionResult(); result.Request = context.Request; if (context.Exception is UnauthorizedAccessException) { context.Result = GetExceptionResult(context, HttpStatusCode.Unauthorized); } else { context.Result = GetExceptionResult(context, HttpStatusCode.BadRequest); } }
private IHttpActionResult GetExceptionResult(ExceptionHandlerContext context, HttpStatusCode statusCode) { var result = new HttpActionResult(); result.Request = context.Request; var returnJsonString = JsonConvert.SerializeObject(new { error = statusCode.ToString().ToLower(), error_description = context.Exception.Message }); result.Response = new HttpResponseMessage(statusCode) { Content = new StringContent(returnJsonString), ReasonPhrase = statusCode.ToString() }; result.Response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return(result); }