{"id":1258,"date":"2018-09-07T15:17:13","date_gmt":"2018-09-07T14:17:13","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=1258"},"modified":"2018-09-07T15:17:13","modified_gmt":"2018-09-07T14:17:13","slug":"c-system-web-http-filter-exceptionhandlingattribute","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-system-web-http-filter-exceptionhandlingattribute\/","title":{"rendered":"C# System.Web.Http Filter ExceptionHandlingAttribute"},"content":{"rendered":"<pre class=\"lang:default decode:true  \">    public class ExceptionHandlingAttribute : ExceptionFilterAttribute\r\n    {\r\n\r\n        public override void OnException(HttpActionExecutedContext context)\r\n        {\r\n            var exception = context.Exception.GetBaseException();\r\n            var method = context.Request.Method;\r\n            var url = context.Request.RequestUri;\r\n            var controllerName = context.ActionContext?.ControllerContext?.ControllerDescriptor?.ControllerName;\r\n            var actionName = context.ActionContext?.ActionDescriptor?.ActionName;\r\n\r\n            LoggingHelper.Logger.Error($\"{method} {url}\\r\\n{controllerName}.{actionName}()\", exception);\r\n\r\n#if !DEBUG\r\n            var msg = \"An unhandled error occurred.\";                \r\n            string stack = null;\r\n#else\r\n            var msg = exception.Message;\r\n            string stack = context.Exception.StackTrace;\r\n#endif\r\n\r\n            var apiError = new ApiError(msg) { Detail = stack };\r\n            context.Response.StatusCode = HttpStatusCode.InternalServerError;\r\n            context.Response.Content =\r\n                new StringContent(JsonConvert.SerializeObject(apiError), Encoding.UTF8, \"application\/json\");\r\n        }\r\n    }\r\n\r\n    public class ApiError\r\n    {\r\n        public string Message { get; set; }\r\n        public bool IsError { get; set; }\r\n        public string Detail { get; set; }\r\n\r\n        public ApiError(string message)\r\n        {\r\n            this.Message = message;\r\n            IsError = true;\r\n        }\r\n    }<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>public class ExceptionHandlingAttribute : ExceptionFilterAttribute { public override void OnException(HttpActionExecutedContext context) { var exception = context.Exception.GetBaseException(); var method = context.Request.Method; var url = context.Request.RequestUri; var controllerName = context.ActionContext?.ControllerContext?.ControllerDescriptor?.ControllerName; var actionName = context.ActionContext?.ActionDescriptor?.ActionName; LoggingHelper.Logger.Error($&#8221;{method} {url}\\r\\n{controllerName}.{actionName}()&#8221;, exception); #if !DEBUG var msg = &#8220;An unhandled error occurred.&#8221;; string stack = null; #else var msg = exception.Message; string stack [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1258","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/1258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=1258"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/1258\/revisions"}],"predecessor-version":[{"id":1259,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/1258\/revisions\/1259"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=1258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=1258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=1258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}