Exemple #1
0
        public virtual bool TryDisplayErrorView(Exception ex, Exceptions.ErrorPage errorPage, int httpStatusCode, bool throwErrorIfAny)
        {
            try
            {
                if (this.ControllerContext == null)
                {
                    throw new ApplicationException("Controller context not set. Be sure to call InitContext on base controller before displaying an error view.");
                }
                string areaName = (RouteData != null && RouteData.DataTokens.ContainsKey("area") ? RouteData.DataTokens["area"].ToString() : null);
                string controllerName = (RouteData != null && RouteData.Values.ContainsKey("controller") ? RouteData.Values["controller"].ToString() : null);
                string actionName = (RouteData != null && RouteData.Values.ContainsKey("action") ? RouteData.Values["action"].ToString() : null);

                if (string.IsNullOrEmpty(areaName))
                {
                    actionName = "(unknown area)";
                }
                if (string.IsNullOrEmpty(controllerName))
                {
                    controllerName = "(unknown controller)";
                }
                if (string.IsNullOrEmpty(actionName))
                {
                    actionName = "(unknown action)";
                }

                string ipAddress = this.Request.UserHostAddress;
                StoreFront currentStoreFrontOrNull = null;
                StoreFrontConfiguration currentStoreFrontConfigOrNull = null;
                _throwErrorIfAnonymous = false;
                _throwErrorIfStoreFrontNotFound = false;
                _throwErrorIfUserProfileNotFound = false;
                _useInactiveStoreFrontAsActive = true;
                _useInactiveStoreFrontConfigAsActive = true;
                try
                {
                    currentStoreFrontOrNull = CurrentStoreFrontOrNull;
                    currentStoreFrontConfigOrNull = CurrentStoreFrontConfigOrNull;
                }
                catch (Exception)
                {
                }

                Response.Clear();
                Response.StatusCode = httpStatusCode;
                string errorPageFileName = errorPage.ErrorPageFileName();
                if (!Enum.IsDefined(typeof(Exceptions.ErrorPage), errorPage))
                {
                    return false;
                }

                string errorViewName = Enum.GetName(typeof(Exceptions.ErrorPage), errorPage);

                if (string.IsNullOrEmpty(Request["NotFound"]) && errorPage == ErrorPage.Error_NotFound && CurrentStoreFrontIdOrNull != null && currentStoreFrontConfigOrNull.NotFoundErrorPage != null && currentStoreFrontConfigOrNull.NotFoundErrorPage.IsActiveBubble())
                {
                    string urlRedirect = currentStoreFrontConfigOrNull.NotFoundErrorPage.UrlResolved(Url) + "?NotFound=1";
                    AddUserMessage("Page Not Found", "Sorry, the URL you were looking for was not found. '" + Request.Url.ToString().ToHtml() + "'", UserMessageType.Danger);
                    new RedirectResult(urlRedirect, false).ExecuteResult(this.ControllerContext);
                    return true;
                }

                if (string.IsNullOrEmpty(Request["StoreError"]) && (errorPage == ErrorPage.Error_AppError || errorPage == ErrorPage.Error_BadRequest || errorPage == ErrorPage.Error_HttpError || errorPage == ErrorPage.Error_InvalidOperation || errorPage == ErrorPage.Error_UnknownError) && CurrentStoreFrontIdOrNull != null && currentStoreFrontConfigOrNull.StoreErrorPage != null && currentStoreFrontConfigOrNull.StoreErrorPage.IsActiveBubble())
                {
                    string urlRedirect = currentStoreFrontConfigOrNull.StoreErrorPage.UrlResolved(Url) + "?StoreError=1";
                    AddUserMessage("Server Error", "Sorry, there was a server error processing your request for URL '" + Request.Url.ToString().ToHtml() + "'", UserMessageType.Danger);
                    new RedirectResult(urlRedirect, false).ExecuteResult(this.ControllerContext);
                    return true;
                }

                GStoreErrorInfo model = new GStoreErrorInfo(errorPage, ex, RouteData, controllerName, actionName, ipAddress, currentStoreFrontOrNull, Request.RawUrl, Request.Url.ToString());
                View(errorViewName, model).ExecuteResult(this.ControllerContext);

                return true;
            }
            catch (Exception exDisplay)
            {
                string message = "Error in Controller Error View."
                    + " \n --Controller: " + this.GetType().FullName
                    + " \n --Url: " + Request.Url.ToString()
                    + " \n --RawUrl: " + Request.RawUrl
                    + " \n --ErrorPage: " + "[" + ((int)errorPage).ToString() + "] " + errorPage.ToString()
                    + " \n --Exception: " + exDisplay.ToString()
                    + " \n --Source: " + exDisplay.Source
                    + " \n --TargetSiteName: " + exDisplay.TargetSite.Name
                    + " \n --StackTrace: " + exDisplay.StackTrace
                    + " \n --Original Exception: " + ex.Message.ToString()
                    + " \n --Original Source: " + ex.Source
                    + " \n --Original TargetSiteName: " + ex.TargetSite.Name
                    + " \n --Original StackTrace: " + ex.StackTrace;

                System.Diagnostics.Trace.WriteLine("--" + message);
                string exceptionMessage = exDisplay.Message;
                string baseExceptionMessage = ex.GetBaseException().Message;
                string baseExceptionToString = ex.GetBaseException().ToString();
                GStoreDb.LogSystemEvent(HttpContext, RouteData, RouteData.ToSourceString(), SystemEventLevel.ApplicationException, message, exceptionMessage, baseExceptionMessage, baseExceptionToString, this);
                if (throwErrorIfAny)
                {
                    throw new ApplicationException(message, exDisplay);
                }
                return false;
            }
        }