Skip to content

Instantly share code, notes, and snippets.

@seangwright
Created April 23, 2016 18:14
Show Gist options
  • Save seangwright/a231be14e1871f489602a1b25f425598 to your computer and use it in GitHub Desktop.
Save seangwright/a231be14e1871f489602a1b25f425598 to your computer and use it in GitHub Desktop.
GlobalExceptionHandler.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Net;
using System.Web.Http.ExceptionHandling;
using WiredViews.WIR03.Web.Api.Results;
namespace WiredViews.WIR03.Web.Api.Exceptions
{
/// <summary>
/// Handles all exceptions thrown by the Web.Api stack
/// </summary>
public class GlobalExceptionHandler : ExceptionHandler
{
/// <summary>
/// Constructor
/// </summary>
public GlobalExceptionHandler() { }
public override bool ShouldHandle(ExceptionHandlerContext context)
{
return true;
}
public override Task HandleAsync(ExceptionHandlerContext context, System.Threading.CancellationToken cancellationToken)
{
return base.HandleAsync(context, cancellationToken);
}
public override void Handle(ExceptionHandlerContext context)
{
Exception exception = context.Exception;
HttpException httpException = exception as HttpException;
if (httpException != null)
{
context.Result = new ErrorResult(context.Request, (HttpStatusCode)httpException.GetHttpCode(), $"Server error");
return;
}
context.Result = new ErrorResult(context.Request, HttpStatusCode.InternalServerError, $"Server Error");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment