Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CustomErrorHandlerAttribute : HandleErrorAttribute
- {
- public override void OnException(ExceptionContext filterContext)
- {
- var logger = log4net.LogManager.GetLogger("SomeLoggerHere");
- logger.Error("An unhandled error occurred", filterContext.Exception);
- if (filterContext.HttpContext.Request.IsAjaxRequest())
- {
- filterContext.HttpContext.Response.Clear();
- filterContext.HttpContext.Response.Status = "500 Internal Server Error";
- filterContext.Result = new JsonResult { Data = new { ErrorMessage = filterContext.Exception.Message } };
- filterContext.ExceptionHandled = true;
- }
- else
- {
- base.OnException(filterContext);
- }
- }
- [CustomErrorHandler]
- public class AnyController : Controller
- {
- ...
- }
- [HttpPost]
- public ActionResult Create(OrderViewModel model)
- {
- if (!ModelState.IsValid)
- return View(model);
- try
- {
- repository.Save(model);
- unitOfWork.Commit();
- return RedirectToAction("Index");
- }
- catch (Exception exc)
- {
- _loggingService.Error(exc);
- ModelState.AddModelError("KeyUsedInView", exc.Message); // or, show a generic error.
- }
- return View(model);
- }
- [NonAction]
- protected override void OnException(ExceptionContext filterContext)
- {
- this.Session["ErrorException"] = filterContext.Exception;
- if (filterContext.Exception.GetType() == typeof(PEDException))
- {
- // Mark exception as handled
- filterContext.ExceptionHandled = true;
- // ... logging, etc
- // Redirect
- filterContext.Result = this.RedirectToAction( "ShowError", "Errors");
- }
- base.OnException(filterContext);
- }
- protected void Application_Error(object sender, EventArgs e)
- {
- if (Request.Url.ToString().StartsWith("http://localhost:"))
- return;
- string msg;
- Exception ex = Server.GetLastError().GetBaseException();
- StringBuilder sb = new StringBuilder();
- sb.AppendLine("Exception Found");
- sb.AppendLine("Timestamp: " + System.DateTime.Now.ToString());
- sb.AppendLine("Error in: " + Request.Url.ToString());
- sb.AppendLine("Browser Version: " + Request.UserAgent.ToString());
- sb.AppendLine("User IP: " + Request.UserHostAddress.ToString());
- sb.AppendLine("Error Message: " + ex.Message);
- sb.AppendLine("Stack Trace: " + ex.StackTrace);
- msg = sb.ToString();
- Server.ClearError();
- YourMailHelper.SendException("Your Site Exception", msg);
- Response.Redirect("~/Error.html");
- }
- public class Base_Controller : Controller
- {
- protected override void OnException(ExceptionContext filterContext)
- {
- Exception e = filterContext.Exception;
- //Custom Exception Logging Here
- //Log Exception e
- //Elmah.Mvc.ElmahController ec = new Elmah.Mvc.ElmahController();
- base.OnException(filterContext);
- }
- }
- public class FilterConfig
- {
- public static void RegisterGlobalFilters(GlobalFilterCollection filters)
- {
- filters.Add(new HandleErrorAttribute());
- }
- }
- void Application_Start(object sender, EventArgs e)
- {
- // Code that runs on application startup
- AreaRegistration.RegisterAllAreas();
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- BundleConfig.RegisterBundles(BundleTable.Bundles);
- FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
- }
- //Check for Transport Exception with "Actual Exception" stored
- //in the inner exception property
- if (Model.Exception.InnerException != null)
- {
- errFriendly = Model.Exception.Message;
- modelEx = Model.Exception.InnerException;
- }
- else
- {
- modelEx = Model.Exception;
- }
- try
- {
- throw modelEx;
- }
- catch (System.Data.SqlClient.SqlException ex)
- {
- //Display Landing page friendly error for exception caused by home controller
- //Display generic data access error for all other controllers/actions
- if (Model.ActionName == "Index" && Model.ControllerName == "Home")
- {errFriendly = "Landing page cannot display product data...";}
- else
- {errFriendly = "Problem Accessing Data...";}
- errType = ex.GetType().ToString();
- errActual = ex.Message;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement