Over a million developers have joined DZone.

OneTrueError and ASP.NET

·

OneTrueError is my new startup which also is a member of Microsoft BizSpark. This post is about the client library for ASP.NET (WebForms/MVC/WebAPI). It will catch and handle all uncaught exceptions automatically. They are also uploaded to our site for analysis to enable us to suggest possible solutions

To get started you need to have an account at onetrueerror.com and download our nuget-packageOneTrueError.AspNet. Once you’ve completed that you’ll only have to add these lines of code to yourglobal.asax.

OneTrue.Configuration.Credentials("appKeyThatYouGot", 
                                  "sharedSecretThatYouGot");
OneTrue.Configuration.CatchAspNetExceptions();

Now we’ll be able to detect and handle all uncaught exceptions. Simply do something which would generate an exception, like visiting a page that do not exist. You should get our default error page which looks like this:

If you inspect the error page HTTP headers you’ll see that we use correct status codes:

We also honor the requested type. If the caller wants XML we do provide XML. Same goes for JSON.

Options

As you’ve might have seen, we collect a lot of context information to be able to provide you with possible error solutions. Some users do not like that and we can therefore enable you to allow them to choose if we can upload the report or not.

You simply change the initialization code:

OneTrue.Configuration.Credentials("appKeyThatYouGot", 
                                  "sharedSecretThatYouGot");
OneTrue.Configuration.AskForPermission = true;
OneTrue.Configuration.CatchAspNetExceptions();

That changes the error page to:

You can also ask the user to provide details:

OneTrue.Configuration.AskForDetails = true;

To enable them to follow the progress of the error (and get notified when you’ve have completed the error) you change another flag:

OneTrue.Configuration.AskForEmail = true;

You can of course combine them:

Customization

You do not have to use our error page. You can create your own ASPX or HTML page (those works for WebApi/MVC too). To activate it, use the following command:

OneTrue.Configuration.SetErrorPage("~/views/error.aspx");

Here is a sample that I’ve made:

< %@ Page Language="C#" AutoEventWireup="true" CodeBehind="Error.aspx.cs" Inherits="TestWebApp.Views.WebForm1" %>
< %@ Import Namespace="OneTrueError.Reporting" %>
< !DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>OneSite - Deadly error</title>
    <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Butcherman"/>
</head>
<body style="background: #000; color: red;font-family: Butcherman; text-align: center; font-size: 1.2em">
    <h1>We are all doomed</h1>
    <img runat="server" src="~/Images/doomed.jpg"/>
    <form id="form1" runat="server">
    <div>
    < %: ((Exception)Context.Items["Exception"]).Message  %>
    </div>
    </form>
</body>
</html>

Result:

You can also use a callback to provide different error pages for different errors:

public class WebApiApplication : System.Web.HttpApplication
{
	protected void Application_Start()
	{
		OneTrue.Configuration.Credentials("appKeyThatYouGot", "sharedSecretThatYouGot");
		OneTrue.Configuration.SetErrorPage(ErrorPageGenerator);
		OneTrue.Configuration.CatchAspNetExceptions();

		AreaRegistration.RegisterAllAreas();
		WebApiConfig.Register(GlobalConfiguration.Configuration);
		FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
		RouteConfig.RegisterRoutes(RouteTable.Routes);
		BundleConfig.RegisterBundles(BundleTable.Bundles);
	}

	private string ErrorPageGenerator(HttpErrorReporterContext context)
	{
		var errorPage = context.HttpContext.Response.StatusCode == 404
			? "~/Views/404.aspx"
			: "~/Views/Error.html";

		// class in our library
		return PageBuilder.Build(errorPage, context);
	}
}

Context information

At our website you can see all information about the HTTP request, response, sessions and cookies.

Topics:

Published at DZone with permission of Jonas Gauffin, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}