private EntegralMVCExceptionModel GetModelFromSession() { EntegralMVCExceptionModel model = Session["ErrorModel"] as EntegralMVCExceptionModel; if (model == null) model = new EntegralMVCExceptionModel() { Message = "Beklenmedik bir hata oluştu" }; return model; }
/* * Bu metodun en önemli amacı : BusinessException dışındaki hata mesajlarını genel bir hata mesajına çevirmek ve hata view'ını döndürmektir. * Bunun dışında * * hata mesajını dile göre lokalize etmek * * hatayı elmah'a log'lamak * * yetki hatası olması durumunda giriş sayfasına otomatik yönlendirme yapmak * * ... * işlerini de yapar. * */ public void OnException(ExceptionContext filterContext) { if (filterContext.ExceptionHandled) return; Exception e = filterContext.Exception; //Token expire olduğu zaman EGAS'tan AuthorizationException fırlatılıyor //burada da AuthorizationException alındığı zaman Account/LogOff ekranına yönlendirme yapılıyor. //Bu yönlendirme yapıldığı zaman Elmah üzerine log yazılmaması için //aşağıdaki satır comment out edilerek AuthorizationException durumunun dışına eklendi //ErrorSignal.FromCurrentContext().Raise(e); if (e.Message == "Token not verified" || e.Message == "Request is not authorized. Please try logging off and logging on again") { // TODO AuAz konduktan sonra burası test edilip tekrar yazılmalı. if (!filterContext.HttpContext.Response.IsRequestBeingRedirected) { filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "controller", "Account" }, { "action", "LogOff" }, { "area", "" } }); filterContext.ExceptionHandled = true; } } else { // Elmah'a log'lanır ErrorSignal.FromCurrentContext().Raise(e); if (e.Message.ToLowerInvariant().Contains("The DELETE statement conflicted with the REFERENCE constraint".ToLowerInvariant())) { e = new BusinessException("Silmeye çalıştığınız dataya bağlı başka datalar bulunuyor. Lütfen önce bağlı dataları siliniz."); } // Hata mesajı modifiye edilir. string messageCode = e.Message; if (e is BusinessException) messageCode = e.Message; else messageCode = NonBusinessErrorMessage; string messageText; if (MessageLocalizer != null) messageText = MessageLocalizer(messageCode); else messageText = messageCode; // Error view'ın modeli hazırlanır. EntegralMVCExceptionModel model = new EntegralMVCExceptionModel { ControllerName = filterContext.RouteData.Values["Controller"].ToString(), ActionName = filterContext.RouteData.Values["Action"].ToString(), ExceptionObject = filterContext.Exception, Message = messageText }; filterContext.HttpContext.Session["ErrorModel"] = model; filterContext.ExceptionHandled = true; filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; filterContext.HttpContext.Response.StatusCode = 500; filterContext.HttpContext.Response.StatusDescription = model.Message; filterContext.HttpContext.Response.Clear(); // Error view dönülür. if (!filterContext.HttpContext.Response.IsRequestBeingRedirected) { if (filterContext.IsChildAction) { filterContext.Result = new EmptyResult(); } else { string fullErrorUrl = VirtualPathUtility.ToAbsolute(this.ErrorUrl); if (filterContext.HttpContext.Request.Path.ToLowerInvariant() == fullErrorUrl.ToLowerInvariant()) { // filterContext.Result = new RedirectResult(FallbackErrorUrl); filterContext.HttpContext.Server.TransferRequest(FallbackErrorUrl); } else { // partial view dönülmesi gereken durum if (filterContext.HttpContext.Request.IsAjaxRequest()) { // Aşağıdaki kod orijinal action'un tekrar tekrar tetiklenmesine sebep olduğu için vazgeçildi. //filterContext.Result = new PartialViewResult //{ // ViewName = PartialErrorUrl, // ViewData = new ViewDataDictionary<EntegralMVCExceptionModel>(model), // TempData = filterContext.Controller.TempData //}; //filterContext.Result = new RedirectResult(PartialErrorUrl); filterContext.HttpContext.Server.TransferRequest(PartialErrorUrl); } else // normal view dönülmesi gereken durum { // filterContext.Result = new RedirectResult(ErrorUrl); filterContext.HttpContext.Server.TransferRequest(ErrorUrl); } } } } } }