Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Martin 181 posts 740 karma points
    Sep 10, 2013 @ 17:37
    Martin
    0

    Error handling? What does people recommend?

    Hi,

    I would like to know what people usually do to handle "unhandled exceptions". Right now I'm trying to create a http module which listens on the OnError event. When the event is triggered I try to lookup a Umbraco content node with document type PageInternalError and that goes fine but when I try to do an internal redirect to that page "it just blows up".

    I'm using Umbraco 6.1.4. I know this has been possible in earlier verions (think it was 4.7.x).

    The code which when an error occur:

    private void OnError(object sender, EventArgs e)
    {
        var lastException = UmbracoContext.Current.HttpContext.Server.GetLastError();
    
        if (lastException != null)
        {
            var umbracoContext = UmbracoContext.Current;
            var websiteNode = umbracoContext.ContentCache.GetAtRoot().SingleOrDefault();
    
            if (websiteNode == null)
                throw new InvalidOperationException("Could not find website node.");
    
            var language = umbracoContext.PublishedContentRequest.Culture;
            var languageNode = websiteNode.Descendants("Language")
                        .SingleOrDefault(x => x.Name.Equals(language.Name, StringComparison.OrdinalIgnoreCase));
    
            if (languageNode == null)
            {
                languageNode = websiteNode.Descendants("Language")
                    .FirstOrDefault();
            }
    
            if (languageNode == null)
                throw new InvalidOperationException(
                    string.Format("Could not find language node with name '{0}'", language.Name));
    
            var pageInternalError = languageNode.Descendants("PageInternalError")
                .SingleOrDefault();
    
            if (pageInternalError == null)
                throw new InvalidOperationException(
                    string.Format("Could not find PageInternalError page for language '{0}'", language.Name));
    
            var url = new Uri(pageInternalError.Url, UriKind.Absolute);
    
            umbracoContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            umbracoContext.HttpContext.Response.TrySkipIisCustomErrors = true;
            umbracoContext.HttpContext.Server.ClearError();
            umbracoContext.HttpContext.Server.Transfer(url.LocalPath);
        }
    }
    

    When Server.Transfer() occurs another error is thrown by asp.net saying that the path is not a virtual path. I have as well tried to use PublishedContentRequest (I think it's called that?) and using the methods SetInternalRedirectPublishedContent and parsing my page. When I do that it throws an exception "Cannot modify a PublishedContentRequest once it is read-only.".

    Soo... What posibilities do I have?

    Well, I know it works when I use Response.Redirect() but then I don't keep my Url where the error happened which I think is important otherwise the user might not be able to report which page failed.

    Anyone having an idea or perhaps the Umbraco Core needs a new extension point to make this easier.

    Note: This ErrorModule is only thought to catch unhandled exceptions which occur in custom code and not within Umbraco itself.

    Best regards Martin

Please Sign in or register to post replies

Write your reply to:

Draft