Skip to content

Instantly share code, notes, and snippets.

@dgwaldo
Created October 28, 2014 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgwaldo/1ed4e107dab68fcc533d to your computer and use it in GitHub Desktop.
Save dgwaldo/1ed4e107dab68fcc533d to your computer and use it in GitHub Desktop.
Example of WebAPI 2 Exception Filter
public static class WebApiConfig{
public static void Register(HttpConfiguration config)
{
#if DEBUG
config.Filters.Add(new DebugGeneralExceptionAttribute());
#endif
webApiConfig . Filters. Add( new DbEntityValidationExceptionAttribute ());
// Other configuration code...
}
}
public class DebugGeneralExceptionAttribute : ExceptionFilterAttribute
{
public override void OnException( HttpActionExecutedContext context)
{
var messageParsedOfNewLineEtc = Regex .Replace(context . Exception. Message, @"\t|\n|\r" , "" );
var response = new HttpResponseMessage ( HttpStatusCode. InternalServerError)
{
Content = new StringContent(context . Exception. Message),
ReasonPhrase = "Something has gone terribly wrong :( " + messageParsedOfNewLineEtc
};
context . Response = response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment