ClearError() public method

public ClearError ( ) : void
return void
 public void ClearError()
 {
     context.ClearError();
 }
Beispiel #2
0
        /// <summary>
        /// Fixes a bug in some configurations of ASP.Net and Mono where a thrown HttpException will cause a redirect with an improper status code
        /// </summary>
        public static void HandleErrors(HttpContext Context)
        {
            try
            {
                if (RunIt(Context) == false)
                {
                    return;
                }
            }
            catch
            {
                throw new NotSupportedException("CustomErrorsFixer does not work in Medium trust");
            }
            HttpException ex = Context.Error as HttpException;
            if(ex==null){
                try{
                    ex=Context.Error.InnerException as HttpException;
                }catch{
                    ex=null;
                }
            }

            if (ex != null){
                Context.Response.StatusCode = ex.GetHttpCode();
            }else{
                Context.Response.StatusCode = 500;
            }
            Context.ClearError();
            Context.Server.Transfer(GetCustomError(Context.Response.StatusCode.ToString()));

            HttpContext.Current.Response.End();
        }
Beispiel #3
0
 public override void ClearError()
 {
     w.ClearError();
 }
Beispiel #4
0
        /// <summary>
        /// Executes the GStore controller with an error view. controller must be set
        /// </summary>
        /// <param name="errorPage"></param>
        /// <param name="httpStatusCode"></param>
        /// <param name="ex"></param>
        /// <param name="clearError"></param>
        /// <param name="context"></param>
        /// <param name="controller"></param>
        /// <returns></returns>
        public static bool TryExecuteAppErrorController(ErrorPage errorPage, int httpStatusCode, Exception ex, bool clearError, HttpContext context, BaseController controller)
        {
            string url = string.Empty;
            string rawUrl = string.Empty;
            if (context != null)
            {
                url = context.Request.Url.ToString();
                rawUrl = context.Request.RawUrl;
            }

            if (controller == null)
            {
                System.Diagnostics.Trace.WriteLine("--Failure. Controller is null; no error controller is available ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Original Exception: " + ex.Message);
                return false;
            }

            var routeData = new RouteData();

            //if we can find the current area from the url, we can use a area-specific error handler to incorporate area layout/design
            string area = context.Request.DetermineAreaFromRequest();
            if (!string.IsNullOrEmpty(area))
            {
                routeData.DataTokens.Add("area", area);
            }
            routeData.Values["controller"] = "GStore";
            routeData.Values["action"] = "AppError";
            routeData.Values["exception"] = ex;
            routeData.Values["errorpage"] = errorPage;
            routeData.Values["httpStatusCode"] = httpStatusCode;

            System.Web.Mvc.IController errorController = null;
            if (controller != null)
            {
                try
                {
                    errorController = controller.GStoreErrorControllerForArea;
                }
                catch (Exception exAreaError)
                {
                    System.Diagnostics.Trace.WriteLine("--Failure. Error getting GStoreErrorControllerForArea for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Exception: " + exAreaError.Message + " Original Exception: " + ex.Message);
                    return false;
                }
            }

            var rc = new RequestContext(new HttpContextWrapper(context), routeData);
            try
            {
                System.Diagnostics.Trace.WriteLine("--Executing error controller for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Original Exception: " + ex.Message);
                errorController.Execute(rc);
                System.Diagnostics.Trace.WriteLine("--Success executing error controller for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Original Exception: " + ex.Message);
                if (clearError)
                {
                    context.ClearError();
                }
                return true;
            }
            catch (Exception exErrorController)
            {
                System.Diagnostics.Trace.WriteLine("--Failure. Error executing error controller for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Exception: " + exErrorController.Message + " Original Exception: " + ex.Message);
                return false;
            }
        }
 public override void ClearError()
 {
     _context.ClearError();
 }
Beispiel #6
0
        public static bool TryDisplayStaticPage(ErrorPage errorPage, int httpStatusCode, Exception ex, bool clearError, HttpContext context, BaseController controller)
        {
            string url = string.Empty;
            string rawUrl = string.Empty;
            if (context != null)
            {
                url = context.Request.Url.ToString();
                rawUrl = context.Request.RawUrl;
            }

            if (controller != null && controller.CurrentStoreFrontOrNull != null)
            {
                //start with storefront error pages folder
                Models.StoreFront storeFront = controller.CurrentStoreFrontOrThrow;

                string customErrorFolder = storeFront.StoreFrontVirtualDirectoryToMap(controller.Request.ApplicationPath) + "/ErrorPages/";
                string customErrorPath = customErrorFolder + errorPage.ErrorPageFileName();
                bool customErrorFileExists = false;
                if (System.IO.File.Exists(context.Server.MapPath(customErrorPath)))
                {
                    customErrorFileExists = true;
                }
                else
                {
                    //if store front error page not found, look for client custom error page
                    customErrorFolder = storeFront.Client.ClientVirtualDirectoryToMap(context.Request.ApplicationPath) + "/ErrorPages/";
                    customErrorPath = customErrorFolder + errorPage.ErrorPageFileName();
                    if (System.IO.File.Exists(context.Server.MapPath(customErrorPath)))
                    {
                        customErrorFileExists = true;
                    }
                }

                if (customErrorFileExists)
                {
                    try
                    {
                        context.Response.Clear();
                        context.Response.StatusCode = httpStatusCode;
                        System.Diagnostics.Trace.WriteLine("--Displaying custom error static page: " + customErrorPath + " for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Original Exception: " + ex.Message);
                        context.Server.Execute(customErrorPath);
                        context.Response.Flush();
                        System.Diagnostics.Trace.WriteLine("--Success displaying custom error static page: " + customErrorPath + " for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Original Exception: " + ex.Message);
                        if (clearError)
                        {
                            context.ClearError();
                        }
                        return true;

                    }
                    catch (Exception exCustomPageError)
                    {
                        System.Diagnostics.Trace.WriteLine("--Failure. Error displaying custom error static page: " + customErrorPath + " for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Exception: " + exCustomPageError.Message + " Original Exception: " + ex.Message);
                        string error = exCustomPageError.Message;
                    }
                }
            }

            //unknown client or client does not have a custom error page, show server static error page
            string serverErrorFolder = "~/Content/Server/ErrorPages/";
            string serverErrorPath = serverErrorFolder + errorPage.ErrorPageFileName();
            if (!System.IO.File.Exists(context.Server.MapPath(serverErrorPath)))
            {
                System.Diagnostics.Trace.WriteLine("--Failure. Error: Server error static page not found: " + serverErrorPath + " for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Original Exception: " + ex.Message);
                return false;
            }

            try
            {
                context.Response.Clear();
                context.Response.StatusCode = httpStatusCode;
                System.Diagnostics.Trace.WriteLine("--Displaying server error static page: " + serverErrorPath + " for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl);
                context.Server.Execute(serverErrorPath);
                context.Response.Flush();
                System.Diagnostics.Trace.WriteLine("--Success displaying server error static page: " + serverErrorPath + " for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl);
                if (clearError)
                {
                    context.Server.ClearError();
                }
                return true;
            }
            catch (Exception exServerPageError)
            {
                System.Diagnostics.Trace.WriteLine("--Failure. Error displaying server error static page: " + serverErrorPath + " for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Exception: " + exServerPageError.Message + " Original Exception: " + ex.Message);
                return false;
            }
        }
Beispiel #7
0
        public static bool TryDisplayControllerErrorView(ErrorPage errorPage, int httpStatusCode, Exception ex, bool clearError, HttpContext context, BaseController controller)
        {
            string url = string.Empty;
            string rawUrl = string.Empty;
            if (context != null)
            {
                url = context.Request.Url.ToString();
                rawUrl = context.Request.RawUrl;
            }

            try
            {
                if (controller == null)
                {
                    return false;
                }

                System.Diagnostics.Trace.WriteLine("--Executing controller.TryDisplayErrorView for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Original Exception: " + ex.Message);
                bool resultSuccess = controller.TryDisplayErrorView(ex, errorPage, httpStatusCode, false);
                System.Diagnostics.Trace.WriteLine("--" + (resultSuccess ? "Success" : "Failure") + " executing controller.TryDisplayErrorView. Result: " + resultSuccess.ToString() + " for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Original Exception: " + ex.Message);
                if (resultSuccess)
                {
                    if (clearError)
                    {
                        context.ClearError();
                    }
                    return true;
                }
                return false;
            }
            catch (Exception exController)
            {
                System.Diagnostics.Trace.WriteLine("--Error executing controller.TryDisplayErrorView for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Exception: " + exController.Message + " Original Exception: " + ex.Message);
                return false;
            }
        }
Beispiel #8
0
        public static void HandleError(HttpContext ctx)
        {
            if (ctx.Request.IsLocal || ctx.Request.Url.IsLoopback || ctx.Request.Url.Host == "localhost")
                return;

            var req = ctx.Request;

            if (req == null)
            {
                ctx.ClearError();
                ctx.Response.Redirect("~/Errors/500.aspx?s=1", true);
                return;
            }

            string u = req.Url.PathAndQuery.ToLower();

            if (u.StartsWith("/Errors/"))
            {
                ctx.ClearError();
                ctx.Response.Write("An unrecoverable error has been encountered. We apologize for any inconvience.");
                ctx.Response.End();
                return;
            }

            if (u.StartsWith("/scriptresource.axd"))
            {
                ctx.ClearError();
                ctx.Response.Redirect("~/Errors/500.aspx?s=2", true);
                return;
            }

            Exception ex = ctx.Error;

            LogError(ex,ctx);

            ctx.ClearError();

            if( ctx.Request.ContentType.ToLower().StartsWith( "application/json" ) || (ctx.Request.Headers["X-Requested-With"] == "XMLHttpRequest") )
            {
                ctx.Response.ContentType = "application/json";
                ctx.Response.StatusCode = 200;
                ctx.Response.TrySkipIisCustomErrors = true;
                ctx.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new Framework.API.ReturnObject
                {
                    StatusCode = 500,
                    Message = ex.ToString(),
                    Result = ex
                }));
                ctx.Response.End();
            }
            else
            {
                ctx.Response.ContentType = "application/json";
                ctx.Response.StatusCode = 200;
                ctx.Response.TrySkipIisCustomErrors = true;
                ctx.Response.Write( Newtonsoft.Json.JsonConvert.SerializeObject( new Framework.API.ReturnObject {
                    StatusCode = 500,
                    Message = ex.ToString(),
                    Result = ex
                } ) );
                ctx.Response.Flush();
                ctx.Response.End();
                //ctx.Response.Redirect("~/Errors/500.aspx?s=3", true);
            }
        }
		private static void ProcessException(HttpContext context)
		{
			if (context.Error != null)
			{
				string detail = EnvironmentHelper.GetEnvironmentInfo() + "\r\n" +
					context.Error.GetAllStackTrace();

				Exception realEx = ExceptionHelper.GetRealException(context.Error);

				context.ClearError();

				realEx.TryWriteAppLog(detail);

				ResponseErrorMessage(context, realEx.Message, detail);
			}
		}
Beispiel #10
0
        private void ShowCustomErrorPage(Exception exception, HttpContext httpContext, string currentController, string currentAction)
        {
            var controller = new ErrorController();
            var routeData = new RouteData();
            var action = "Index";

            if (exception is HttpException)
            {
                var httpEx = exception as HttpException;

                switch (httpEx.GetHttpCode())
                {
                    case 404:
                        action = "NotFound";
                        break;

                    // others if any
                }
            }

            httpContext.ClearError();
            httpContext.Response.Clear();
            httpContext.Response.StatusCode = exception is HttpException ? ((HttpException)exception).GetHttpCode() : 500;
            httpContext.Response.TrySkipIisCustomErrors = true;

            routeData.Values["controller"] = "Error";
            routeData.Values["action"] = action;

            controller.ViewData.Model = new HandleErrorInfo(exception, currentController, currentAction);
            ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));
        }