-
-
Save ccpu/064b1e3e3fd93f67fabcf8d99eec69ef to your computer and use it in GitHub Desktop.
Revisions
-
Ciantic revised this gist
Apr 20, 2016 . 1 changed file with 19 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ using System; using Microsoft.AspNetCore.Mvc.Filters; namespace Example { public class ApiExceptionFilter : Attribute, IExceptionFilter { public void OnException(ExceptionContext context) { if (context.Exception is Exception) { context.Result = new ObjectResult(new { Error = "Exception", Exception = context.Exception }); context.Exception = null; } } } } -
Ciantic revised this gist
Apr 20, 2016 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.Extensions.Options; using System; namespace Example { -
Ciantic revised this gist
Apr 20, 2016 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -32,11 +32,11 @@ public async Task Invoke(HttpContext context) throw; } context.Response.StatusCode = 500; context.Response.Clear(); await _oex.ExecuteAsync(new ActionContext() { HttpContext = context }, new ObjectResult(new { Error = "Exception", Exception = e })); } } } -
Ciantic revised this gist
Apr 20, 2016 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -32,11 +32,11 @@ public async Task Invoke(HttpContext context) throw; } context.Response.StatusCode = 500; context.Response.Clear(); await _oex.ExecuteAsync(new ActionContext() { HttpContext = context }, new ObjectResult(new { Error = "Exception", Exception = e })); } } } -
Ciantic created this gist
Apr 20, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ using Microsoft.AspNetCore.Http; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.Extensions.Options; namespace Example { public class ApiExceptionMiddleware { private readonly RequestDelegate _next; private readonly ILogger _logger; private readonly ObjectResultExecutor _oex; public ApiExceptionMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, ObjectResultExecutor oex) { _next = next; _logger = loggerFactory.CreateLogger<ApiExceptionMiddleware>(); _oex = oex; } public async Task Invoke(HttpContext context) { try { await _next.Invoke(context); } catch (Exception e) { if (context.Response.HasStarted) { _logger.LogWarning("The response has already started, the api exception middleware will not be executed"); throw; } context.Response.StatusCode = 500; context.Response.Clear(); await _oex.ExecuteAsync(new ActionContext() { HttpContext = context }, new ObjectResult(new { Error = "Exception", Exception = e })); } } } }