简体   繁体   中英

Convert base Exception into HttpException

I'm developing ASP.Net application and I'm currently struggling with exception handling. I've already managed to do proper exception handling but I don't really know how to handle internal errors that are not suitable for UI.

void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();
    LogManager.GetCurrentClassLogger().Error(ex);
    Response.Clear();

    HttpException httpEx = ex as HttpException;
    if (httpEx == null)
    {
        switch(ex.GetType())
        {
            //Convert ex into httpexception 
            //with statuscode that depends on the exception type
        }
    }

    RouteData routeData = new RouteData();
    routeData.Values.Add("controller", "Error");
    routeData.Values.Add("action", "Handler");
    routeData.Values.Add("exception", httpEx);
    Server.ClearError();
    Response.TrySkipIisCustomErrors = true;

    var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
    var c = ControllerBuilder.Current.GetControllerFactory().CreateController(rc, "Error");
    c.Execute(rc);
}

与其尝试将异常HttpException转换为HttpException ,不如尝试创建一个以原始异常作为内部异常的新HttpException

HttpException httpEx = new HttpException(500, msg, ex);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM