Created
April 23, 2016 18:14
-
-
Save seangwright/a231be14e1871f489602a1b25f425598 to your computer and use it in GitHub Desktop.
GlobalExceptionHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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