Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. public class CustomErrorHandlerAttribute : HandleErrorAttribute
  2. {
  3. public override void OnException(ExceptionContext filterContext)
  4. {
  5. var logger = log4net.LogManager.GetLogger("SomeLoggerHere");
  6.  
  7. logger.Error("An unhandled error occurred", filterContext.Exception);
  8.  
  9. if (filterContext.HttpContext.Request.IsAjaxRequest())
  10. {
  11. filterContext.HttpContext.Response.Clear();
  12. filterContext.HttpContext.Response.Status = "500 Internal Server Error";
  13. filterContext.Result = new JsonResult { Data = new { ErrorMessage = filterContext.Exception.Message } };
  14. filterContext.ExceptionHandled = true;
  15. }
  16. else
  17. {
  18. base.OnException(filterContext);
  19. }
  20.  
  21. }
  22.  
  23. [CustomErrorHandler]
  24. public class AnyController : Controller
  25. {
  26. ...
  27. }
  28.  
  29. [HttpPost]
  30. public ActionResult Create(OrderViewModel model)
  31. {
  32. if (!ModelState.IsValid)
  33. return View(model);
  34.  
  35. try
  36. {
  37. repository.Save(model);
  38. unitOfWork.Commit();
  39. return RedirectToAction("Index");
  40. }
  41. catch (Exception exc)
  42. {
  43. _loggingService.Error(exc);
  44. ModelState.AddModelError("KeyUsedInView", exc.Message); // or, show a generic error.
  45. }
  46.  
  47. return View(model);
  48. }
  49.  
  50. [NonAction]
  51. protected override void OnException(ExceptionContext filterContext)
  52. {
  53.  
  54. this.Session["ErrorException"] = filterContext.Exception;
  55.  
  56. if (filterContext.Exception.GetType() == typeof(PEDException))
  57. {
  58. // Mark exception as handled
  59. filterContext.ExceptionHandled = true;
  60.  
  61. // ... logging, etc
  62.  
  63. // Redirect
  64. filterContext.Result = this.RedirectToAction( "ShowError", "Errors");
  65. }
  66.  
  67. base.OnException(filterContext);
  68. }
  69.  
  70. protected void Application_Error(object sender, EventArgs e)
  71. {
  72. if (Request.Url.ToString().StartsWith("http://localhost:"))
  73. return;
  74. string msg;
  75. Exception ex = Server.GetLastError().GetBaseException();
  76. StringBuilder sb = new StringBuilder();
  77. sb.AppendLine("Exception Found");
  78. sb.AppendLine("Timestamp: " + System.DateTime.Now.ToString());
  79. sb.AppendLine("Error in: " + Request.Url.ToString());
  80. sb.AppendLine("Browser Version: " + Request.UserAgent.ToString());
  81. sb.AppendLine("User IP: " + Request.UserHostAddress.ToString());
  82. sb.AppendLine("Error Message: " + ex.Message);
  83. sb.AppendLine("Stack Trace: " + ex.StackTrace);
  84. msg = sb.ToString();
  85. Server.ClearError();
  86. YourMailHelper.SendException("Your Site Exception", msg);
  87. Response.Redirect("~/Error.html");
  88. }
  89.  
  90. public class Base_Controller : Controller
  91. {
  92. protected override void OnException(ExceptionContext filterContext)
  93. {
  94. Exception e = filterContext.Exception;
  95. //Custom Exception Logging Here
  96. //Log Exception e
  97. //Elmah.Mvc.ElmahController ec = new Elmah.Mvc.ElmahController();
  98. base.OnException(filterContext);
  99. }
  100. }
  101.  
  102. public class FilterConfig
  103. {
  104. public static void RegisterGlobalFilters(GlobalFilterCollection filters)
  105. {
  106. filters.Add(new HandleErrorAttribute());
  107. }
  108. }
  109.  
  110. void Application_Start(object sender, EventArgs e)
  111. {
  112. // Code that runs on application startup
  113. AreaRegistration.RegisterAllAreas();
  114. RouteConfig.RegisterRoutes(RouteTable.Routes);
  115. BundleConfig.RegisterBundles(BundleTable.Bundles);
  116. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  117.  
  118. }
  119.  
  120. //Check for Transport Exception with "Actual Exception" stored
  121. //in the inner exception property
  122. if (Model.Exception.InnerException != null)
  123. {
  124. errFriendly = Model.Exception.Message;
  125. modelEx = Model.Exception.InnerException;
  126. }
  127. else
  128. {
  129. modelEx = Model.Exception;
  130. }
  131. try
  132. {
  133. throw modelEx;
  134. }
  135. catch (System.Data.SqlClient.SqlException ex)
  136. {
  137. //Display Landing page friendly error for exception caused by home controller
  138. //Display generic data access error for all other controllers/actions
  139. if (Model.ActionName == "Index" && Model.ControllerName == "Home")
  140. {errFriendly = "Landing page cannot display product data...";}
  141. else
  142. {errFriendly = "Problem Accessing Data...";}
  143. errType = ex.GetType().ToString();
  144. errActual = ex.Message;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement