globalasax

 avatar
unknown
csharp
2 years ago
2.5 kB
5
Indexable
private void renderError(bool isAjaxCall, string message, bool isKE = false)
        {
            message = message.Replace("\r\n", "<br/>");

            if (isAjaxCall || isKE)
            {
                Response.Clear();
                Server.ClearError();
            }

            if (isAjaxCall)
                Response.Write(message);
            else
            {
                Response.Redirect(String.Format("~/Error/_500/?message={0}", message));
            }
        }

        private string getInnerErrorMessage(Exception e)
        {
            if (e.InnerException == null) return e.Message;
            return getInnerErrorMessage(e);
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            bool isAjaxCall = new HttpRequestWrapper(Context.Request).IsAjaxRequest();

            var httpApp = (HttpApplication)sender;
            var rawURL = httpApp.Request.RawUrl;

            Exception exception = Server.GetLastError();

            if (exception is KnownException)
            {
                var ke = (KnownException)exception;
                Response.StatusCode = 501;

                renderError(isAjaxCall, ke.ErrorCode + ": " + ke.Message);
            }
            else
            {
                Response.StatusCode = 501;
                renderError(isAjaxCall, getInnerErrorMessage(exception));
            }

            /*Server.ClearError();
            Response.Clear();
            Response.Redirect(String.Format("~/Error/_500/"));*/
            /*string hostName = Request.Headers["host"].Split(':')[0];
            if (hostName.Contains("exampleOnMyOtherDomain"))
            {
                Exception exception = Server.GetLastError();
                Response.Clear();
                HttpException httpException = exception as HttpException;
                Response.TrySkipIisCustomErrors = true;

                switch (httpException.GetHttpCode())
                {
                    case 404:
                        Response.StatusCode = 404;
                        Server.Transfer("~Views/Errors/_404.cshtml");
                        break;
                    case 500:
                    default:
                        Response.StatusCode = 500;
                        Server.Transfer("~/Errors/_500.cshtml");
                        break;
                }
                Server.ClearError();
            }*/

        }
Editor is loading...