public HttpResponseException General(HttpException ex)
        {
            Debug.WriteLine(GetType().FullName + "." + MethodBase.GetCurrentMethod().Name);

            string strHtmlError = ex.GetBaseException().ToString();
            @ViewBag.strError = strHtmlError;

            //return View("Error");
            return new HttpResponseException(HttpStatusCode.InternalServerError);
        }
Example #2
0
        public static void SendErrorResponse(HttpResponseBase response, System.Web.HttpException he, string moduleVersion)
        {
            int    httpStatusCode = he.GetHttpCode();
            string msgType        = StatusCodeMessage(httpStatusCode);

            string errorDoc = null;

            try
            {
                errorDoc = Utilities.GetXmlStringFromFile(Settings.Get(("ErrorTemplate")));
            }
            catch (Exception ex)
            {
                // use default template if we can't find the requested one
                if (string.IsNullOrEmpty(errorDoc))
                {
                    errorDoc =
                        "<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE html PUBLIC \"-/W3C/DTD XHTML 1.0 Transitional/EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns = \"http:/www.w3.org/1999/xhtml\"><head><title>Application Error - {0} {1}</title></head><body style=\"font-family:Arial; font-size:10pt; color:#330000\"><h1>{0} {1}</h1><div class=\"error_message\">{2}</div><b><span class=\"error_location\">{4}.{3}</span></b><!--\n{5}\n--><br/><br/>In addition, the error page could not be loaded, so this default page was used instead.\n<!--THE FOLLOWING EXCEPTION OCCURRED WHILE LOADING THE ERROR TEMPLATE:\n" +
                        ex.Message + "\n--><hr/><i>RestNet Application {6}</i></body></html>";
                }
            }

            var url = (HttpContext.Current != null && HttpContext.Current.Request != null && HttpContext.Current.Request.Url != null) ? string.Format(" ({0})", HttpContext.Current.Request.Headers["X-REWRITE-URL"] ?? HttpContext.Current.Request.Url.PathAndQuery) : string.Empty;

            System.Reflection.MethodBase targetSite = he.GetBaseException().TargetSite;
            string msg = string.Format(errorDoc,
                                       httpStatusCode,
                                       msgType,
                                       he.Message + url,
                                       targetSite == null ? string.Empty : targetSite.Name,
                                       targetSite == null ? string.Empty : targetSite.ReflectedType.FullName,
                                       System.Web.HttpUtility.HtmlEncode(he.GetBaseException().StackTrace),
                                       moduleVersion);

            // Set logging level differently for client vs. server errors.
            // 100, 200, 300 series should never really appear, but just in case I've included them here
            // also, don't bother including full exceptions for < 500, as they're likely to just duplicate the message
            switch ((int)(httpStatusCode / 100))
            {
            case 0:
            case 1:
            case 2:
                Logging.Debug(he.Message + url);
                break;

            case 3:
                Logging.InfoFormat("{0} - {1}", httpStatusCode, he.Message + url);
                break;

            case 4:
                if (httpStatusCode == 401)
                {
                    // 401 challenge isn't really worthy of a warning
                    Logging.DebugFormat("{0} - {1}", httpStatusCode, he.Message + url);
                }
                else
                {
                    Logging.WarnFormat("{0} - {1}", httpStatusCode, he.Message + url);
                }
                break;

            default:
                Logging.Error(string.Format("{0} - {1}", httpStatusCode, he.Message + url), he);
                break;
            }
            ReturnError(response, httpStatusCode, msg);
        }